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

import cz.moneta.test.dsl.monetaportal.components.forms.AlaCarteLoanForm;
import cz.moneta.test.harness.context.StoreAccessor;
import cz.moneta.test.harness.endpoints.MonetaPortalEndpoint;
import cz.moneta.test.harness.support.web.Builder;
import cz.moneta.test.harness.support.web.Click;
import cz.moneta.test.harness.support.web.CustomAction;
import org.junit.jupiter.api.Assertions;

public interface CreditCalculator extends Builder, StoreAccessor {

    //Credit calculator
    String CALCULATOR_CREDIT_CARD_AMOUNT_INPUT = "//div[contains(@class, 'inputWrapper')]//input";
    String CALCULATOR_CREDIT_CARD_AMOUNT_SLIDER = "//div[contains(@class, 'Slider__wrapper')]/div[@role='slider']";
    String CALCULATOR_APPROVE_ONLINE_BUTTON = "//div[contains(@class, 'buttonWrapper')]//button";

    @Click(CALCULATOR_CREDIT_CARD_AMOUNT_INPUT)
    CreditCalculator clickCreditCalculatorAmountInput();

    @Click(CALCULATOR_APPROVE_ONLINE_BUTTON)
    AlaCarteLoanForm clickCreditCalculatorOnlineButton();

    @CustomAction
    default CreditCalculator fillCreditCalculatorAmountInput(String amount) {
        MonetaPortalEndpoint endpoint = getEndpoint(MonetaPortalEndpoint.class);
        String initialCreditAmountSlider = endpoint.getAttribute(CALCULATOR_CREDIT_CARD_AMOUNT_SLIDER, "aria-valuenow");
        endpoint.type(() -> CALCULATOR_CREDIT_CARD_AMOUNT_INPUT, amount, true);
        String changedCreditAmountSlider = endpoint.getAttribute(CALCULATOR_CREDIT_CARD_AMOUNT_SLIDER, "aria-valuenow");
        Assertions.assertNotEquals(initialCreditAmountSlider, changedCreditAmountSlider, "Credit amount did not change.");
        return null;
    }

    @CustomAction
    default CreditCalculator moveCreditCalculatorAmount(int xOffset) {
        MonetaPortalEndpoint endpoint = getEndpoint(MonetaPortalEndpoint.class);
        String initialMonthlyCreditAmount = endpoint.getAttribute(CALCULATOR_CREDIT_CARD_AMOUNT_INPUT, "value");
        endpoint.dragAndDropBy(CALCULATOR_CREDIT_CARD_AMOUNT_SLIDER, xOffset, 0);
        String changedMonthlyCreditAmount = endpoint.getAttribute(CALCULATOR_CREDIT_CARD_AMOUNT_INPUT, "value");
        Assertions.assertNotEquals(initialMonthlyCreditAmount, changedMonthlyCreditAmount, "Credit amount did not change.");
        return null;
    }
}