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

    //Express loan calculator
    String CALCULATOR_DIV = "//div[contains(@id, 'PersonalCalculator')]";
    String CALCULATOR_AMOUNT_INPUT = CALCULATOR_DIV + "//input[@class='calcInput__input___KmRsG calcInput__inputStrong___2ApyV']";
    String CALCULATOR_LOAN_AMOUNT_SLIDER = CALCULATOR_DIV + "//div[contains(@class, 'personalCalculator__loanAmountWrapperBannerDesign___L9eEd')]//div[contains(@class, 'calcSliderRangeNumber__thumbVisual___18-pc')]";
    String CALCULATOR_LENGTH_BY_SLIDER = CALCULATOR_DIV + "//div[contains(@class, 'personalCalculator__paymentPeriodWrapperBannerDesign___3isSh')]//div[contains(@class, 'calcSliderRangeNumber__thumbVisual___18-pc')]";
    String CALCULATOR_BOX = CALCULATOR_DIV + "//div[@class='calcCalculatorBoxBannerDesignStyle__Box-sc-1gailm5-0 EnhNJ']";
    String CALCULATOR_SPINNER = "//div[contains(@id, 'PersonalCalculator')]//div[@class='moneta-spinner--wrapper']";
    String RESULT_SECTION_MONTHLY_PRICE_SPAN = CALCULATOR_DIV + "//div[contains(@class, 'calcResultRowBannerDesignStyle__RightStyle')]//span[@itemprop='price']/span ";
    String CALCULATOR_LOAN_BUTTON = CALCULATOR_DIV + "//button[@class='button__button___2nGpm btn btn-tertiary sc-bdVaJa gRrvFh']";

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

    @Wait(value = {CALCULATOR_AMOUNT_INPUT})
    @Click(CALCULATOR_AMOUNT_INPUT)
    ExpressLoanBannerCalculator clickLoanCalculatorAmountInput();

    @CustomAction
    default ExpressLoanBannerCalculator fillLoanCalculatorAmountInput(String amount) {
        MonetaPortalEndpoint endpoint = getEndpoint(MonetaPortalEndpoint.class);
        String initialMonthlyPrice = this.getMonthlyPrice();
        endpoint.type(() -> CALCULATOR_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 ExpressLoanBannerCalculator moveCalculatorLoanAmount(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 ExpressLoanBannerCalculator moveCalculatorLoanLengthBy(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;
    }

    @Wait(value = {CALCULATOR_LOAN_BUTTON})
    @Click(CALCULATOR_LOAN_BUTTON)
    ExpressLoanBannerCalculator clickLoanCalculatorButton();
}