package com.example.stepdefinations.pakistan;

import com.example.pageobjects.pakistan.BayanatPage;
import com.example.utils.TextContextSetup;
import io.cucumber.datatable.DataTable;
import io.cucumber.java.en.When;
import org.testng.Assert;

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

public class BayanatsPageSteps {
    TextContextSetup textContextSetup;

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


    @When("I fill the Become a Volunteer form with the following data:")
    public void iFillFromOfBecomeAVolunteerForm(DataTable formData) {
        BayanatPage bayanatPage = textContextSetup.pagesObject.getBayanatPage();

        List<Map<String, String>> data = formData.asMaps(String.class, String.class);
        for (Map<String, String> itemData : data) {
            if (itemData.containsKey("firstName")) {
                bayanatPage.firstNameFieldLocator().clear();
                bayanatPage.firstNameFieldLocator().sendKeys(itemData.get("firstName"));
            }
            if (itemData.containsKey("lastName")) {
                bayanatPage.lastNameFieldLocator().click();
                bayanatPage.lastNameFieldLocator().sendKeys(itemData.get("lastName"));
            }
            if (itemData.containsKey("email")) {
                bayanatPage.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];
                    bayanatPage.emailFieldLocator().sendKeys(emailAddress);
                }
            }
            if (itemData.containsKey("phoneNumber")) {
                bayanatPage.phoneFieldLocator().clear();
                bayanatPage.phoneFieldLocator().sendKeys(itemData.get("phoneNumber"));
            }
            if (itemData.containsKey("address")) {
                bayanatPage.addressFieldLocator().clear();
                bayanatPage.addressFieldLocator().sendKeys(itemData.get("address"));
            }
            if (itemData.containsKey("city")) {
                bayanatPage.cityFieldLocator().clear();
                bayanatPage.cityFieldLocator().sendKeys(itemData.get("city"));
            }
            if (itemData.containsKey("country")) {
                bayanatPage.countryFieldLocator().clear();
                bayanatPage.countryFieldLocator().sendKeys(itemData.get("country"));
            }
            if (itemData.containsKey("skillsOrQualification")) {
                bayanatPage.skillsFieldLocator().clear();
                bayanatPage.skillsFieldLocator().sendKeys(itemData.get("skillsOrQualification"));
            }
            if (itemData.containsKey("volunteeringExperience")) {
                bayanatPage.volunteerExperienceFieldLocator().clear();
                bayanatPage.volunteerExperienceFieldLocator().sendKeys(itemData.get("volunteeringExperience"));
            }
        }
    }

    @When("^I Should see success message on become a volunteer page$")
    public void iFillFromOfBecomeAVolunteerForm() {
        BayanatPage bayanatPage = textContextSetup.pagesObject.getBayanatPage();
        String successMessage = bayanatPage.successAlertMessageLocator().getText();
        System.out.println(successMessage);
        Assert.assertEquals(successMessage, "×\nAlert!\nThank you for applying as a volunteer");
    }
}
