package zacksolutions;

import java.time.Duration;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;

public class Scope {

  public static WebDriver driver;

  public static void main(String[] args) throws InterruptedException {
    Initialize();
  }

  public static void Initialize() throws InterruptedException {
    System.setProperty("web-driver.gecko.driver", "user.dir" + "LearnSelenium/DemoProject/drivers/geckodriver");
    driver = new FirefoxDriver();
    driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(5));
    String url = "https://qaclickacademy.com/practice.php";
    driver.get(url);
    try {
      linksCount();
    } finally {
      driver.quit();
    }
  }

  public static void linksCount() throws InterruptedException {
    //all the links in the home page
    List<WebElement> linkC = driver.findElements(By.tagName("a"));
    System.out.println("The full page links count is: " + linkC.size());

    //all links in the pageFooter
    WebElement footdriver = driver.findElement(By.xpath("//div[@id='gf-BIG']"));
    List<WebElement> footLinks = footdriver.findElements(By.tagName("a"));
    System.out.println("The footer pages links amount to: " + footLinks.size());

    //all the links in the first column
    WebElement columnDriver = footdriver.findElement(By.xpath("//div/table/tbody/tr/td[1]/ul"));
    List<WebElement> columnLinks = columnDriver.findElements(By.tagName("a"));
    System.out.println("The column links amount to: " + columnLinks.size());

    //Open each link in the column
    for(int i = 1; i < columnLinks.size(); i ++){
      String clickON = Keys.chord(Keys.CONTROL, Keys.ENTER);
      columnDriver.findElements(By.tagName("a")).get(i).sendKeys(clickON);
      Thread.sleep(5000);
    }
    Set<String> tabs = driver.getWindowHandles();
    Iterator<String> it = tabs.iterator();
    while(it.hasNext()){
      driver.switchTo().window(it.next());
      System.out.println(driver.getTitle());
    }
  }
}
