package ru.stqa.training.selenium;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeUnit;

import static org.junit.Assert.assertTrue;

/**
 * Created by ilya on 04/02/2017.
 */
public class SortingOfCountriesAndGeozones {
    private WebDriver driver;

    @Before
    public void before()
    {
        ChromeOptions chromeOptions = new ChromeOptions();
        chromeOptions.addArguments("start-fullscreen");
        driver = new ChromeDriver(chromeOptions);
        driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
    }

    @Test
    public void countriesAndGeozonesShouldBeSorted() {
        driver.get("http://localhost/litecart/admin/?app=countries&doc=countries");
        driver.findElement(By.name("username")).sendKeys("admin");
        driver.findElement(By.name("password")).sendKeys("admin");
        driver.findElement(By.name("login")).click();

        List<WebElement> countryRows = driver.findElements(By.cssSelector("tr.row"));
        List<String> checkCountrySort = new ArrayList<>();
        List<String> sortedCountries = new ArrayList<>();
        List<String> haveZones = new ArrayList<>();

//        check country names are sorted
        for (WebElement row : countryRows) {
            WebElement countryName = row.findElement(By.cssSelector("td:nth-of-type(5) a"));
            checkCountrySort.add(countryName.getText());

            if (!row.findElement(By.cssSelector("td:nth-of-type(6)")).getText().equals("0")){
                haveZones.add(countryName.getAttribute("href"));
            }
        }
        sortedCountries.addAll(checkCountrySort);
        Collections.sort(sortedCountries);

        for (int i = 0; i < checkCountrySort.size(); i++) {
            assertTrue(checkCountrySort.get(i).equals(sortedCountries.get(i)));
        }

//        check zone names are sorted
        for (String url : haveZones) {
            driver.get(url);
            List<WebElement> zoneRows = driver.findElements(By.cssSelector("#table-zones tr:not(.header)"));
            List<String> checkZonesSort = new ArrayList<>();
            List<String> sortedZones = new ArrayList<>();
            for (WebElement row : zoneRows) {
                checkZonesSort.add(row.findElement(By.cssSelector("td:nth-of-type(3)")).getText());
            }

            checkZonesSort.remove(checkZonesSort.size()-1);
            sortedZones.addAll(checkZonesSort);
            Collections.sort(sortedZones);

            for (int i = 0; i < checkZonesSort.size(); i++) {
                assertTrue(checkZonesSort.get(i).equals(sortedZones.get(i)));
            }
        }

    }

    @After
    public void after(){
        driver.quit();
        driver = null;
    }
}

@@ -1037,184 +1037,13 @@
     
-driver.findElement(By.name(%22username%22)).sendKeys(%22admin%22);%0A        driver.findElement(By.name(%22password%22)).sendKeys(%22admin%22);%0A        driver.findElement(By.name(%22login%22)).click
+login
 ();%0A
@@ -2792,83 +2792,1533 @@
    @
-After%0A    public void after()%7B%0A        driver.quit();%0A        driver = null
+Test%0A    public void geozonesShouldBeSorted()%7B%0A        driver.get(%22http://localhost/litecart/admin/?app=geo_zones&doc=geo_zones%22);%0A        login();%0A        List%3CString%3E urls = new ArrayList%3C%3E();%0A        List%3CWebElement%3E countryNames = driver.findElements(By.cssSelector(%22tr.row td:nth-of-type(3) a%22));%0A        List%3CWebElement%3E selectedZoneOptions;%0A        List%3CString%3E checkZonesSort;%0A        List%3CString%3E sortedZones;%0A        for (WebElement link : countryNames ) %7B%0A            urls.add(link.getAttribute(%22href%22));%0A        %7D%0A%0A        for (String url : urls) %7B%0A            driver.get(url);%0A%0A            selectedZoneOptions = driver.findElements(By.cssSelector(%22#table-zones td:nth-of-type(3) option%5Bselected%5D%22));%0A            checkZonesSort = new ArrayList%3C%3E();%0A            sortedZones = new ArrayList%3C%3E();%0A%0A            for (WebElement selectedZoneOption : selectedZoneOptions) %7B%0A                checkZonesSort.add(selectedZoneOption.getText());%0A            %7D%0A%0A            sortedZones.addAll(checkZonesSort);%0A            Collections.sort(sortedZones);%0A%0A            for (int i = 0; i %3C checkZonesSort.size(); i++) %7B%0A                assertTrue(checkZonesSort.get(i).equals(sortedZones.get(i)));%0A            %7D%0A        %7D%0A    %7D%0A%0A    @After%0A    public void after()%7B%0A        driver.quit();%0A        driver = null;%0A    %7D%0A%0A    private void login() %7B%0A        driver.findElement(By.name(%22username%22)).sendKeys(%22admin%22);%0A        driver.findElement(By.name(%22password%22)).sendKeys(%22admin%22);%0A        driver.findElement(By.name(%22login%22)).click()
 ;%0A  

add test geozonesShouldBeSorted