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

import cz.moneta.test.dsl.Harness;
import cz.moneta.test.dsl.brokerportal.contractdetail.ContractDetailPage;
import cz.moneta.test.dsl.brokerportal.enums.Client;
import cz.moneta.test.dsl.brokerportal.contract.shortapplication.ShortApplicationPage;
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.CalculatorPage.ADD_APPLICANT_BUTTON;

@Wait(ADD_APPLICANT_BUTTON)
public interface CalculatorPage extends WebFlow<CalculatorPage>, StoreAccessor {
    //CLIENTS
    String ADD_APPLICANT_BUTTON = "//button[@id='addApplicant']";
    String HOUSEHOLD_CHECKBOX = "//input[@name='persons[%s].sameHousehold']//..";
    String CLIENT_TAB = "//a[@id='persons[%s]']";

    //Basic info
    String SEX_SELECT = "//div[@id='persons[%s].sex']//input";
    String FIRST_NAME_INPUT = "//input[@name='persons[%s].firstName']";
    String LAST_NAME_INPUT = "//input[@name='persons[%s].lastName']";
    String BIRTH_DATE_INPUT = "//input[@name='persons[%s].birthDate']";
    String CONTACT_COUNTRY = "//div[@id='persons[%s].contactCountryId']//input";
    String NATIONALITY = "//div[@id='persons[%s].nationalityId']//input";
    String MARITAL_STATUS_SELECT = "//div[@id='persons[%s].maritalStatus']//input";
    String MATE_CHECKBOX = "//label[@id='persons[%s].partner']";

    //Income Employment
    String GROSS_INCOME_INPUT = "//input[@name='persons[%s].employeeIncomes[0].grossIncome']";
    String NET_INCOME_INPUT = "//input[@name='persons[%s].employeeIncomes[0].netIncome']";

    //Income Business
    String BUSINESS_INCOME_BUTTON = "//h2/button//span[text()='Z podnikání']";
    String TURNOVER_LATEST_INPUT = "//input[@name='persons[%s].turnoverLatest']";
    String TAX_BASE_LATEST_INPUT = "//input[@name='persons[%s].taxBaseLatest']";
    String TAX_AMOUNT_LATEST_INPUT = "//input[@name='persons[%s].taxAmountLatest']";
    String COST_ACCOUNTING_TYPE_SELECT = "//div[@id='persons[%s].costAccountingType']//input";
    String COST_PERCENTAGE_RATE_SELECT = "//div[@id='persons[%s].costPercentageRate']//input";
    String NACE_CODE_SELECT = "//div[@id='persons[%s].naceCode']//input";
    String TURNOVER_PENULTIMATE_INPUT = "//input[@name='persons[%s].turnoverPenultimate']";
    String TAX_BASE_PENULTIMATE_INPUT = "//input[@name='persons[%s].taxBasePenultimate']";
    String DAP_CHECKBOX = "//label[@id='persons[%1$s].dapYes']";
    String COST_YEAR_LATEST_SELECT = "//div[@id='persons[%s].yearLatest']//input";
    String COST_YEAR_PENULTIMATE_SELECT = "//div[@id='persons[%s].yearPenultimate']//input";

    //Income Other
    String OTHER_INCOME_BUTTON = "//h2/button//span[text()='Ostatní']";
    String ADD_INCOME_BUTTON = "//button[@id='persons[%s].addIncome']";
    String OTHER_INCOME_TYPE_SELECT = "//div[@id='persons[%1$s].otherIncomes[%2$s].type']//input";
    String OTHER_INCOME_AMOUNT_INPUT = "//input[@name='persons[%1$s].otherIncomes[%2$s].amount']";

    //Expenditures
    String ADD_EXPENDITURE_BUTTON = "//button[@id='persons[%s].addExpenditure']";
    String EXPENDITURE_TYPE_SELECT = "//div[@id='persons[%1$s].expenditures[%2$s].type']//input";
    String EXPENDITURE_INSTALMENT_AMOUNT_INPUT = "//input[@name='persons[%1$s].expenditures[%2$s].instalmentAmount']";
    String EXPENDITURE_CREDIT_RESTRICTION_INPUT = "//input[@name='persons[%1$s].expenditures[%2$s].creditRestriction']";
    String EXPENDITURE_DEBT_INPUT = "//input[@name='persons[%1$s].expenditures[%2$s].debt']";
    String EXPENDITURE_CORRECTION_SELECT = "//div[@id='persons[%1$s].expenditures[%2$s].correction']//input";

    //LOAN
    //Parameters
    String PRODUCT_SELECT = "//div[@id='product']//input";
    String LOAN_AMOUNT_INPUT = "//input[@name='loanAmount']";
    String MATURITY_INPUT = "//input[@name='maturityYears']";
    String FIXATION_SELECT = "//div[@id='fixation']//input";
    String LOAN_PURPOSE_SELECT = "//div[@id='purposes']//input";
    String SERVICES_SELECT = "//div[@id='services']//input";

    //Insurance
    String INSURANCE_TYPE_SELECT = "//div[@id='insuranceType']//input";
    String INSURANCE_CHECKBOX = "//label[@id='insuredApplicants[%s].selected']";

    //Loan owner
    String LOAN_OWNER_CHECKBOX = "//li[@id='isLoanOwner_value-false']";

    //Loan security
    String LOAN_SECURITY_ESTATE_TYPE_SELECT = "//div[@id='loanSecurity[%s].estateType']//input";
    String LOAN_SECURITY_VALUE_INPUT = "//input[@name='loanSecurity[%s].value']";
    String LOAN_SECURITY_CONTRACT_RELATION_SELECT = "//div[@id='loanSecurity[%s].contractRelation']//input";
    String ADD_PROPERTY_BUTTON = "//button[@id='addProperty']";

    //Buttons
    String BACK_TO_CONTRACT_DETAIL_BUTTON = "//button[@id='backToBusinessDetailResult']";
    String CALCULATE_BUTTON = "//button[@id='calculate']";
    String SAVE_CALCULATION_BUTTON = "//button[@id='saveCalculation']";
    String TO_SHORT_APPLICATION_BUTTON = "//button[@id='toCalculatorApplication']";

    //CONTROL
    String CALCULATION_ALERT_SUCCESS = "//div[@role='alert' and contains(@class, 'success')]";
    String CONTRACT_ID = "//div[@id='result-anchor-id']/..//p[2]";

    @Click(value = ADD_APPLICANT_BUTTON, jsClick = true)
    CalculatorPage clickAddApplicantButton();

    @Click(value = HOUSEHOLD_CHECKBOX, jsClick = true)
    CalculatorPage clickHouseholdCheckbox(Client client);

    @Click(CLIENT_TAB)
    CalculatorPage clickClientTab(Client client);

    @CustomAction
    default CalculatorPage selectSex(String sex, Client client) {
        BrokerPortalEndpoint endpoint = getEndpoint(BrokerPortalEndpoint.class);
        endpoint.type(() -> String.format(SEX_SELECT, client.getValue()), sex, false);
        endpoint.sendKeysOneAtATime(Key.ENTER);
        return null;
    }

    @TypeInto(value = FIRST_NAME_INPUT, isStringDynamicXpath = true)
    CalculatorPage fillFirstName(String client, String firstName);

    @TypeInto(value = LAST_NAME_INPUT, isStringDynamicXpath = true)
    CalculatorPage fillLastName(String client, String lastName);

    @TypeInto(value = BIRTH_DATE_INPUT, isStringDynamicXpath = true)
    CalculatorPage fillBirthDate(String client, String date);

    @TypeInto(value = CONTACT_COUNTRY, isStringDynamicXpath = true)
    @KeyPress(Key.ENTER)
    CalculatorPage fillContactCountry(String client, String contactCountry);

    @TypeInto(value = NATIONALITY, isStringDynamicXpath = true)
    @KeyPress(Key.ENTER)
    CalculatorPage fillNationality(String client, String nationality);

    @TypeInto(value = MARITAL_STATUS_SELECT, isStringDynamicXpath = true)
    @KeyPress(Key.ENTER)
    CalculatorPage selectMaritalStatus(String client, String maritalStatus);

    @Click(MATE_CHECKBOX)
    CalculatorPage clickMateCheckbox(Client client);

    @TypeInto(value = GROSS_INCOME_INPUT, isStringDynamicXpath = true)
    CalculatorPage fillGrossIncome(String client, String grossIncome);

    @TypeInto(value = NET_INCOME_INPUT, isStringDynamicXpath = true)
    CalculatorPage fillNetIncome(String client, String netIncome);

    @Click(BUSINESS_INCOME_BUTTON)
    CalculatorPage clickBusinessIncome();

    @TypeInto(value = COST_YEAR_LATEST_SELECT, isStringDynamicXpath = true)
    @KeyPress(Key.ENTER)
    CalculatorPage selectYearLatest(String client, String yearLatest);

    @TypeInto(value = TURNOVER_LATEST_INPUT, isStringDynamicXpath = true)
    CalculatorPage fillTurnoverLatest(String client, String turnOverLatest);

    @TypeInto(value = TAX_BASE_LATEST_INPUT, isStringDynamicXpath = true)
    CalculatorPage fillTaxBaseLatest(String client, String taxBaseLatest);

    @TypeInto(value = TAX_AMOUNT_LATEST_INPUT, isStringDynamicXpath = true)
    CalculatorPage fillTaxAmountLatest(String client, String taxAmountLatest);

    @TypeInto(value = COST_ACCOUNTING_TYPE_SELECT, isStringDynamicXpath = true)
    @KeyPress(Key.ENTER)
    CalculatorPage selectCostAccountingType(String client, String costAccountingType);

    @TypeInto(value = COST_PERCENTAGE_RATE_SELECT, isStringDynamicXpath = true)
    @KeyPress(Key.ENTER)
    CalculatorPage selectCostPercentageRate(String client, String costPercentageRate);

    @TypeInto(value = NACE_CODE_SELECT, isStringDynamicXpath = true)
    @KeyPress(Key.ENTER)
    CalculatorPage selectNaceCode(String client, String naceCode);

    @TypeInto(value = COST_YEAR_PENULTIMATE_SELECT, isStringDynamicXpath = true)
    @KeyPress(Key.ENTER)
    CalculatorPage selectYearPenultimate(String client, String yearPenultimate);

    @TypeInto(value = TURNOVER_PENULTIMATE_INPUT, isStringDynamicXpath = true)
    CalculatorPage fillTurnoverPenultimate(String client, String turnOverPenultimate);

    @TypeInto(value = TAX_BASE_PENULTIMATE_INPUT, isStringDynamicXpath = true)
    CalculatorPage fillTaxBasePenultimate(String client, String taxBasePenultimate);

    @Click(DAP_CHECKBOX)
    CalculatorPage clickDapCheckbox(Client client);

    @Click(OTHER_INCOME_BUTTON)
    CalculatorPage clickOtherIncome();

    @CustomAction
    default CalculatorPage selectOtherIncome(String client, String row, String otherIncomeType) {
        BrokerPortalEndpoint endpoint = getEndpoint(BrokerPortalEndpoint.class);
        endpoint.type(() -> String.format(OTHER_INCOME_TYPE_SELECT, client, row), otherIncomeType, false);
        endpoint.sendKeysOneAtATime(Key.ENTER);
        return null;
    }

    @CustomAction
    default CalculatorPage fillOtherIncomeAmount(String client, String row, String otherIncomeAmount) {
        BrokerPortalEndpoint endpoint = getEndpoint(BrokerPortalEndpoint.class);
        endpoint.type(() -> String.format(OTHER_INCOME_AMOUNT_INPUT, client, row), otherIncomeAmount, false);
        return null;
    }

    @Click(ADD_INCOME_BUTTON)
    CalculatorPage clickAddIncomeButton(Client client);

    @Click(ADD_EXPENDITURE_BUTTON)
    CalculatorPage clickAddExpenditureButton(Client client);

    @CustomAction
    default CalculatorPage selectExpenditureType(String client, String row, String expenditureType) {
        BrokerPortalEndpoint endpoint = getEndpoint(BrokerPortalEndpoint.class);
        endpoint.type(() -> String.format(EXPENDITURE_TYPE_SELECT, client, row), expenditureType, false);
        endpoint.sendKeysOneAtATime(Key.ENTER);
        return null;
    }

    @CustomAction
    default CalculatorPage fillExpenditureInstalmentAmount(String client, String row, String expenditureInstalmentAmount) {
        BrokerPortalEndpoint endpoint = getEndpoint(BrokerPortalEndpoint.class);
        endpoint.type(() -> String.format(EXPENDITURE_INSTALMENT_AMOUNT_INPUT, client, row), expenditureInstalmentAmount, false);
        return null;
    }

    @CustomAction
    default CalculatorPage fillExpenditureCreditRestriction(String client, String row, String expenditureCreditRestriction) {
        BrokerPortalEndpoint endpoint = getEndpoint(BrokerPortalEndpoint.class);
        endpoint.type(() -> String.format(EXPENDITURE_CREDIT_RESTRICTION_INPUT, client, row), expenditureCreditRestriction, false);
        return null;
    }

    @CustomAction
    default CalculatorPage fillExpenditureDebt(String client, String row, String expenditureDebt) {
        BrokerPortalEndpoint endpoint = getEndpoint(BrokerPortalEndpoint.class);
        endpoint.type(() -> String.format(EXPENDITURE_DEBT_INPUT, client, row), expenditureDebt, false);
        return null;
    }

    @CustomAction
    default CalculatorPage selectExpenditureCorrection(String client, String row, String expenditureCorrection) {
        BrokerPortalEndpoint endpoint = getEndpoint(BrokerPortalEndpoint.class);
        endpoint.type(() -> String.format(EXPENDITURE_CORRECTION_SELECT, client, row), expenditureCorrection, false);
        endpoint.sendKeysOneAtATime(Key.ENTER);
        return null;
    }

    @TypeInto(PRODUCT_SELECT)
    @KeyPress(Key.ENTER)
    CalculatorPage selectProduct(String product);

    @TypeInto(LOAN_AMOUNT_INPUT)
    CalculatorPage fillLoanAmount(String loanAmount);

    @TypeInto(value = MATURITY_INPUT, clear = true)
    CalculatorPage fillMaturity(String maturity);

    @TypeInto(FIXATION_SELECT)
    @KeyPress(Key.ENTER)
    CalculatorPage selectFixation(String fixation);

    @TypeInto(LOAN_PURPOSE_SELECT)
    @KeyPress(Key.ENTER)
    CalculatorPage selectLoanPurpose(String loanPurpose);

    @TypeInto(SERVICES_SELECT)
    @KeyPress(Key.ENTER)
    CalculatorPage selectService(String service);

    @TypeInto(INSURANCE_TYPE_SELECT)
    CalculatorPage selectInsuranceType(String insuranceType);

    @Click(INSURANCE_CHECKBOX)
    CalculatorPage clickInsuranceCheckbox(Client client);

    @Click(value = LOAN_OWNER_CHECKBOX, jsClick = true)
    CalculatorPage clickLoanOwnerCheckbox();

    @Click(ADD_PROPERTY_BUTTON)
    CalculatorPage clickAddPropertyButton();

    @TypeInto(value = LOAN_SECURITY_ESTATE_TYPE_SELECT, isStringDynamicXpath = true)
    @KeyPress(Key.ENTER)
    CalculatorPage selectLoanSecurityEstateType(String row, String loanSecurityEstateType);

    @TypeInto(value = LOAN_SECURITY_VALUE_INPUT, isStringDynamicXpath = true)
    CalculatorPage fillLoanSecurityAmount(String row, String loanSecurityAmount);

    @TypeInto(value = LOAN_SECURITY_CONTRACT_RELATION_SELECT, isStringDynamicXpath = true)
    @KeyPress(Key.ENTER)
    CalculatorPage selectLoanSecurityContractRelation(String row, String loanSecurityContractRelation);

    @CustomAction
    default CalculatorPage getContractId(Harness harness) {
        BrokerPortalEndpoint endpoint = getEndpoint(BrokerPortalEndpoint.class);
        String str = endpoint.getText(CONTRACT_ID);
        harness.store("CONTRACT_ID", str.replaceAll("\\D+",""));
        return null;
    }

    @Click(BACK_TO_CONTRACT_DETAIL_BUTTON)
    ContractDetailPage opensContractDetailPage();

    @Click(value = CALCULATE_BUTTON, andWait = @Wait(SAVE_CALCULATION_BUTTON))
    CalculatorPage clickCalculateButton();

    @Wait(value = SAVE_CALCULATION_BUTTON, explicitWaitSeconds = 2)
    @Click(value = SAVE_CALCULATION_BUTTON, andWait = @Wait(TO_SHORT_APPLICATION_BUTTON))
    CalculatorPage clickSaveCalculationButton();

    @Click(TO_SHORT_APPLICATION_BUTTON)
    ShortApplicationPage clickToShortApplicationButton();

    @CheckElementPresent(CALCULATION_ALERT_SUCCESS)
    CalculatorPage checkCalculationSuccessfulAlert();
}