package zacksolutions;

import org.apache.maven.surefire.shared.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

import java.io.File;
import java.io.IOException;
import java.time.Duration;

public class Locators {
  public static WebDriver driver;

  public static void main(String[] args) throws InterruptedException, IOException {
    System.setProperty("web-driver.gecko.driver", "/home/ilyes/LearnSelenium/DemoProject/drivers/geckodriver");
    driver = new FirefoxDriver();
    driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(5));
    String url = "https://rahulshettyacademy.com/locatorspractice/";
    driver.get(url);
    loginPage();
    getScreenShot(driver);
  }

  public static void loginPage() throws InterruptedException {
    driver.findElement(By.cssSelector("input[id='inputUsername']")).sendKeys("Zakaria");
    driver.findElement(By.cssSelector("input[name='inputPassword']")).sendKeys("Zakaria");
    driver.findElement(By.cssSelector("button[type='submit']")).click();
    String Err = driver.findElement(By.cssSelector("p.error")).getText();
    System.out.println(Err);
    driver.findElement(By.linkText("Forgot your password?")).click();
    driver.findElement(By.xpath("//input[@placeholder='Name']")).sendKeys("Desmond");
    driver.findElement(By.xpath("//input[@placeholder='Email']")).sendKeys("hdesmond@island.con");
    driver.findElement(By.cssSelector("input[placeholder='Phone Number']")).sendKeys("5551118888");
    Thread.sleep(1000);
    driver.findElement(By.cssSelector(".reset-pwd-btn")).click();
    System.out.println("");
    String newUser = driver.findElement(By.cssSelector("form p")).getText();
    System.out.println(newUser);
    String passw = "rahulshettyacademy";
    System.out.println("New User Created please enter the password: " + passw);
    Thread.sleep(1000);
    driver.findElement(By.cssSelector(".go-to-login-btn")).click();
    Thread.sleep(1000);
    driver.findElement(By.cssSelector("input[id='inputUsername']")).sendKeys("Desmond");
    driver.findElement(By.cssSelector("input[placeholder='Password']")).sendKeys(passw);
    driver.findElement(By.xpath("//input[@id='chkboxOne']")).click();
    driver.findElement(By.xpath("//input[@id='chkboxTwo']")).click();
    driver.findElement(By.cssSelector("button[type='submit']")).click();
    Thread.sleep(1000);
    String homePage = driver.getTitle();
    System.out.println(homePage);
    Thread.sleep(2000);
  }

  public static void getScreenShot(WebDriver driver) throws IOException {
    File src = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
    FileUtils.copyFile(src, new File("/home/ilyes/SeleniumPractice/SeleniumIntro/screenshot.png"));
    System.out.println("Picture taken... Please check your forlder");
    driver.close();
  }
}
