package cz.moneta.test.dsl.brokerportal.contract.fullapplication;

import cz.moneta.test.dsl.brokerportal.enums.AddressType;
import cz.moneta.test.dsl.brokerportal.enums.Client;
import cz.moneta.test.dsl.brokerportal.enums.Household;
import cz.moneta.test.harness.context.StoreAccessor;
import cz.moneta.test.harness.endpoints.hypos.BrokerPortalEndpoint;
import cz.moneta.test.harness.support.web.*;

import static cz.moneta.test.dsl.brokerportal.contract.fullapplication.FullApplicationPage.GENERATE_FULL_APPLICATION_BUTTON;

@Wait(value = GENERATE_FULL_APPLICATION_BUTTON, waitSecondsForElement = 40)
public interface FullApplicationPage extends WebFlow<FullApplicationPage>, StoreAccessor {
    String SECOND_IDENTITY_TYPE_SELECT = "//div[@id='households[%2$s].persons[%1$s].identities[1].type']//input";
    String SECOND_IDENTITY_VALUE_INPUT = "//input[@name='households[%2$s].persons[%1$s].identities[1].value']";
    String SECOND_IDENTITY_EXPIRATION_DATE_INPUT = "//input[@name='households[%2$s].persons[%1$s].identities[1].expirationDate']";
    String EDUCATION_SELECT = "//div[@id='households[%2$s].persons[%1$s].education']//input";
    String CURRENT_LIVING_TYPE_SELECT = "//div[@id='households[%2$s].persons[%1$s].livingType']//input";
    String EMPLOYMENT_PEP_CHECKBOX = "//li[@id='households[%2$s].persons[%1$s].pepFlag_value-false']";
    String MAIN_CLIENT_RELATION_SELECT = "//div[@id='households[%2$s].persons[%1$s].mainClientRelation']//input";
    String ADDRESS_SAME_CHECKBOX = "//label[@id='households[%2$s].persons[%1$s].%3$sSame']//..";

    String EMPLOYER_SEARCH = "//div[@id='households[%2$s].persons[%1$s].employeeIncomes[0].employerNameSearch']";
    String EMPLOYER_SEARCH_INPUT = EMPLOYER_SEARCH + "/div";
    String EMPLOYER_SEARCH_RESULT = EMPLOYER_SEARCH + "//div[contains(text(),'%3$s')]/p";
    String EMPLOYER_SEARCH_MANUAL = EMPLOYER_SEARCH + "//div[text()='Zadat ručně']";
    String EMPLOYER_NAME = "//input[@name='households[%2$s].persons[%1$s].employeeIncomes[0].employerName']";
    String EMPLOYER_ICO = "//input[@name='households[%2$s].persons[%1$s].employeeIncomes[0].employerRegistrationNumber']";
    String EMPLOYER_ADDRESS_STREET = "//input[@name='households[%2$s].persons[%1$s].employeeIncomes[0].employeeAddress.street']";
    String EMPLOYER_ADDRESS_STREET_NUMBER = "//input[@name='households[%2$s].persons[%1$s].employeeIncomes[0].employeeAddress.streetNumber']";
    String EMPLOYER_ADDRESS_NUMBER_ORIENTATIONAL = "//input[@name='households[%2$s].persons[%1$s].employeeIncomes[0].employeeAddress.streetNumberOrientational']";
    String EMPLOYER_ADDRESS_ZIP_CODE = "//input[@name='households[%2$s].persons[%1$s].employeeIncomes[0].employeeAddress.zipCode']";
    String EMPLOYER_ADDRESS_CITY = "//input[@name='households[%2$s].persons[%1$s].employeeIncomes[0].employeeAddress.city']";
    String EMPLOYER_PHONE = "//input[@name='households[%2$s].persons[%1$s].employeeIncomes[0].employerPhone']";
    String EMPLOYMENT_TYPE_SELECT = "//div[@id='households[%2$s].persons[%1$s].employeeIncomes[0].employmentType']//input";
    String EMPLOYMENT_START_DATE_INPUT = "//input[@name='households[%2$s].persons[%1$s].employeeIncomes[0].employmentStart']";
    String EMPLOYMENT_FINISH_DATE_INPUT = "//input[@name='households[%2$s].persons[%1$s].employeeIncomes[0].employmentFinish']";
    String PROFESSION_INPUT = "//input[@name='households[%2$s].persons[%1$s].employeeIncomes[0].profession']";
    String EMPLOYMENT_IS_FIRT_CHECKBOX = "//li[@id='households[%2$s].persons[%1$s].employeeIncomes[0].isFirst_value-false']";
    String EMPLOYMENT_SPECIAL_RELATION_CHECKBOX = "//li[@id='households[%2$s].persons[%1$s].employeeIncomes[0].specialRelation_value-false']";
    String EMPLOYMENT_ABROAD_CHECKBOX = "//li[@id='households[%2$s].persons[%1$s].employeeIncomes[0].employeeAbroad_value-true']";
    String EMPLOYMENT_AGENCY_CHECKBOX = "//li[@id='households[%2$s].persons[%1$s].employeeIncomes[0].employmentAgency_value-false']";
    String EMPLOYMENT_CASH_SALARY_CHECKBOX = "//li[@id='households[%2$s].persons[%1$s].employeeIncomes[0].cashSalary_value-true']";
    String EMPLOYMENT_PAID_TO_MMB_CHECKBOX = "//li[@id='households[%2$s].persons[%1$s].employeeIncomes[0].paidToMmb_value-false']";

    String DRAWING_ACCOUNT_SELECT = "//div[@id='drawingAccount']//input";
    String NOTIFICATION_SMS_CHECKBOX = "//label[@id='notificationIfPaymentChangedSms']";
    String NOTIFICATION_EMAIL_CHECKBOX = "//label[@id='notificationIfPaymentChangedEmail']";
    String REALTY_PURCHASE_REASON_SELECT = "//div[@id='realtyPurchaseReason']//input";
    String LOAN_OBJECT_SELECT = "//div[@id='loanObject']//input";

    String GENERATE_FULL_APPLICATION_BUTTON = "//button[@id='fullApplicationGenerate']";

    @CustomAction
    default FullApplicationPage selectSecondIdentityType(String identityCardType, Client client, Household household) {
        BrokerPortalEndpoint endpoint = getEndpoint(BrokerPortalEndpoint.class);
        endpoint.type(() -> String.format(SECOND_IDENTITY_TYPE_SELECT, client.getInHouseholdValue(), household.getValue()), identityCardType, false);
        endpoint.sendKeysOneAtATime(Key.ENTER);
        return null;
    }

    @CustomAction
    default FullApplicationPage fillSecondIdentityValue(String identityCardId, Client client, Household household) {
        BrokerPortalEndpoint endpoint = getEndpoint(BrokerPortalEndpoint.class);
        endpoint.type(() -> String.format(SECOND_IDENTITY_VALUE_INPUT, client.getInHouseholdValue(), household.getValue()), identityCardId, false);
        return null;
    }

    @CustomAction
    default FullApplicationPage fillSecondIdentityExpirationDate(String date, Client client, Household household) {
        BrokerPortalEndpoint endpoint = getEndpoint(BrokerPortalEndpoint.class);
        endpoint.type(() -> String.format(SECOND_IDENTITY_EXPIRATION_DATE_INPUT, client.getInHouseholdValue(), household.getValue()), date, false);
        return null;
    }

    @CustomAction
    default FullApplicationPage selectEducation(String education, Client client, Household household) {
        BrokerPortalEndpoint endpoint = getEndpoint(BrokerPortalEndpoint.class);
        endpoint.type(() -> String.format(EDUCATION_SELECT, client.getInHouseholdValue(), household.getValue()), education, false);
        endpoint.sendKeysOneAtATime(Key.ENTER);
        return null;
    }

    @CustomAction
    default FullApplicationPage selectCurrentLivingType(String currentLivingType, Client client, Household household) {
        BrokerPortalEndpoint endpoint = getEndpoint(BrokerPortalEndpoint.class);
        endpoint.type(() -> String.format(CURRENT_LIVING_TYPE_SELECT, client.getInHouseholdValue(), household.getValue()), currentLivingType, false);
        endpoint.sendKeysOneAtATime(Key.ENTER);
        return null;
    }

    @CustomAction
    default FullApplicationPage clickPep(Client client, Household household) {
        BrokerPortalEndpoint endpoint = getEndpoint(BrokerPortalEndpoint.class);
        endpoint.jsClick(() -> String.format(EMPLOYMENT_PEP_CHECKBOX, client.getInHouseholdValue(), household.getValue()));
        return null;
    }

    @CustomAction
    default FullApplicationPage selectMainClientRelation(String mainClientRelation, Client client, Household household) {
        BrokerPortalEndpoint endpoint = getEndpoint(BrokerPortalEndpoint.class);
        endpoint.type(() -> String.format(MAIN_CLIENT_RELATION_SELECT, client.getInHouseholdValue(), household.getValue()), mainClientRelation, false);
        endpoint.sendKeysOneAtATime(Key.ENTER);
        return null;
    }

    @CustomAction
    default FullApplicationPage clickAddressSameCheckbox(Client client, Household household, AddressType addressType) {
        BrokerPortalEndpoint endpoint = getEndpoint(BrokerPortalEndpoint.class);
        endpoint.click(() -> String.format(ADDRESS_SAME_CHECKBOX, client.getInHouseholdValue(), household.getValue(), addressType.getValue()));
        return null;
    }

    @CustomAction
    default FullApplicationPage searchEmployer(String search, Client client, Household household) {
        BrokerPortalEndpoint endpoint = getEndpoint(BrokerPortalEndpoint.class);
        String employerSearchResult = String.format(EMPLOYER_SEARCH_RESULT, client.getInHouseholdValue(), household.getValue(), search);
        endpoint.type(() -> String.format(EMPLOYER_SEARCH_INPUT, client.getInHouseholdValue(), household.getValue()), search, false);
        endpoint.waitForElementsToLoad(10, employerSearchResult);
        endpoint.click(() -> employerSearchResult);
        return null;
    }

    @CustomAction
    default FullApplicationPage searchEmployerManual(Client client, Household household) {
        BrokerPortalEndpoint endpoint = getEndpoint(BrokerPortalEndpoint.class);
        endpoint.click(() -> String.format(EMPLOYER_SEARCH_INPUT, client.getInHouseholdValue(), household.getValue()));
        endpoint.click(() -> String.format(EMPLOYER_SEARCH_MANUAL, client.getInHouseholdValue(), household.getValue()));
        return null;
    }

    @CustomAction
    default FullApplicationPage selectEmploymentType(String employmentType, Client client, Household household) {
        BrokerPortalEndpoint endpoint = getEndpoint(BrokerPortalEndpoint.class);
        endpoint.type(() -> String.format(EMPLOYMENT_TYPE_SELECT, client.getInHouseholdValue(), household.getValue()), employmentType, false);
        endpoint.sendKeysOneAtATime(Key.ENTER);
        return null;
    }

    @CustomAction
    default FullApplicationPage fillEmployerName(String employerName, Client client, Household household) {
        BrokerPortalEndpoint endpoint = getEndpoint(BrokerPortalEndpoint.class);
        endpoint.type(() -> String.format(EMPLOYER_NAME, client.getInHouseholdValue(), household.getValue()), employerName, false);
        return null;
    }

    @CustomAction
    default FullApplicationPage fillEmployerIco(String employerIco, Client client, Household household) {
        BrokerPortalEndpoint endpoint = getEndpoint(BrokerPortalEndpoint.class);
        endpoint.type(() -> String.format(EMPLOYER_ICO, client.getInHouseholdValue(), household.getValue()), employerIco, false);
        return null;
    }

    @CustomAction
    default FullApplicationPage fillEmployerStreet(String employerStreet, Client client, Household household) {
        BrokerPortalEndpoint endpoint = getEndpoint(BrokerPortalEndpoint.class);
        endpoint.type(() -> String.format(EMPLOYER_ADDRESS_STREET, client.getInHouseholdValue(), household.getValue()), employerStreet, false);
        return null;
    }

    @CustomAction
    default FullApplicationPage fillEmployerStreetNumber(String employerStreetNumber, Client client, Household household) {
        BrokerPortalEndpoint endpoint = getEndpoint(BrokerPortalEndpoint.class);
        endpoint.type(() -> String.format(EMPLOYER_ADDRESS_STREET_NUMBER, client.getInHouseholdValue(), household.getValue()), employerStreetNumber, false);
        return null;
    }

    @CustomAction
    default FullApplicationPage fillEmployerNumberOrientational(String employerNumberOrientational, Client client, Household household) {
        BrokerPortalEndpoint endpoint = getEndpoint(BrokerPortalEndpoint.class);
        endpoint.type(() -> String.format(EMPLOYER_ADDRESS_NUMBER_ORIENTATIONAL, client.getInHouseholdValue(), household.getValue()), employerNumberOrientational, false);
        return null;
    }

    @CustomAction
    default FullApplicationPage fillEmployerZipCode(String employerZipCode, Client client, Household household) {
        BrokerPortalEndpoint endpoint = getEndpoint(BrokerPortalEndpoint.class);
        endpoint.type(() -> String.format(EMPLOYER_ADDRESS_ZIP_CODE, client.getInHouseholdValue(), household.getValue()), employerZipCode, false);
        return null;
    }

    @CustomAction
    default FullApplicationPage fillEmployerCity(String employerCity, Client client, Household household) {
        BrokerPortalEndpoint endpoint = getEndpoint(BrokerPortalEndpoint.class);
        endpoint.type(() -> String.format(EMPLOYER_ADDRESS_CITY, client.getInHouseholdValue(), household.getValue()), employerCity, false);
        return null;
    }

    @CustomAction
    default FullApplicationPage fillEmployerPhone(String employerPhone, Client client, Household household) {
        BrokerPortalEndpoint endpoint = getEndpoint(BrokerPortalEndpoint.class);
        endpoint.type(() -> String.format(EMPLOYER_PHONE, client.getInHouseholdValue(), household.getValue()), employerPhone, false);
        return null;
    }

    @CustomAction
    default FullApplicationPage fillEmploymentStartDate(String date, Client client, Household household) {
        BrokerPortalEndpoint endpoint = getEndpoint(BrokerPortalEndpoint.class);
        endpoint.type(() -> String.format(EMPLOYMENT_START_DATE_INPUT, client.getInHouseholdValue(), household.getValue()), date, false);
        return null;
    }

    @CustomAction
    default FullApplicationPage fillEmploymentFinishDate(String date, Client client, Household household) {
        BrokerPortalEndpoint endpoint = getEndpoint(BrokerPortalEndpoint.class);
        endpoint.type(() -> String.format(EMPLOYMENT_FINISH_DATE_INPUT, client.getInHouseholdValue(), household.getValue()), date, false);
        return null;
    }

    @CustomAction
    default FullApplicationPage clickEmploymentIsFirst(Client client, Household household) {
        BrokerPortalEndpoint endpoint = getEndpoint(BrokerPortalEndpoint.class);
        endpoint.jsClick(() -> String.format(EMPLOYMENT_IS_FIRT_CHECKBOX, client.getInHouseholdValue(), household.getValue()));
        return null;
    }

    @CustomAction
    default FullApplicationPage clickEmploymentSpecialRelation(Client client, Household household) {
        BrokerPortalEndpoint endpoint = getEndpoint(BrokerPortalEndpoint.class);
        endpoint.jsClick(() -> String.format(EMPLOYMENT_SPECIAL_RELATION_CHECKBOX, client.getInHouseholdValue(), household.getValue()));
        return null;
    }

    @CustomAction
    default FullApplicationPage fillProfession(String profession, Client client, Household household) {
        BrokerPortalEndpoint endpoint = getEndpoint(BrokerPortalEndpoint.class);
        endpoint.type(() -> String.format(PROFESSION_INPUT, client.getInHouseholdValue(), household.getValue()), profession, false);
        return null;
    }

    @CustomAction
    default FullApplicationPage clickEmploymentAbroad(Client client, Household household) {
        BrokerPortalEndpoint endpoint = getEndpoint(BrokerPortalEndpoint.class);
        endpoint.jsClick(() -> String.format(EMPLOYMENT_ABROAD_CHECKBOX, client.getInHouseholdValue(), household.getValue()));
        return null;
    }

    @CustomAction
    default FullApplicationPage clickEmploymentCashSalary(Client client, Household household) {
        BrokerPortalEndpoint endpoint = getEndpoint(BrokerPortalEndpoint.class);
        endpoint.jsClick(() -> String.format(EMPLOYMENT_CASH_SALARY_CHECKBOX, client.getInHouseholdValue(), household.getValue()));
        return null;
    }

    @CustomAction
    default FullApplicationPage clickEmploymentAgency(Client client, Household household) {
        BrokerPortalEndpoint endpoint = getEndpoint(BrokerPortalEndpoint.class);
        endpoint.jsClick(() -> String.format(EMPLOYMENT_AGENCY_CHECKBOX, client.getInHouseholdValue(), household.getValue()));
        return null;
    }

    @CustomAction
    default FullApplicationPage clickEmploymentPaidToMmb(Client client, Household household) {
        BrokerPortalEndpoint endpoint = getEndpoint(BrokerPortalEndpoint.class);
        endpoint.jsClick(() -> String.format(EMPLOYMENT_PAID_TO_MMB_CHECKBOX, client.getInHouseholdValue(), household.getValue()));
        return null;
    }

    @TypeInto(DRAWING_ACCOUNT_SELECT)
    @KeyPress(Key.ENTER)
    FullApplicationPage selectDrawingAccount(String drawingAccount);

    @Click(NOTIFICATION_SMS_CHECKBOX)
    FullApplicationPage clickSmsNotifyWhenPaymentChangesCheckbox();

    @Click(NOTIFICATION_EMAIL_CHECKBOX)
    FullApplicationPage clickEmailNotifyWhenPaymentChangesCheckbox();

    @TypeInto(REALTY_PURCHASE_REASON_SELECT)
    @KeyPress(Key.ENTER)
    FullApplicationPage selectRealtyPurchaseReason(String purchaseReason);

    @TypeInto(LOAN_OBJECT_SELECT)
    @KeyPress(Key.ENTER)
    FullApplicationPage selectLoanObject(String loanObject);

    @Click(GENERATE_FULL_APPLICATION_BUTTON)
    FullApplicationSignPage clickGenerateFullApplication();
}