package com.example.pageobjects.pakistan;

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

public class AddToCartPage {
    public WebDriver driver;

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

    public void closePopup() {
        driver.findElement(By.xpath("//button[@aria-label='Close']")).click();
    }

    public void contributeButton() {
        driver.findElement(By.cssSelector("div.container  a:nth-child(1) > button")).click();
    }

    public void donateNowButton() {
        driver.findElement(By.cssSelector("div:nth-child(3) div:nth-child(3) img:nth-child(1)")).click();
    }

    public void addToCartItem(String items) {
        driver.findElement(By.xpath(String.format("//p[normalize-space()='%s']/../../a[2]/p", items))).click();
    }

    public void addItem2() {
        driver.findElement(By.cssSelector("div:nth-child(6) > div:nth-child(5) > div > a:nth-child(3) > p")).click();
    }

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

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

    public WebElement donationAddedToCart(String text) {
        return driver.findElement(By.xpath(String.format("//h4[contains(text(),'%s')]", text)));
    }

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

    public void closeMessengerPopUp() throws InterruptedException {
        Thread.sleep(8000);
        driver.findElement(By.cssSelector("#fb-root > div.fb_dialog.fb_dialog_advanced > div > iframe:nth-child(1)")).click();
    }

    public WebElement removeDonation(String donation) {

        return driver.findElement(By.xpath(String.format("(//button[normalize-space()='Remove'])['%s']", donation)));
    }

    public String numberOfDonation() {
        return driver.findElement(By.cssSelector(".modal-body .no-of-cart-items")).getText().trim();
    }

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

    public String DonationTotalAmountInCart() {
        return driver.findElement(By.cssSelector("span.cart-amount")).getText();
    }

    public WebElement amountInCart(String amount) {
        return driver.findElement(By.xpath(String.format("(//div[@class='col-xs-5 text-right'][normalize-space()='%s'])", amount)));
    }

    public void amountAddedForDonationInCart(String amount) {
        Select donationAmount = new Select(driver.findElement(By.xpath("//select[@name='amount']")));
        donationAmount.selectByVisibleText(amount);
    }

    public WebElement enterAmountForDonationInCart() {
        return driver.findElement(By.cssSelector("input[id='input_other_amount']"));
    }


}
