package com.example.stepdefinations.pakistan;

import com.example.pageobjects.pakistan.AddToCartPage;
import com.example.utils.TextContextSetup;
import io.cucumber.datatable.DataTable;
import io.cucumber.java.en.And;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;

import org.testng.Assert;

import java.util.List;
import java.util.Map;


public class AddToCartSteps {
    TextContextSetup textContextSetup;

    public AddToCartSteps(TextContextSetup textContextSetup) {
        this.textContextSetup = textContextSetup;
    }

    AddToCartPage addToCartPage;

    @And("Close the popup")
    public void closePopUp() throws InterruptedException {
        AddToCartPage addToCartPage = textContextSetup.pagesObject.getAddToCartPage();
        Thread.sleep(3000);
        addToCartPage.closePopup();
    }

    @When("^Click on Contribute button$")
    public void iClickOnContributeButton() {
        AddToCartPage addToCartPage = textContextSetup.pagesObject.getAddToCartPage();
        addToCartPage.contributeButton();
    }

    @When("^Click on Donate Now$")
    public void iClickOnDonateNow() {
        AddToCartPage addToCartPage = textContextSetup.pagesObject.getAddToCartPage();
        addToCartPage.donateNowButton();
    }

    @When("I add {string} donation to the cart")
    public void iAddItemToCart(String addItem) throws InterruptedException {
        AddToCartPage addToCartPage = textContextSetup.pagesObject.getAddToCartPage();
        Thread.sleep(2000);
        addToCartPage.addToCartItem(addItem);
    }

    @When("^I add donation to the cart$")
    public void iAddItem2ToCart() throws InterruptedException {
        AddToCartPage addToCartPage = textContextSetup.pagesObject.getAddToCartPage();
        Thread.sleep(2000);
        addToCartPage.addItem2();
    }

    @When("I select {string} payment")
    public void iSelectPayment(String paymentType) throws InterruptedException {
        AddToCartPage addToCartPage = textContextSetup.pagesObject.getAddToCartPage();
        Thread.sleep(2000);
        addToCartPage.paymentTypes(paymentType).click();
    }

    @When("I press {string}")
    public void iPressButton(String button) throws InterruptedException {
        AddToCartPage addToCartPage = textContextSetup.pagesObject.getAddToCartPage();
        Thread.sleep(1000);
        addToCartPage.clickButton(button).click();
        Thread.sleep(4000);
    }

    @When("I should see {string} is added to the cart")
    public void iShouldSeeDonationAddedToCart(String donation) throws InterruptedException {
        AddToCartPage addToCartPage = textContextSetup.pagesObject.getAddToCartPage();
        Thread.sleep(1000);
        addToCartPage.donationAddedToCart(donation).isDisplayed();
    }

    @When("^I close the messenger popUp$")
    public void messengerPopUp() throws InterruptedException {
        AddToCartPage addToCartPage = textContextSetup.pagesObject.getAddToCartPage();
        Thread.sleep(7000);
        addToCartPage.closeMessengerPopUp();
    }

    @When("I remove {string} Donation to the cart")
    public void iRemoveDonationToCart(String donation) throws InterruptedException {
        AddToCartPage addToCartPage = textContextSetup.pagesObject.getAddToCartPage();
        Thread.sleep(1000);
        addToCartPage.removeDonation(donation).click();
    }

    @Then("I should see You are donating to {string} cause")
    public void iShouldSeeNumberOfDonationAddedToCart(String donation) throws InterruptedException {
        AddToCartPage addToCartPage = textContextSetup.pagesObject.getAddToCartPage();
        String donationCount = donation;
        Thread.sleep(2000);
        String actual = addToCartPage.numberOfDonation();
        System.out.println("Visible On Web : " + actual);
        Assert.assertEquals(donationCount, actual);
    }

    @Then("User should be redirected to {string} page")
    public void iShouldSeeCheckOutPage(String page) throws InterruptedException {
        AddToCartPage addToCartPage = textContextSetup.pagesObject.getAddToCartPage();
        Thread.sleep(2000);
        addToCartPage.pageHeader(page).isDisplayed();
    }

    @Then("I should see total amount Rs{string} on add to cart module")
    public void iShouldSeeTotalAmountOnCartModule(String amount) throws InterruptedException {
        AddToCartPage addToCartPage = textContextSetup.pagesObject.getAddToCartPage();
        String totalAmount = amount;
        Thread.sleep(2000);
        String actual = addToCartPage.DonationTotalAmountInCart();
        System.out.println("Visible On Web : " + actual);
        Assert.assertEquals(totalAmount, actual);
    }


    @When("^I should see the following donation detail added to the cart$")
    public void iShouldSeeFollowingDonationAmountAddedToCart(DataTable donations) {
        AddToCartPage addToCartPage = textContextSetup.pagesObject.getAddToCartPage();
        List<Map<String, String>> data = donations.asMaps(String.class, String.class);
        for (Map<String, String> itemData : data) {
            if (itemData.containsKey("title")) {
                addToCartPage.donationInCart(itemData.get("title")).isDisplayed();
            }
            if (itemData.containsKey("amount")) {
                addToCartPage.amountInCart(itemData.get("amount")).isDisplayed();
            }
        }
    }

    @Then("I select {string} amount for the donation")
    public void iShouldSelectAmountOfDonation(String amount) {
        AddToCartPage addToCartPage = textContextSetup.pagesObject.getAddToCartPage();
        addToCartPage.amountAddedForDonationInCart(amount);
    }
    @Then("I enter amount of {string} for donation")
    public void iShouldEnterAmountOfDonation(String amount) {
        AddToCartPage addToCartPage = textContextSetup.pagesObject.getAddToCartPage();
        addToCartPage.enterAmountForDonationInCart().clear();
        addToCartPage.enterAmountForDonationInCart().sendKeys(amount);
    }
}
