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 ExpressLoanCalculator 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__loanAmountBoxFullDesign___Lz7KF')]//div[contains(@class, 'calcSliderRangeNumber__thumbDefault___qxXnH')]";
    String CALCULATOR_LENGTH_BY_SLIDER = CALCULATOR_DIV + "//div[contains(@class, 'personalCalculator__paymentPeriodBoxFullDesign___oLJII')]//div[contains(@class, 'calcSliderRangeNumber__thumbDefault___qxXnH')]";
    String CALCULATOR_BOX = CALCULATOR_DIV + "//div[@class='CalcCalculatorBoxFullDesign__box___3AiZy']";
    String CALCULATOR_SPINNER = "//div[contains(@id, 'PersonalCalculator')]//div[@class='moneta-spinner--wrapper']";
    String RESULT_SECTION_MONTHLY_PRICE_SPAN = "//div[contains(@class, 'calcBoxFullDesign__resultBox___3LfyW')]//div[contains(@class, 'header__header___CJ0TM')]//span[@itemprop='price']/span ";
    String CALCULATOR_LOAN_BUTTON = CALCULATOR_DIV + "//button[@class='button__button___3t77U 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)
    ExpressLoanCalculator clickLoanCalculatorAmountInput();

    @CustomAction
    default ExpressLoanCalculator 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 ExpressLoanCalculator 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 ExpressLoanCalculator 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)
    ExpressLoanCalculator clickLoanCalculatorButton();
}