package com.example.stepdefinations.pakistan;

import com.example.pageobjects.pakistan.PublicationPage;
import com.example.pageobjects.pakistan.WelfarePage;
import com.example.utils.TextContextSetup;
import io.cucumber.datatable.DataTable;
import io.cucumber.java.en.Given;
import io.cucumber.java.en.When;

import org.openqa.selenium.support.ui.Select;

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


public class WelfarePageSteps {
    TextContextSetup textContextSetup;

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

    @Given("I select {string} Donation category form dropdown on welfare page")
    public void i_select_donation_category_form_dropdown_on_welfare_page(String DonationCategory) throws InterruptedException {
        WelfarePage welfarePage = textContextSetup.pagesObject.getWelfarePage();
        Thread.sleep(4000);
        Select Donation = new Select(welfarePage.donationCategory());
        Donation.selectByVisibleText(DonationCategory);
    }

    @Given("I select {string} Sub Donation category form dropdown on welfare page")
    public void i_select_sub_donation_category_form_dropdown_on_welfare_page(String subDonationCategory) throws InterruptedException {
        WelfarePage welfarePage = textContextSetup.pagesObject.getWelfarePage();
        Thread.sleep(4000);
        Select subDonation = new Select(welfarePage.donationSubCategory());
        subDonation.selectByVisibleText(subDonationCategory);
    }

    @Given("I fill {string} in Amount field on welfare page")
    public void i_fill_in_in_amount_field_on_welfare_page(String amount) throws InterruptedException {
        WelfarePage welfarePage = textContextSetup.pagesObject.getWelfarePage();
        Thread.sleep(4000);
        String attr = welfarePage.amountField().getAttribute("readonly");
        welfarePage.amountField().sendKeys(amount);
//        Thread.sleep(4000);
//        System.out.println("attribute : "+attr);
//        if (attr.equalsIgnoreCase("null")){
//            welfarePage.amountField().sendKeys(amount);
//        }
    }

    @When("I select donation {string} cause in 'Donation now modal' on Home page")
    public void iSelectDonationCause(String type) {
        WelfarePage welfarePage = textContextSetup.pagesObject.getWelfarePage();
        welfarePage.SelectDonationCause(type);
    }

    @When("I select donation {string} type in 'Donation now modal' on Home page")
    public void iSelectDonationType(String type) {
        WelfarePage welfarePage = textContextSetup.pagesObject.getWelfarePage();
        welfarePage.SelectDonationType(type);
    }

    @When("I Enter Donation {string} amount")
    public void iEnterDonationAmount(String amount) {
        WelfarePage welfarePage = textContextSetup.pagesObject.getWelfarePage();
        Select selectAmount = new Select(welfarePage.amountFieldWelfare());
        selectAmount.selectByVisibleText(amount);
    }


    @When("I should see the {string} Donation is added to the cart")
    public void iShouldSeeDonationAddedToCart(String donation) throws InterruptedException {
        WelfarePage welfarePage = textContextSetup.pagesObject.getWelfarePage();
        Thread.sleep(3000);
        welfarePage.donationInCart(donation).isDisplayed();
    }

    @When("I should hover on {string} welfare header title to select {string} item from drop down on welfare page")
    public void iShouldSelectItemFromDropDownHeaderTitle(String header, String headerItem) throws InterruptedException {
        WelfarePage welfarePage = textContextSetup.pagesObject.getWelfarePage();
        Thread.sleep(300);
        welfarePage.welfarePageHeader(header);
        Thread.sleep(3000);
        welfarePage.welfarePageHeaderItem(headerItem).click();
    }

    @When("I click on {string} button on welfare page")
    public void iShouldSelectItemFromDropDownHeaderTitle(String header) throws InterruptedException {
        PublicationPage publicationPage = textContextSetup.pagesObject.getPublicationPage();
        Thread.sleep(300);
        publicationPage.pageHeader(header).click();
    }

    @When("I fill Volunteer information in the Become a Volunteer form with the following data on welfare Page:")
    public void iFillFromOfVolunteerInformation(DataTable formData) {
        WelfarePage welfarePage = textContextSetup.pagesObject.getWelfarePage();

        List<Map<String, String>> data = formData.asMaps(String.class, String.class);
        for (Map<String, String> itemData : data) {
            if (itemData.containsKey("fullName")) {
                welfarePage.fullNameFieldLocator().clear();
                welfarePage.fullNameFieldLocator().sendKeys(itemData.get("fullName"));
            }
            if (itemData.containsKey("DateOfBirth")) {
                welfarePage.dateOfBirthFieldLocator().clear();
                welfarePage.dateOfBirthFieldLocator().sendKeys(itemData.get("DateOfBirth"));
            }
            if (itemData.containsKey("email")) {
                welfarePage.emailFieldLocator().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];
                    welfarePage.emailFieldLocator().sendKeys(emailAddress);
                }
            }
            if (itemData.containsKey("phoneNumber")) {
                if (itemData.get("phoneNumber").equalsIgnoreCase("random")) {
                    welfarePage.phoneFieldLocator().clear();
                    String phoneNumber = welfarePage.phoneNumber();
                    welfarePage.phoneFieldLocator().sendKeys(phoneNumber);
                } else {
                    welfarePage.phoneFieldLocator().clear();
                    welfarePage.phoneFieldLocator().sendKeys(itemData.get("phoneNumber"));
                }
            }
            if (itemData.containsKey("CNIC")) {
                if (itemData.get("CNIC").equalsIgnoreCase("random")) {
                    welfarePage.cnicFieldLocator().clear();
                    String cnicNumber = welfarePage.cnicNumber();
                    welfarePage.cnicFieldLocator().sendKeys(cnicNumber);
                } else {
                    welfarePage.cnicFieldLocator().clear();
                    welfarePage.cnicFieldLocator().sendKeys(itemData.get("CNIC"));
                }
            }
            if (itemData.containsKey("address")) {
                welfarePage.addressFieldLocator().clear();
                welfarePage.addressFieldLocator().sendKeys(itemData.get("address"));
            }
            if (itemData.containsKey("city")) {
                welfarePage.cityFieldLocator().clear();
                welfarePage.cityFieldLocator().sendKeys(itemData.get("city"));
            }
            if (itemData.containsKey("country")) {
                welfarePage.countryFieldLocator().clear();
                welfarePage.countryFieldLocator().sendKeys(itemData.get("country"));
            }
            if (itemData.containsKey("mobileNumber")) {
                if (itemData.get("mobileNumber").equalsIgnoreCase("random")) {
                    welfarePage.mobileFieldLocator().clear();
                    String mobileNumber = welfarePage.mobileNumber();
                    welfarePage.mobileFieldLocator().sendKeys(mobileNumber);
                } else {
                    welfarePage.mobileFieldLocator().clear();
                    welfarePage.mobileFieldLocator().sendKeys(itemData.get("mobileNumber"));
                }
            }
            if (itemData.containsKey("postalCode")) {
                welfarePage.postalCodeFieldLocator().clear();
                welfarePage.postalCodeFieldLocator().sendKeys(itemData.get("postalCode"));
            }
        }
    }

    @When("I fill alternate contact information in the Become a Volunteer form with the following data on welfare Page:")
    public void iFillFromOfAlternateContactInformation(DataTable formData) {
        WelfarePage welfarePage = textContextSetup.pagesObject.getWelfarePage();

        List<Map<String, String>> data = formData.asMaps(String.class, String.class);
        for (Map<String, String> itemData : data) {
            if (itemData.containsKey("alternateFullName")) {
                welfarePage.alternateFullNameFieldLocator().clear();
                welfarePage.alternateFullNameFieldLocator().sendKeys(itemData.get("alternateFullName"));
            }
            if (itemData.containsKey("relation")) {
                welfarePage.relationFieldLocator().clear();
                welfarePage.relationFieldLocator().sendKeys(itemData.get("relation"));
            }
            if (itemData.containsKey("education")) {
                welfarePage.educationLocator().clear();
                welfarePage.educationLocator().sendKeys(itemData.get("education"));
            }
            if (itemData.containsKey("alternatePhoneNumber")) {
                welfarePage.alternatePhoneFieldLocator().clear();
                welfarePage.alternatePhoneFieldLocator().sendKeys(itemData.get("alternatePhoneNumber"));
            }
            if (itemData.containsKey("alternateAddress")) {
                welfarePage.alternateAddressFieldLocator().clear();
                welfarePage.alternateAddressFieldLocator().sendKeys(itemData.get("alternateAddress"));
            }
            if (itemData.containsKey("alternateCity")) {
                welfarePage.alternateCityFieldLocator().clear();
                welfarePage.alternateCityFieldLocator().sendKeys(itemData.get("alternateCity"));
            }
            if (itemData.containsKey("alternateCountry")) {
                welfarePage.alternateCountryFieldLocator().clear();
                welfarePage.alternateCountryFieldLocator().sendKeys(itemData.get("alternateCountry"));
            }
            if (itemData.containsKey("alternateMobileNumber")) {
                welfarePage.alternateMobileFieldLocator().clear();
                welfarePage.alternateMobileFieldLocator().sendKeys(itemData.get("alternateMobileNumber"));
            }
            if (itemData.containsKey("alternatePostalCode")) {
                welfarePage.alternatePostalCodeFieldLocator().clear();
                welfarePage.alternatePostalCodeFieldLocator().sendKeys(itemData.get("alternatePostalCode"));
            }
        }
    }

    @When("I fill occupation information in the Become a Volunteer form with the following data on welfare Page:")
    public void iFillFromOfOccupationInformation(DataTable formData) {
        WelfarePage welfarePage = textContextSetup.pagesObject.getWelfarePage();

        List<Map<String, String>> data = formData.asMaps(String.class, String.class);
        for (Map<String, String> itemData : data) {
            if (itemData.containsKey("Occupation")) {
                welfarePage.occupationFieldLocator().clear();
                welfarePage.occupationFieldLocator().sendKeys(itemData.get("Occupation"));
            }
            if (itemData.containsKey("typeOfEmployment")) {
                welfarePage.typeOfOccupationField(itemData.get("typeOfEmployment"));
            }
            if (itemData.containsKey("employmentName")) {
                welfarePage.employmentName().clear();
                welfarePage.employmentName().sendKeys(itemData.get("employmentName"));
            }
            if (itemData.containsKey("areaOfInterest")) {
                welfarePage.areaOfInterest().clear();
                welfarePage.areaOfInterest().sendKeys(itemData.get("areaOfInterest"));
            }
        }
    }

    @When("I fill Voluntarism Details in the Become a Volunteer form with the following data on welfare Page:")
    public void iFillVolunteerDetail(DataTable formData) {
        WelfarePage welfarePage = textContextSetup.pagesObject.getWelfarePage();

        List<Map<String, String>> data = formData.asMaps(String.class, String.class);
        for (Map<String, String> itemData : data) {
            if (itemData.containsKey("workExperience")) {
                if ((itemData.get("workExperience")).equalsIgnoreCase("Yes")) {
                    welfarePage.volunteerExperienceRadioButton(itemData.get("workExperience")).click();
                }
            }
            if (itemData.containsKey("workExperienceDetail")) {
                if (itemData.get("workExperience").equalsIgnoreCase("Yes")) {
                    welfarePage.volunteerExperienceDescription().sendKeys(itemData.get("workExperienceDetail"));
                }
            }
            if (itemData.containsKey("startWorkingDate")) {
                welfarePage.workStartDate().clear();
                welfarePage.workStartDate().sendKeys(itemData.get("startWorkingDate"));
            }
            if (itemData.containsKey("workingHourPerDay")) {
                welfarePage.workHourPerDay().clear();
                welfarePage.workHourPerDay().sendKeys(itemData.get("workingHourPerDay"));
            }
            if (itemData.containsKey("proficiencyInLanguage")) {
                welfarePage.languageProficiency().clear();
                welfarePage.languageProficiency().sendKeys(itemData.get("proficiencyInLanguage"));
            }
            if (itemData.containsKey("timeDetail")) {
                welfarePage.timeToVolunteerToggle(itemData.get("timeDetail")).click();
            }
            if (itemData.containsKey("monthlyAvailability")) {
                welfarePage.monthsAvailability(itemData.get("monthlyAvailability")).click();
            }
            if (itemData.containsKey("dayAvailability")) {
                String arr[] = itemData.get("dayAvailability").split(",");
                welfarePage.daysAvailability(arr[0], arr[1]).click();
            }
            if (itemData.containsKey("reasonForWorking")) {
                welfarePage.reasonToWork().sendKeys(itemData.get("reasonForWorking"));
            }
            if (itemData.containsKey("preferredField")) {
                welfarePage.fieldToPerform().sendKeys(itemData.get("preferredField"));
            }
            if (itemData.containsKey("haveDrivingLicense")) {
                welfarePage.drivingLicenseAvailability(itemData.get("haveDrivingLicense")).click();
            }
            if (itemData.containsKey("didKnownPersonWorking")) {
                welfarePage.knownWorkToggle(itemData.get("didKnownPersonWorking")).click();
            }
        }
    }

}
