package com.example.pageobjects.usa;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.Select;

import java.util.Iterator;
import java.util.Set;

public class WebPage {
    public WebDriver driver;

    public WebPage(WebDriver driver) {
        this.driver = driver;
    }

    public By donationCauseDropDownLocator = By.cssSelector("#dropdown1");
    public By donationTypeDropDownLocator = By.cssSelector("#dropdown2");
    public By donationAmountLocator = By.cssSelector("#dropdown3");


    public void selectDonationCause(String type) {
        driver.findElement(donationCauseDropDownLocator).click();
        Select donationCause = new Select(driver.findElement(By.cssSelector("#dropdown1")));
        donationCause.selectByVisibleText(type);
    }

    public void selectDonationType(String type) {
        driver.findElement(donationTypeDropDownLocator).click();
        Select donationCause = new Select(driver.findElement(By.cssSelector("#dropdown2")));
        donationCause.selectByVisibleText(type);
    }

    public WebElement paymentTypes(String payment) {
        return driver.findElement(By.xpath(String.format("//label[normalize-space()='%s']", payment)));
    }

    public void clickButton(String buttons) {
        driver.findElement(By.xpath(String.format("//button[normalize-space()='%s']", buttons))).click();
    }

    public WebElement titleButton(String title) {
        return driver.findElement(By.xpath(String.format("//p[normalize-space()='%s']", title)));
    }

    public WebElement payPalButton() {
        return driver.findElement(By.xpath(" //button[@type='submit']"));
    }

    public WebElement title(String title) {
        return driver.findElement(By.xpath(String.format("//h2[normalize-space()='%s']", title)));
    }

    public WebElement contactTitle(String title) {
        return driver.findElement(By.xpath(String.format("//h3[normalize-space()='%s']", title)));
    }

    public String payPalTitle() {
        return driver.findElement(By.xpath("//h1[normalize-space()='Log in to PayPal']")).getText();
    }

    public void selectPrograms(String programs) {
        WebElement menuOption = driver.findElement(By.xpath("//p[normalize-space()='Programs']"));
        Actions actions = new Actions(driver);
        actions.moveToElement(menuOption).perform();
        driver.findElement(By.xpath(String.format("//p[normalize-space()='%s']", programs))).click();
    }

    public String getCurrentUrl() {
        String CurrentUrl = driver.getCurrentUrl();
        System.out.println(CurrentUrl);
        return CurrentUrl;
    }

    public void switchScreen() throws InterruptedException {
        Thread.sleep(2000);
        Set<String> s1 = driver.getWindowHandles();
        Iterator<String> i1 = s1.iterator();
        String parentWindow = i1.next();
        String childWindow = i1.next();
        driver.switchTo().window(childWindow);
    }

}
