package cz.moneta.test.dsl.monetaportal.mortgages;

import cz.moneta.test.dsl.finanso.FinansoIncomePage;
import cz.moneta.test.harness.context.StoreAccessor;
import cz.moneta.test.harness.endpoints.MonetaPortalEndpoint;
import cz.moneta.test.harness.support.web.*;

@Wait(MortgagePage.MORTGAGE_CALCULATOR)
@Wait(MortgagePage.REALITY_AMOUNT_INPUT)
public interface MortgagePage extends WebFlow<MortgagePage>, StoreAccessor {
    String MORTGAGE_CALCULATOR = "//mortgage-calculator-widget";
    String FIND_MAX_MORTGAGE_TAB_BUTTON = "//button[@aria-controls='tabPanelIdfindMaxMortgage:r0:']";
    String INCOME_APPLICANT_INPUT = MORTGAGE_CALCULATOR + "//input[@name='incomeApplicant']";
    String FEE_APPLICANT_INPUT = MORTGAGE_CALCULATOR + "//input[@name='feeApplicant']";
    String IS_COAPPLICANT_TRUE_RADIO_BUTTON = MORTGAGE_CALCULATOR + "//div[./input[@name='isCoApplicant' and @value='true']]";
    String IS_COAPPLICANT_FALSE_RADIO_BUTTON = MORTGAGE_CALCULATOR + "//div[./input[@name='isCoApplicant' and @value='false']]";
    String MAX_MORTGAGE_RESULT_SPAN = MORTGAGE_CALCULATOR + "//span[@data-message-id='MortgageResultSection.headerMaximumMortgage.mortgage']/ancestor::div[2]//span[@aria-hidden='true']";
    String MAX_MORTGAGE_TRANSFER_RESULT_BUTTON = MORTGAGE_CALCULATOR + "//button[not(@disabled) and .//* [@data-message-id='MortgageResultSection.iWantLoanMaximumMortgage.mortgage']]";
    String MAX_MORTGAGE_RESULT_MESSAGE = MORTGAGE_CALCULATOR + "//span[@data-message-id='MortgageResultSection.headerMaximumMortgage.mortgage']";
    String REALITY_AMOUNT_INPUT = MORTGAGE_CALCULATOR + "//input[@name='realtyAmount']";
    String AMOUNT_INPUT = MORTGAGE_CALCULATOR + "//input[@name='amount']";
    String MATURITY_INPUT = MORTGAGE_CALCULATOR + "//input[@name='maturity']";
    String FIXATION_RADIO = MORTGAGE_CALCULATOR + "//div[./input[@value='%S']]";
    String I_WANT_LOAN_BUTTON = MORTGAGE_CALCULATOR + "//button[not(@disabled) and .//span[@data-message-id='MortgageResultSection.iWantLoan.mortgage']]";
    String RESULT_LOADING = MORTGAGE_CALCULATOR + "//div[contains(@class,'Loading')]";

    @Click(value = FIND_MAX_MORTGAGE_TAB_BUTTON, jsClick = true, andWait = @Wait(INCOME_APPLICANT_INPUT))
    MortgagePage clickFindMaxMortgageTabButton();

    @TypeInto(value = INCOME_APPLICANT_INPUT, clear = true)
    MortgagePage fillIncome(String income);

    @TypeInto(value = FEE_APPLICANT_INPUT, clear = true)
    MortgagePage fillFee(String fee);

    @Click(IS_COAPPLICANT_TRUE_RADIO_BUTTON)
    MortgagePage clickIsApplicantTrue();

    @Click(IS_COAPPLICANT_FALSE_RADIO_BUTTON)
    MortgagePage clickIsApplicantFalse();

    @Click(value = FIXATION_RADIO, isStringDynamicXpath = true, jsClick = true, andWait = @Wait(value = RESULT_LOADING, until = Until.GONE))
    MortgagePage clickFixation(String fixation);

    @Wait(value = I_WANT_LOAN_BUTTON, until = Until.CLICKABLE)
    @Click(value = I_WANT_LOAN_BUTTON, jsClick = true)
    FinansoIncomePage clickIWantLoan();

    @Wait(MAX_MORTGAGE_RESULT_MESSAGE)
    @Wait(MAX_MORTGAGE_TRANSFER_RESULT_BUTTON)
    @Wait(MAX_MORTGAGE_RESULT_SPAN)
    @CustomAction
    default MortgagePage storeMaxMortgageResult(String storeKey) {
        MonetaPortalEndpoint e = getEndpoint(MonetaPortalEndpoint.class);
        String resultText = e.getText(MAX_MORTGAGE_RESULT_SPAN);
        store(storeKey, resultText.replaceAll("\\D", ""));
        return null;
    }

    @Click(value = MAX_MORTGAGE_TRANSFER_RESULT_BUTTON, andWait = @Wait(I_WANT_LOAN_BUTTON))
    MortgagePage clickTransferMaxMortgageResult();

    @CustomAction
    default MortgagePage storeMortgageRealityAmountValue(String storeKey) {
        String valueText = getInputValue(REALITY_AMOUNT_INPUT);
        store(storeKey, valueText);
        return null;
    }

    @CustomAction
    default MortgagePage storeMortgageAmountValue(String storeKey) {
        String valueText = getInputValue(AMOUNT_INPUT);
        store(storeKey, valueText);
        return null;
    }

    @CustomAction
    default MortgagePage storeMortgageMaturityValue(String storeKey) {
        String valueText = getInputValue(MATURITY_INPUT);
        store(storeKey, valueText);
        return null;
    }

    private String getInputValue(String xpath) {
        MonetaPortalEndpoint e = getEndpoint(MonetaPortalEndpoint.class);
        return e.getAttribute(xpath, "value").replaceAll("\\D", "");
    }
}