package cz.moneta.test.dsl.monetaportal.components.calculators;

import cz.moneta.test.harness.context.StoreAccessor;
import cz.moneta.test.harness.endpoints.MonetaPortalEndpoint;
import cz.moneta.test.harness.support.web.*;
import org.junit.jupiter.api.Assertions;

public interface LoanCalculator extends Builder, StoreAccessor {

    //Loan calculator
    String CALCULATOR_DIV = "//section[contains(@id, 'portlet_Wso2LoanCalculator_INSTANCE')]";
    String CALCULATOR_LOAN_AMOUNT_INPUT = "(" + CALCULATOR_DIV + "//input[@name='amount'])";
    String CALCULATOR_LOAN_AMOUNT_SLIDER = "(" + CALCULATOR_DIV + "//div[contains(@class, 'sc-dEFUer klhqCs')]//div[contains(@class, 'rc-slider-handle')])[1]";
    String CALCULATOR_LENGTH_BY_INPUT = "(" + CALCULATOR_DIV + "//input[@name='paymentPeriod'])";
    String CALCULATOR_LENGTH_BY_SLIDER = "(" + CALCULATOR_DIV + "//div[contains(@class, 'sc-dEFUer klhqCs')]//div[contains(@class, 'rc-slider-handle')])[2]";
    String CALCULATOR_BOX = CALCULATOR_DIV + "//div[contains(@class, 'calculatorLayoutstyle__ResultWrapper-sc-yndede-2')]";
    String CALCULATOR_SPINNER = "//div[contains(@id, 'Wso2LoanCalculator_INSTANCE')]//div[contains(@class, 'loanCalculatorstyles__ResultLoadingWrapper-sc')]";
    String RESULT_SECTION_MONTHLY_PRICE_SPAN = "//div[contains(@class, 'calculatorLayoutstyle__ResultWrapper-sc-yndede-2')]//span[@class='sc-guDLey bHiqw']";
    String CALCULATOR_LOAN_FIXATION_BUTTON = CALCULATOR_DIV + "//span[text()='3 roky']";
    String CALCULATOR_RESULT_SECTION_LOAN_BUTTON = CALCULATOR_DIV + "//div[contains(@class, 'calculatorLayoutstyle__ResultWrapper')]//button";

    @Wait(value = CALCULATOR_SPINNER, until = Until.GONE)
    @GetElementContent(RESULT_SECTION_MONTHLY_PRICE_SPAN)
    String getMonthlyPrice();

    @Wait(value = {CALCULATOR_LOAN_AMOUNT_INPUT})
    @Click(CALCULATOR_LOAN_AMOUNT_INPUT)
    LoanCalculator clickLoanCalculatorAmountInput();

    @CustomAction
    default LoanCalculator fillLoanCalculatorAmountInput(String amount) {
        MonetaPortalEndpoint endpoint = getEndpoint(MonetaPortalEndpoint.class);
        String initialMonthlyPrice = this.getMonthlyPrice();
        endpoint.type(() -> CALCULATOR_LOAN_AMOUNT_INPUT, amount, true);
        endpoint.click(() -> CALCULATOR_BOX);
        String changedMonthlyPrice = this.getMonthlyPrice();
        System.out.println(initialMonthlyPrice);
        System.out.println(changedMonthlyPrice);
        Assertions.assertNotEquals(initialMonthlyPrice, changedMonthlyPrice, "Monthly price calculation did not change.");
        return null;
    }

    @CustomAction
    default LoanCalculator moveLoanCalculatorLoanAmount(int xOffset) {
        MonetaPortalEndpoint endpoint = getEndpoint(MonetaPortalEndpoint.class);
        String initialMonthlyPrice = this.getMonthlyPrice();
        endpoint.dragAndDropBy(CALCULATOR_LOAN_AMOUNT_SLIDER, xOffset, 0);
        String changedMonthlyPrice = this.getMonthlyPrice();
        System.out.println(changedMonthlyPrice);
        Assertions.assertNotEquals(initialMonthlyPrice, changedMonthlyPrice, "Monthly price calculation did not change.");
        return null;
    }

    @CustomAction
    default LoanCalculator fillLoanCalculatorLengthBy(String amount) {
        MonetaPortalEndpoint endpoint = getEndpoint(MonetaPortalEndpoint.class);
        String initialMonthlyPrice = this.getMonthlyPrice();
        endpoint.type(() -> CALCULATOR_LENGTH_BY_INPUT, amount, true);
        endpoint.click(() -> CALCULATOR_BOX);
        String changedMonthlyPrice = this.getMonthlyPrice();
        System.out.println(initialMonthlyPrice);
        System.out.println(changedMonthlyPrice);
        Assertions.assertNotEquals(initialMonthlyPrice, changedMonthlyPrice, "Monthly price calculation did not change.");
        return null;
    }

    @CustomAction
    default LoanCalculator moveLoanCalculatorLoanLengthBy(int xOffset) {
        MonetaPortalEndpoint endpoint = getEndpoint(MonetaPortalEndpoint.class);
        String initialMonthlyPrice = this.getMonthlyPrice();
        endpoint.dragAndDropBy(CALCULATOR_LENGTH_BY_SLIDER, xOffset, 0);
        String changedMonthlyPrice = this.getMonthlyPrice();
        System.out.println(changedMonthlyPrice);
        Assertions.assertNotEquals(initialMonthlyPrice, changedMonthlyPrice, "Monthly price calculation did not change.");
        return null;
    }

    @CustomAction
    default LoanCalculator clickLoanCalculatorFixationButton() {
        MonetaPortalEndpoint endpoint = getEndpoint(MonetaPortalEndpoint.class);
        String initialMonthlyPrice = this.getMonthlyPrice();
        endpoint.click(() -> CALCULATOR_LOAN_FIXATION_BUTTON);
        String changedMonthlyPrice = this.getMonthlyPrice();
        System.out.println(changedMonthlyPrice);
        Assertions.assertNotEquals(initialMonthlyPrice, changedMonthlyPrice, "Monthly price calculation did not change.");
        return null;
    }
}