package com.example.pageobjects.pakistan;

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.math.BigInteger;
import java.util.Random;

public class WelfarePage {
    public WebDriver driver;

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

    public By donationCauseDropDownLocator = By.xpath("//select[@id='item_name']");
    public By donationTypeDropDownLocator = By.xpath("//select[@id='donation_type']");
    public By donationAmountLocator = By.xpath("//input[@id='input_other_amount']");

    public WebElement amountField() {
        return driver.findElement(By.xpath("//input[@id='amount']"));
    }

    public WebElement donationSubCategory() {
        return driver.findElement(By.xpath("//select[@id='donation_sub_category_id']"));
    }

    public WebElement donationCategory() {
        return driver.findElement(By.xpath("//select[@id='donation_category_id']"));
    }

    public void SelectDonationCause(String type) {
        driver.findElement(donationCauseDropDownLocator).click();
        Select donationCause = new Select(driver.findElement(By.xpath("//select[@id='item_name']")));
        donationCause.selectByVisibleText(type);
    }

    public void SelectDonationType(String type) {
        driver.findElement(donationTypeDropDownLocator).click();
        Select donationCause = new Select(driver.findElement(By.xpath("//select[@id='donation_type']")));
        donationCause.selectByVisibleText(type);
    }

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

    public WebElement amountFieldWelfare() {
        return driver.findElement(By.xpath("//select[@name='amount']"));
    }

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

    public void welfarePageHeader(String pageHeader) {
        WebElement welfarePageHeader = driver.findElement(By.xpath(String.format("//a[normalize-space()='%s']", pageHeader)));
        Actions action = new Actions(driver);
        action.moveToElement(welfarePageHeader).perform();
    }

    public WebElement fullNameFieldLocator() {
        return driver.findElement(By.cssSelector("input[name='full_name']"));
    }

    public WebElement dateOfBirthFieldLocator() {
        return driver.findElement(By.cssSelector("input[name='date_of_birth']"));
    }

    public WebElement emailFieldLocator() {
        return driver.findElement(By.cssSelector("input[name='email']"));
    }

    public WebElement phoneFieldLocator() {
        return driver.findElement(By.cssSelector("input[name='phone']"));
    }

    public WebElement mobileFieldLocator() {
        return driver.findElement(By.cssSelector("input[name='mobile']"));
    }

    public WebElement cnicFieldLocator() {
        return driver.findElement(By.cssSelector("input[name='cnic']"));
    }

    public WebElement addressFieldLocator() {
        return driver.findElement(By.cssSelector("input[name='address']"));
    }

    public WebElement cityFieldLocator() {
        return driver.findElement(By.cssSelector("input[name='city']"));
    }

    public WebElement countryFieldLocator() {
        return driver.findElement(By.cssSelector("input[name='country']"));
    }

    public WebElement postalCodeFieldLocator() {
        return driver.findElement(By.cssSelector("input[name='postal_code']"));
    }


    public WebElement alternateFullNameFieldLocator() {
        return driver.findElement(By.cssSelector("input[name='alternative_name']"));
    }

    public WebElement relationFieldLocator() {
        return driver.findElement(By.cssSelector("input[name='relation']"));
    }

    public WebElement educationLocator() {
        return driver.findElement(By.cssSelector("input[name='education']"));
    }

    public WebElement alternatePhoneFieldLocator() {
        return driver.findElement(By.cssSelector("input[name='alternative_phone']"));
    }

    public WebElement alternateMobileFieldLocator() {
        return driver.findElement(By.cssSelector("input[name='alternative_mobile']"));
    }

    public WebElement alternateAddressFieldLocator() {
        return driver.findElement(By.cssSelector("input[name='alternative_address']"));
    }

    public WebElement alternateCityFieldLocator() {
        return driver.findElement(By.cssSelector("input[name='alternative_city']"));
    }

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

    public WebElement alternatePostalCodeFieldLocator() {
        return driver.findElement(By.cssSelector("input[name='alternative_postal_code']"));
    }

    // Occupation fields
    public WebElement occupationFieldLocator() {
        return driver.findElement(By.cssSelector("input[name='occupation']"));
    }

    public void typeOfOccupationField(String type) {
        Select occupationType = new Select(driver.findElement(By.cssSelector("select[name='employment']")));
        occupationType.selectByVisibleText(type);
    }

    public WebElement employmentName() {
        return driver.findElement(By.cssSelector("input[name='employment_name']"));
    }

    public WebElement areaOfInterest() {
        return driver.findElement(By.cssSelector("input[name='area_of_interest']"));
    }

    // Volunteer detail

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

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

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

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

    public WebElement languageProficiency() {
        return driver.findElement(By.xpath("//input[@id='txt_language']"));
    }

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

    public WebElement monthsAvailability(String month) {
        return driver.findElement(By.xpath(String.format("//input[@value='%s']", month)));
    }

    public WebElement daysAvailability(String day, String quarter) {
        return driver.findElement(By.cssSelector(String.format("input[value='%s'][name='availability_%s[]']", day, quarter)));
    }

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

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

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

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

    public String cnicNumber() {
        BigInteger maxLimit = new BigInteger("5000000000000");
        Random randNum = new Random();
        String len = String.valueOf(maxLimit.bitLength());
        String res = String.valueOf(new BigInteger(Integer.parseInt(len), randNum));
        return res;
    }

    public String mobileNumber() {
        BigInteger maxLimit = new BigInteger("500000000");
        Random randNum = new Random();
        int len = maxLimit.bitLength();
        String phone = "03" + new BigInteger(len, randNum);
        return phone;
    }

    public String phoneNumber() {
        BigInteger maxLimit = new BigInteger("50000000000");
        Random randNum = new Random();
        int len = maxLimit.bitLength();
        String phone = String.valueOf(new BigInteger(len, randNum));
        return phone;
    }
}
