package cz.moneta.test.dsl.ib.loans.expressloan;

import cz.moneta.test.dsl.ib.IbPageFlow;
import cz.moneta.test.dsl.ib.loans.IbLoanData;
import cz.moneta.test.dsl.ib.loans.expressloan.dataCheckPart.PersonalDataExpressLoanPage;
import cz.moneta.test.harness.context.StoreAccessor;
import cz.moneta.test.harness.endpoints.ib.IbEndpoint;
import cz.moneta.test.harness.support.web.CustomAction;
import cz.moneta.test.harness.support.web.Lookup;
import cz.moneta.test.harness.support.web.Until;
import cz.moneta.test.harness.support.web.Wait;
import cz.moneta.test.harness.support.web.Waits;

import static cz.moneta.test.dsl.ib.loans.expressloan.IbExpressLoanPage.EXPRESS_LOAN_CALCULATOR;

@Wait(value = {EXPRESS_LOAN_CALCULATOR}, by = Lookup.ID)
public interface IbExpressLoanPage extends IbPageFlow<IbExpressLoanPage>, StoreAccessor {
    String EXPRESS_LOAN_CALCULATOR = "loanForm";

    String LOAN_AMOUNT_INPUT = "//input[@id = 'loanAmount']";
    String LOAN_AMOUNT_SLIDER = "//div[@id = 'slider1']";

    String LOAN_TERMS_INPUT = "//input[@id = 'loanTerms']";
    String LOAN_TERMS_SLIDER = "//div[@id = 'slider2']";

    String MONTHLY_INSTALLMENT_VALUE = "monthlyInstallmentId";
    String INT_RATE_VALUE = "intRateId";
    String RPSN = "rpsnId";
    String TOTAL_AMOUNT = "totalAmountId";

    String CONTINUE_BUTTON = "//a[@class = 'continue-button']";
    String LOADER = "//div[@class = 'busyLoaderImg']";

    @Waits(value = {
            @Wait(LOAN_AMOUNT_SLIDER),
            @Wait(value = LOAN_AMOUNT_INPUT)})
    @CustomAction
    default IbExpressLoanPage setLoanAmount(String loanAmount) {
        IbEndpoint endpoint = getEndpoint(IbEndpoint.class);
        endpoint.type(() -> LOAN_AMOUNT_INPUT, loanAmount, true);
        endpoint.sleepSeconds(5);
        endpoint.waitForElementsToLoad(30, Until.GONE, LOADER);
        return null;
    }


    @Waits(value = {
            @Wait(LOAN_TERMS_SLIDER),
            @Wait(value = LOAN_TERMS_INPUT)})
    @CustomAction
    default IbExpressLoanPage setLoanTerms(String loanTerms) {
        IbEndpoint endpoint = getEndpoint(IbEndpoint.class);
        endpoint.type(() -> LOAN_TERMS_INPUT, loanTerms, true);
        endpoint.sleepSeconds(5);
        endpoint.waitForElementsToLoad(30, Until.GONE, LOADER);
        return null;
    }

    @Wait(CONTINUE_BUTTON)
    @Wait(value = LOADER, until = Until.GONE)
    @CustomAction
    default PersonalDataExpressLoanPage getDataAndContinueToPersonalData(IbLoanData loanData) {
        IbEndpoint endpoint = getEndpoint(IbEndpoint.class);
        endpoint.sleepSeconds(10);
        loanData.setMonthlyInstallment(endpoint.getText(MONTHLY_INSTALLMENT_VALUE, Lookup.ID));
        loanData.setIntRate(endpoint.getText(INT_RATE_VALUE, Lookup.ID));
        loanData.setRpsn(endpoint.getText(RPSN, Lookup.ID));
        loanData.setTotalAmount(endpoint.getText(TOTAL_AMOUNT, Lookup.ID));
        endpoint.click(() -> CONTINUE_BUTTON);
        return null;
    }
}

