package cz.moneta.test.dsl.ib.loans.flexicredit.request;

import cz.moneta.test.dsl.ib.IbPageFlow;
import cz.moneta.test.dsl.ib.loans.flexicredit.FlexicreditApplicationPage;
import cz.moneta.test.harness.context.StoreAccessor;
import cz.moneta.test.harness.endpoints.ib.IbEndpoint;
import cz.moneta.test.harness.support.web.Click;
import cz.moneta.test.harness.support.web.CustomAction;
import cz.moneta.test.harness.support.web.TypeInto;
import cz.moneta.test.harness.support.web.Wait;

import static cz.moneta.test.dsl.ib.loans.flexicredit.request.RequestRevenuesFlexicreditApplicationPage.COUNTRY;
import static cz.moneta.test.dsl.ib.loans.flexicredit.request.RequestRevenuesFlexicreditApplicationPage.DEDUCTIONS_FROM_SALARY;
import static cz.moneta.test.dsl.ib.loans.flexicredit.request.RequestRevenuesFlexicreditApplicationPage.DESCRIPTIVE_NUMBER;
import static cz.moneta.test.dsl.ib.loans.flexicredit.request.RequestRevenuesFlexicreditApplicationPage.EMPLOYER_ID;
import static cz.moneta.test.dsl.ib.loans.flexicredit.request.RequestRevenuesFlexicreditApplicationPage.EMPLOYMENT_TYPE;
import static cz.moneta.test.dsl.ib.loans.flexicredit.request.RequestRevenuesFlexicreditApplicationPage.IN_TRIAL_PERIOD;
import static cz.moneta.test.dsl.ib.loans.flexicredit.request.RequestRevenuesFlexicreditApplicationPage.MONTHLY_HOUSE_INCOME;
import static cz.moneta.test.dsl.ib.loans.flexicredit.request.RequestRevenuesFlexicreditApplicationPage.MONTHLY_INCOME;
import static cz.moneta.test.dsl.ib.loans.flexicredit.request.RequestRevenuesFlexicreditApplicationPage.NAME_OF_EMPLOYEE;
import static cz.moneta.test.dsl.ib.loans.flexicredit.request.RequestRevenuesFlexicreditApplicationPage.NUMBER_OF_DEPENDENTS;
import static cz.moneta.test.dsl.ib.loans.flexicredit.request.RequestRevenuesFlexicreditApplicationPage.ON_NOTICE;
import static cz.moneta.test.dsl.ib.loans.flexicredit.request.RequestRevenuesFlexicreditApplicationPage.ORIENTATION_NUMBER;
import static cz.moneta.test.dsl.ib.loans.flexicredit.request.RequestRevenuesFlexicreditApplicationPage.OTHER_LIVING_COSTS;
import static cz.moneta.test.dsl.ib.loans.flexicredit.request.RequestRevenuesFlexicreditApplicationPage.OTHER_MONTHLY_INSTALLMENTS;
import static cz.moneta.test.dsl.ib.loans.flexicredit.request.RequestRevenuesFlexicreditApplicationPage.REVENUES_HEADER_TEXT;
import static cz.moneta.test.dsl.ib.loans.flexicredit.request.RequestRevenuesFlexicreditApplicationPage.STREET;
import static cz.moneta.test.dsl.ib.loans.flexicredit.request.RequestRevenuesFlexicreditApplicationPage.TOWN;
import static cz.moneta.test.dsl.ib.loans.flexicredit.request.RequestRevenuesFlexicreditApplicationPage.ZIP;


@Wait({REVENUES_HEADER_TEXT, EMPLOYMENT_TYPE, IN_TRIAL_PERIOD, ON_NOTICE,
        NAME_OF_EMPLOYEE, EMPLOYER_ID, STREET, DESCRIPTIVE_NUMBER, ORIENTATION_NUMBER, TOWN, ZIP, COUNTRY,
        MONTHLY_INCOME, MONTHLY_HOUSE_INCOME, OTHER_MONTHLY_INSTALLMENTS, OTHER_LIVING_COSTS, DEDUCTIONS_FROM_SALARY, NUMBER_OF_DEPENDENTS})
public interface RequestRevenuesFlexicreditApplicationPage extends IbPageFlow<RequestRevenuesFlexicreditApplicationPage>, FlexicreditApplicationPage, StoreAccessor {
    String REVENUES_HEADER_TEXT = "//span[text() = 'Máme správně údaje o Vašich příjmech?']";

    //Employment
    String EMPLOYMENT_TYPE = "//span[text()='Pracovní poměr']//parent::label//following::input[@name= 'incomeSource.employmentType']";
    String IN_TRIAL_PERIOD = "//span[text()='Jsem ve zkušební lhůtě']//ancestor::label//input[@id = 'incomeSource.inTrialPeriod']";
    String ON_NOTICE = "//span[text()='Jsem ve výpovědní lhůtě']//ancestor::label//input[@id = 'incomeSource.onNotice']";

    //Employer
    String NAME_OF_EMPLOYEE = "//span[text()='Název zaměstnavatele']//parent::label//following::div[@id = 'react-autowhatever-1']//preceding-sibling::input";
    String EMPLOYER_ID = "//span[text()='IČ']//parent::label//following::input[@name= 'employer.employerId']";
    String STREET = "//span[text()='Ulice']//parent::label//following::input[@name= 'employer.employerAddress.street']";
    String DESCRIPTIVE_NUMBER = "//span[text()='Č. popisné / orientační']//parent::label//following::input[@name= 'employer.employerAddress.descriptiveNumber']";
    String ORIENTATION_NUMBER = "//input[@name= 'employer.employerAddress.orientationNumber']";
    String TOWN = "//span[text()='Obec / Město']//parent::label//following::input[@name= 'employer.employerAddress.town']";
    String ZIP = "//span[text()='PSČ']//parent::label//following::input[@name= 'employer.employerAddress.zip']";
    String COUNTRY = "//span[text()='Země']//parent::label//following::input[@name= 'employer.employerAddress.country']"; //TODO test to click on element

    //Revenues and expenses
    String MONTHLY_INCOME = "//span[text()='Čistý měsíční příjem']//parent::label//following::input[@name= 'solvency.netMonthlyIncome']";
    String MONTHLY_HOUSE_INCOME = "//span[text()='Čistý měsíční příjem domácnosti']//parent::label//following::input[@name= 'householdSolvency.netMonthlyHouseIncome']";
    String OTHER_MONTHLY_INSTALLMENTS = "//span[text()='Měsíční splátky mimo MMB']//parent::label//following::input[@name= 'solvency.otherMonthlyInstallments']";
    String OTHER_LIVING_COSTS = "//span[text()='Ostatní nestandardní měsíční výdaje']//parent::label//following::input[@name= 'solvency.otherLivingCosts']";
    String DEDUCTIONS_FROM_SALARY = "//span[text()='Srážky ze mzdy']//parent::label//following::input[@name= 'solvency.deductionsFromWages']";

    String RESIDENCE_FLAG_TRUE = "//span[text()='Daňový rezident ČR']//parent::label//following::input[@name= 'solvency.residenceFlag' and @value = '%s']";
    String NUMBER_OF_DEPENDENTS = "//span[text()='Počet vyživovaných osob']//parent::label//following::input[@id= 'react-select-6-input']";

    @TypeInto(value = MONTHLY_INCOME, clear = true)
    RequestRevenuesFlexicreditApplicationPage typeMonthlyIncome(String monthlyIncome);

    @TypeInto(value = MONTHLY_HOUSE_INCOME, clear = true)
    RequestRevenuesFlexicreditApplicationPage typeMonthlyHouseIncome(String monthlyHouseIncome);

    @TypeInto(value = OTHER_MONTHLY_INSTALLMENTS, clear = true)
    RequestRevenuesFlexicreditApplicationPage typeOtherMonthlyInstallments(String typeOtherMonthlyInstallments);

    @TypeInto(value = OTHER_LIVING_COSTS, clear = true)
    RequestRevenuesFlexicreditApplicationPage typeOtherLivingCosts(String otherLivingCosts);

    @TypeInto(value = DEDUCTIONS_FROM_SALARY, clear = true)
    RequestRevenuesFlexicreditApplicationPage typeDeductionsFromSalary(String deductionsFromSalary);

    @CustomAction
    default RequestRevenuesFlexicreditApplicationPage setResidenceFlag(boolean residenceFlag) {
        IbEndpoint ibEndpoint = getEndpoint(IbEndpoint.class);
        ibEndpoint.jsClick(() -> String.format(RESIDENCE_FLAG_TRUE, residenceFlag ? "true" : "else"));
        return null;
    }

    @Wait(CONTINUE_BUTTON)
    @Click(value = CONTINUE_BUTTON, jsClick = true)
    RequestConfirmationFlexicreditApplicationPage clickContinueButton();
}
