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

    String CALCULATOR_DIV = "//div[contains(@id,'HousingPlanCalculator')]";
    String CALCULATOR_BUILDING_SAVINGS_AMOUNT_INPUT = "(" + CALCULATOR_DIV + "//div[@class='styles__rowWrapper___2O0qg']//input[contains(@class,'FormattedNumberInput')])[1]";
    String CALCULATOR_BUILDING_SAVINGS_AMOUNT_SLIDER = "(" + CALCULATOR_DIV + "//div[@class='styles__sliderWrapper___2u_3B sc-bdVaJa iHZvIS']//div[contains(@class,'Slider__slider')])[1]";
    String CALCULATOR_BUILDING_SAVINGS_LENGTH_BY_SLIDER = "(" + CALCULATOR_DIV + "//div[@class='styles__sliderWrapper___2u_3B sc-bdVaJa iHZvIS']//div[contains(@class,'Slider__slider')])[2]";
    String CALCULATOR_BOX = CALCULATOR_DIV + "//div[@class='styles__EntrySection___3NWX7 sc-bdVaJa iHZvIS']";
    String CALCULATOR_SPINNER = CALCULATOR_DIV + "//div[@class='moneta-spinner__logo']";
    String RESULT_SECTION_SUM_SPAN = CALCULATOR_DIV + "//div[contains(@class, 'styles__ResultSection')]//span[@class='Text__PrimaryText-e8ep9d-0 GpVeu sc-bdVaJa dYbDtt']/span/span";

    @Wait(value = CALCULATOR_SPINNER, until = Until.GONE)
    @GetElementContent(RESULT_SECTION_SUM_SPAN)
    String getSumOfSaving();

    @Wait(CALCULATOR_BUILDING_SAVINGS_AMOUNT_INPUT)
    @Click(CALCULATOR_BUILDING_SAVINGS_AMOUNT_INPUT)
    BuildingSavingsCalculator clickBuildingSavingCalculatorAmountInput();

    @CustomAction
    default BuildingSavingsCalculator fillBuildingSavingCalculatorAmountInput(String amount) {
        MonetaPortalEndpoint endpoint = getEndpoint(MonetaPortalEndpoint.class);
        String initialSumOfSaving = this.getSumOfSaving();
        endpoint.type(() -> CALCULATOR_BUILDING_SAVINGS_AMOUNT_INPUT, amount, true);
        endpoint.click(() -> CALCULATOR_BOX);
        String changedSumOfSaving = this.getSumOfSaving();
        System.out.println(initialSumOfSaving);
        System.out.println(changedSumOfSaving);
        Assertions.assertNotEquals(initialSumOfSaving, changedSumOfSaving, "Sum of savings calculation did not change.");
        return null;
    }

    @CustomAction
    default BuildingSavingsCalculator moveBuildingSavingCalculatorAmountSlider(int xOffset) {
        MonetaPortalEndpoint endpoint = getEndpoint(MonetaPortalEndpoint.class);
        String initialSumOfSaving = this.getSumOfSaving();
        endpoint.dragAndDropBy(CALCULATOR_BUILDING_SAVINGS_AMOUNT_SLIDER, xOffset, 0);
        String changedSumOfSaving = this.getSumOfSaving();
        System.out.println(changedSumOfSaving);
        Assertions.assertNotEquals(initialSumOfSaving, changedSumOfSaving, "Sum of savings calculation did not change.");
        return null;
    }

    @CustomAction
    default BuildingSavingsCalculator moveBuildingSavingCalculatorLengthBySlider(int xOffset) {
        MonetaPortalEndpoint endpoint = getEndpoint(MonetaPortalEndpoint.class);
        String initialSumOfSaving = this.getSumOfSaving();
        endpoint.dragAndDropBy(CALCULATOR_BUILDING_SAVINGS_LENGTH_BY_SLIDER, xOffset, 0);
        String changedSumOfSaving = this.getSumOfSaving();
        System.out.println(changedSumOfSaving);
        Assertions.assertNotEquals(initialSumOfSaving, changedSumOfSaving, "Sum of savings calculation did not change.");
        return null;
    }
}