package systemTest;

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 org.openqa.selenium.remote.DesiredCapabilities;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

//import org.openqa.selenium.firefox.FirefoxDriver;

public class MobileJudgeTester {
    public static void main(String[] args) {
        // Create a new instance of the Firefox driver
        // Notice that the remainder of the code relies on the interface, 
        // not the implementation.
        //WebDriver driver = new FirefoxDriver();
        /*WebDriver driver = new ChromeDriver();

        // And now use this to visit Google
        driver.get("http://www.google.com");
        // Alternatively the same thing can be done like this
        // driver.navigate().to("http://www.google.com");

        // Find the text input element by its name
        WebElement element = driver.findElement(By.name("q"));

        // Enter something to search for
        element.sendKeys("Cheese!");

        // Now submit the form. WebDriver will find the form for us from the element
        element.submit();

        // Check the title of the page
        System.out.println("Page title is: " + driver.getTitle());
        
        // Google's search is rendered dynamically with JavaScript.
        // Wait for the page to load, timeout after 10 seconds
        (new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() {
            public Boolean apply(WebDriver d) {
                return d.getTitle().toLowerCase().startsWith("cheese!");
            }
        });

        // Should see: "cheese! - Google Search"
        System.out.println("Page title is: " + driver.getTitle());
        
        //Close the browser
       // driver.quit();*/
        //tester();
        //testMobileJudge();
        testMain();
    }

    public static void testGoogleSearch() {
        // Optional, if not specified, WebDriver will search your path for chromedriver.
        System.setProperty("webdriver.chrome.driver", "C:/Users/venitmcar/Downloads/chromedriver_win32/chromedriver.exe");

        WebDriver driver = new ChromeDriver();
        //driver.get("http://www.google.com/xhtml");
        driver.get("http://localhost:3000/");
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }  // Let the user actually see something!
        WebElement authorBox = driver.findElement(By.name("authorBox"));
        authorBox.sendKeys("Marlon Cardero");
        WebElement commentBox = driver.findElement(By.name("commentBox"));
        commentBox.sendKeys("This is MY comment");
        WebElement postButton = driver.findElement(By.name("postButton"));
        postButton.submit();
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }  // Let the user actually see something!
        driver.quit();
    }

    public static void testMobileJudge() {
        System.setProperty("webdriver.chrome.driver", "C:/Users/venitmcar/Downloads/chromedriver_win32/chromedriver.exe");

        DesiredCapabilities capabilities = DesiredCapabilities.chrome();
        ChromeOptions options = new ChromeOptions();
        options.addArguments("start-maximized");
        options.addArguments("--disable-web-security");
        capabilities.setCapability(ChromeOptions.CAPABILITY, options);
        ChromeDriver testDriver = new ChromeDriver(capabilities);

        testDriver.get("http://localhost");
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        ArrayList<String> students = new ArrayList();
        ArrayList<String> judges = new ArrayList();
        ArrayList<Integer> grades = new ArrayList();

        for (int i = 1; i < 4; i++) {
            students.add("Student" + i);
        }
        for (int i = 1; i < 4; i++) {
            judges.add("Judge" + i);
        }
        for (int i = 1; i < 4; i++) {
            grades.add(i + 90);
        }

        WebElement ta = testDriver.findElement(By.tagName("table"));
        WebElement tb = ta.findElement(By.tagName("tbody"));
        List<WebElement> ro = tb.findElements(By.tagName("tr"));
        int size = ro.size();
        Reporter.initialize();

        Iterator studentIter = students.iterator();
        while (studentIter.hasNext()) {
            Iterator judgeIter = judges.iterator();
            while (judgeIter.hasNext()) {
                Iterator gradeIter = grades.iterator();
                while (gradeIter.hasNext()) {
                    WebElement studentName = testDriver.findElement(By.name("studentName"));
                    String student = studentIter.next().toString();
                    studentName.sendKeys(student);
                    WebElement judgeName = testDriver.findElement(By.name("judgeName"));
                    String judge = judgeIter.next().toString();
                    judgeName.sendKeys(judge);
                    WebElement gradeValue = testDriver.findElement(By.name("grade"));
                    String grade = gradeIter.next().toString();
                    gradeValue.sendKeys(grade);
                    WebElement addButton = testDriver.findElement(By.name("addButton"));
                    addButton.click();

                    WebElement tab = testDriver.findElement(By.tagName("table"));
                    WebElement tbody = tab.findElement(By.tagName("tbody"));
                    WebElement row = tbody.findElements(By.tagName("tr")).get(size);
                    List<WebElement> cells = row.findElements(By.tagName("td"));
                    String getStudent = cells.get(0).getText() + " "
                            + cells.get(1).getText() + " "
                            + cells.get(2).getText();
                    String getTabStudent = student + " " + judge + " " + grade;
                    size++;
                    Reporter.report(getTabStudent, getStudent);

                }
            }
        }

        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        Reporter.writeResults();
        testDriver.quit();
    }

    public static void testMain() {
        System.setProperty("webdriver.chrome.driver", "C:/Users/venitmcar/Downloads/chromedriver_win32/chromedriver.exe");

        DesiredCapabilities capabilities = DesiredCapabilities.chrome();
        ChromeOptions options = new ChromeOptions();
        options.addArguments("start-maximized");
        options.addArguments("--disable-web-security");
        capabilities.setCapability(ChromeOptions.CAPABILITY, options);
        ChromeDriver testDriver = new ChromeDriver(capabilities);

        testDriver.get("http://localhost");
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        WebElement exampleInputEmail1 = testDriver.findElement(By.id("exampleInputEmail1"));
        exampleInputEmail1.sendKeys("test@test.co");
        WebElement exampleInputPassword1 = testDriver.findElement(By.id("exampleInputPassword1"));
        exampleInputPassword1.sendKeys("qwerty21");
        WebElement loginme = testDriver.findElement(By.id("loginme"));
        loginme.click();

        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }  // Let the user actually see something!

        WebElement adminEmail = testDriver.findElement(By.id("adminEmail"));
        adminEmail.click();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }  // Let the user actually see something!
        WebElement all = testDriver.findElement(By.id("all"));
        all.click();
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }  // Let the user actually see something!
        WebElement field_3 = testDriver.findElement(By.id("field-3"));
        field_3.click();
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }  // Let the user actually see something!
        WebElement filter_2 = testDriver.findElement(By.id("filter-2"));
        filter_2.click();
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }  // Let the user actually see something!
        WebElement filter_1 = testDriver.findElement(By.id("filter-1"));
        filter_1.click();
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }  // Let the user actually see something!
        WebElement extras = testDriver.findElement(By.id("extras"));
        extras.sendKeys("test@test.co");
        extras.sendKeys("\n");
        extras.sendKeys("second@test.com");
        extras.sendKeys("\n");
        extras.sendKeys("last@test.info");
        WebElement toEmailListButton = testDriver.findElement(By.id("toEmailListButton"));
        toEmailListButton.click();
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }  // Let the user actually see something!
        WebElement emailRow = testDriver.findElement(By.className("4245982"));
        emailRow.click();
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }  // Let the user actually see something!
        WebElement emailRow2 = testDriver.findElement(By.id("6"));
        emailRow2.click();
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }  // Let the user actually see something!
        WebElement emailRow3 = testDriver.findElement(By.id("test@test.co"));
        emailRow3.click();
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }  // Let the user actually see something!
        testDriver.findElement(By.id("toEmailListButton"));
        toEmailListButton.click();
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }  // Let the user actually see something!
        WebElement templateSelect = testDriver.findElement(By.id("templateSelect"));
        templateSelect.click();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }  // Let the user actually see something!
        WebElement option = testDriver.findElement(By.id("2"));
        option.click();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }  // Let the user actually see something!

        /*try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }  // Let the user actually see something!*/

        WebElement sendButton = testDriver.findElement(By.id("sendButton"));
        sendButton.click();
        WebElement roleDop = testDriver.findElement(By.id("roleDop"));
        roleDop.click();
        WebElement roleStudent = testDriver.findElement(By.id("roleStudent"));
        roleStudent.click();

        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }  // Let the user actually see something!

        /*testDriver.findElement(By.id("judgeHome"));
        judgeHome.click();

        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }  // Let the user actually see something!*/

        testDriver.quit();

    }
};