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 BusinessLoanCalculator extends Builder, StoreAccessor {

    String CALCULATOR_DIV = "//div[@id='PersonalLoanCalculator_INSTANCE_XpGpIBXXDICl-react-root']";
    String CALCULATOR_APPROVE_ONLINE_BUTTON = CALCULATOR_DIV + "//a[@class='btn btn-tertiary sc-bdVaJa gRrvFh']";
    String CALCULATOR_LOAN_AMOUNT_INPUT = CALCULATOR_DIV + "//input[@class='FormattedNumberInput-sc-1v8uygx-1 FGtuM']/..//span[text()='Kč']/../..//input";
    String CALCULATOR_LOAN_AMOUNT_SLIDER = "(" + CALCULATOR_DIV + "//div[@class='FormattedSliderInput__sliderRangeWrapper___11-Gc'])[1]";
    String CALCULATOR_LENGTH_BY_INPUT = CALCULATOR_DIV + "//input[@class='FormattedNumberInput-sc-1v8uygx-1 FGtuM']/..//span[text()='splátek']/../..//input";
    String CALCULATOR_LENGTH_BY_SLIDER = "(" + CALCULATOR_DIV + "//div[@class='FormattedSliderInput__sliderRangeWrapper___11-Gc'])[2]";
    String CALCULATOR_BOX = CALCULATOR_DIV + "//div[@class='LoanCalculatorSection__slidersWrapper___2al12']";
    String CALCULATOR_SPINNER = CALCULATOR_DIV + "//div[@class='LoanResultSection__loadingCalculation___2QPS3']";
    String RESULT_SECTION_MONTHLY_PRICE_SPAN = CALCULATOR_DIV + "//div[contains(@class, 'LoanResultSection__result___2fwgr')]//span[@itemprop='price']/span";

    @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)
    BusinessLoanCalculator clickBusinessCalculatorAmountInput();

    @CustomAction
    default BusinessLoanCalculator fillBusinessCalculatorAmountInput(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 BusinessLoanCalculator moveBusinessCalculatorLoanAmount(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 BusinessLoanCalculator fillBusinessCalculatorLengthBy(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 BusinessLoanCalculator moveBusinessCalculatorLoanLengthBy(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;
    }
}