package com.example.stepdefinations.pakistan;

import com.example.pageobjects.pakistan.HomePage;
import com.example.utils.TextContextSetup;
import io.cucumber.datatable.DataTable;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;
import org.openqa.selenium.Keys;

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

public class HomePageSteps {

    TextContextSetup textContextSetup;

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

    @When("I Click on About Us button on Home page")
    public void i_click_on_about_us_button_on_home_page() {
        HomePage homePage = textContextSetup.pagesObject.getHomePage();
        homePage.aboutUsButtonLocator().click();
    }

    @When("I Click On Our Timeline Button On home page")
    public void i_click_on_our_timeline_button_on_home_page() {
        HomePage homePage = textContextSetup.pagesObject.getHomePage();
        homePage.timeLineButtonLocator().click();
    }

    @When("I Click On Our contact Button On home page")
    public void i_click_on_our_contact_button_on_home_page() {
        HomePage homePage = textContextSetup.pagesObject.getHomePage();
        homePage.contactButtonLocator().click();
    }

    @When("I select a {string} feature on home page")
    public void i_select_feature(String feature) throws InterruptedException {
        HomePage homePage = textContextSetup.pagesObject.getHomePage();
        if (feature.equalsIgnoreCase("welfare")) {
            homePage.featureLocator(1).click();
            homePage.featureLearnMoreButtonLocator(feature).click();
        }
        if (feature.equalsIgnoreCase("education")) {
            homePage.featureLocator(2).click();
            homePage.featureLearnMoreButtonLocator(feature).click();
        }
        if (feature.equalsIgnoreCase("publication")) {
            homePage.featureLocator(3).click();
            homePage.featureLearnMoreButtonLocator(feature).click();
        }
        if (feature.equalsIgnoreCase("bayanat")) {
            homePage.featureLocator(4).click();
            homePage.featureLearnMoreButtonLocator(feature).click();
        }
    }

    @Then("^I should see the following bayanat on bayan page:$")
    public void i_should_see_bayanat_list(DataTable bayanList) throws InterruptedException {
        HomePage homePage = textContextSetup.pagesObject.getHomePage();
        Thread.sleep(7000);
        List<Map<String, String>> data = bayanList.asMaps(String.class, String.class);
        for (Map<String, String> itemData : data) {
            homePage.bayanListLocator(itemData.get("Bayanat")).isDisplayed();
        }
    }

    @Then("^I should see the following publication on Publication page:$")
    public void i_should_see_publication_list(DataTable bayanList) throws InterruptedException {
        HomePage homePage = textContextSetup.pagesObject.getHomePage();
        Thread.sleep(7000);
        List<Map<String, String>> data = bayanList.asMaps(String.class, String.class);
        for (Map<String, String> itemData : data)
            homePage.publicationListLocator(itemData.get("Publication")).isDisplayed();
    }

    @Then("^I should see the following education projects on Education page:$")
    public void i_should_see_education_project_list(DataTable bayanList) throws InterruptedException {
        HomePage homePage = textContextSetup.pagesObject.getHomePage();
        Thread.sleep(7000);
        List<Map<String, String>> data = bayanList.asMaps(String.class, String.class);
        for (Map<String, String> itemData : data)
            homePage.educationProjectListLocator(itemData.get("Education")).isDisplayed();
    }

    @Then("^I should see the following welfare project on Welfare page:$")
    public void i_should_see_welfare_project_list(DataTable bayanList) throws InterruptedException {
        HomePage homePage = textContextSetup.pagesObject.getHomePage();
        Thread.sleep(7000);
        List<Map<String, String>> data = bayanList.asMaps(String.class, String.class);
        for (Map<String, String> itemData : data)
            homePage.welfareProjectLocator(itemData.get("Welfare Project")).isDisplayed();
    }

    @Then("I enter email {string} to subscribe 'BAITUSSALAMS NEWSLETTER' on home page")
    public void i_enter_email_text(String email) {
        HomePage homePage = textContextSetup.pagesObject.getHomePage();
        homePage.emailFieldForSubscription().sendKeys(email);
        homePage.emailFieldForSubscription().sendKeys(Keys.ENTER);
    }

    @Then("^I should see the success message$")
    public void i_Should_See_Success_Message() throws InterruptedException {
        HomePage homePage = textContextSetup.pagesObject.getHomePage();
        Thread.sleep(3000);
        homePage.successMessage().isDisplayed();
        String successMessage = homePage.successMessage().getText();
        System.out.println("Success alert " + successMessage);
    }

    @Then("I click on {string} to follow other social media account")
    public void i_Click_Other_Social_Media_Account(String button) throws InterruptedException {
        HomePage homePage = textContextSetup.pagesObject.getHomePage();
        Thread.sleep(3000);
        homePage.socialMediaButton(button).click();
        Thread.sleep(7000);
    }

    @When("I fill contact form with following data :")
    public void iFillFromOfOccupationInformation(DataTable formData) {
        HomePage homePage = textContextSetup.pagesObject.getHomePage();

        List<Map<String, String>> data = formData.asMaps(String.class, String.class);
        for (Map<String, String> itemData : data) {
            if (itemData.containsKey("name")) {
                homePage.contactNameField().clear();
                homePage.contactNameField().sendKeys(itemData.get("name"));
            }
            if (itemData.containsKey("subject")) {
                homePage.contactSubjectField().clear();
                homePage.contactSubjectField().sendKeys(itemData.get("subject"));
            }
            if (itemData.containsKey("phone")) {
                homePage.contactPhoneField().clear();
                homePage.contactPhoneField().sendKeys(itemData.get("phone"));
            }
            if (itemData.containsKey("message")) {
                homePage.messageField().clear();
                homePage.messageField().sendKeys(itemData.get("message"));
            }
            if (itemData.containsKey("email")) {
                homePage.contactEmailField().clear();
                String[] arrSplit = itemData.get("email").split("@");
                String emailAddress = "";
                while (emailAddress.length() < 5) {
                    emailAddress += arrSplit[0];
                    emailAddress += Integer.valueOf((int) (Math.random() * 99)).toString();
                    emailAddress += "@" + arrSplit[1];
                    homePage.contactEmailField().sendKeys(emailAddress);
                }
            }
        }
    }

}


