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

import cz.moneta.test.dsl.ib.IbPageFlow;
import cz.moneta.test.dsl.ib.components.MainMenu;
import cz.moneta.test.harness.endpoints.ib.IbEndpoint;
import cz.moneta.test.harness.support.web.*;
import org.junit.jupiter.api.Assertions;

import static cz.moneta.test.dsl.ib.loans.consolidationloans.YouRequestToMergeLoansPage.*;

@Wait({TITLE_PAGE_XPATH, LOAN_CALCULATOR, LOAN_CALCULATOR_SLIDER, REPAYMENT_PERIOD_SLIDER})
public interface YouRequestToMergeLoansPage extends IbPageFlow<YouRequestToMergeLoansPage>, MainMenu {
    String TITLE_PAGE_XPATH = "//div[@id='parentform']//td[contains(text(), 'Požadujete sloučit úvěry ve výši')]";
    String REQUIREMENT_ONLINE_BUTTON = "//div[@id='next-step-btn']";

    //Loan calculator
    String LOAN_CALCULATOR = "//div[@id='loanForm']";
    String LOAN_CALCULATOR_SLIDER = "//h3[contains(text(), 'Ke své konsolidaci si můžete ještě půjčit navíc')]//following::div[@class='slider_box']//div[@id='slider1']";
    String REPAYMENT_PERIOD_SLIDER = "//h3[contains(text(), 'Nastavte si, jak dlouho chcete nově splácet')]//following::div[@class='slider_box']//div[@id='slider2']";
    String LOANS_INPUT = "//div[@id='slider1']/ancestor::div[@class='slider_box']//input[@id='loanAmount']";
    String TERMS_INPUT = "//div[@id='slider2']/ancestor::div[@class='slider_box']//input[@id='loanTerms']";
    String INSURANCE_ABBILITY_TO_PAYMENT_RADIO = "//span[contains(text(), 'Pojištění schopnosti splácet')]//preceding-sibling::span[@id='spanInsurance10']";

    // Result table
    String RESULT_TABLE_XPATH = "//table[@id='resultTable']";
    String NEW_AMOUNT_OF_INSTALLMENT = RESULT_TABLE_XPATH + "//td[contains(text(), '%s')]";
    String NEW_AMOUNT_OF_LOAN = RESULT_TABLE_XPATH + "//td[contains(text(), '%s')]";
    String SAVE_MONTHLY = RESULT_TABLE_XPATH + "//td[contains(text(), '%s')]";

    @CustomAction
    default YouRequestToMergeLoansPage moveSlidersAndCheckValues() {
        IbEndpoint ibEndpoint = getEndpoint(IbEndpoint.class);
        String valueLoanInput;
        String newValueLoanInput;
        String valueTermInput;
        String newValueTermInput;

        valueLoanInput = ibEndpoint.getAttribute(LOANS_INPUT, "value");
        ibEndpoint.dragAndDropBy(LOAN_CALCULATOR_SLIDER, 0, 5);
        ibEndpoint.click(() -> LOANS_INPUT);
        newValueLoanInput = ibEndpoint.getAttribute(LOANS_INPUT, "value");
        Assertions.assertNotEquals(valueLoanInput, newValueLoanInput);

        valueTermInput = ibEndpoint.getAttribute(TERMS_INPUT, "value");
        ibEndpoint.dragAndDropBy(REPAYMENT_PERIOD_SLIDER, 0, 5);
        ibEndpoint.click(() -> TERMS_INPUT);
        newValueTermInput = ibEndpoint.getAttribute(TERMS_INPUT, "value");
        Assertions.assertNotEquals(valueTermInput, newValueTermInput);
        return null;
    }

    @CheckElementsPresent({
            @CheckElementPresent(value = NEW_AMOUNT_OF_INSTALLMENT, isStringDynamicXpath = true),
            @CheckElementPresent(value = NEW_AMOUNT_OF_LOAN, isStringDynamicXpath = true),
            @CheckElementPresent(value = SAVE_MONTHLY, isStringDynamicXpath = true)})
    YouRequestToMergeLoansPage checkLablesInResultTable(String nameOfLableNewInstallment, String nameOfLableNewAmountLoan, String nameOfLableSaveMonthly);

    @Wait(explicitWaitSeconds = 5)
    @TypeIntos({
            @TypeInto(value = LOANS_INPUT, clear = true, andWait = @Wait(explicitWaitSeconds = 5)),
            @TypeInto(value = TERMS_INPUT, clear = true, andWait = @Wait(explicitWaitSeconds = 5))})
    YouRequestToMergeLoansPage fillInLoanAndTermInput(String fillInLoan, String fillInTerm);

    @Click(INSURANCE_ABBILITY_TO_PAYMENT_RADIO)
    YouRequestToMergeLoansPage clickToInsuranceRadioButton();

    @Click(REQUIREMENT_ONLINE_BUTTON)
    ThankPage clickToRequirementOnlineButton();
}
