package cz.moneta.test.dsl.ufo.banka.pages.cards.debit;

import cz.moneta.test.dsl.Harness;
import cz.moneta.test.dsl.ufo.banka.pages.common.pages.OBR001_S001_PrintInstructionPage;
import cz.moneta.test.harness.context.StoreAccessor;
import cz.moneta.test.harness.support.web.*;

@Wait(value = PLK008_S001_ChangeInfoOfDebitCard.PLK008_S001_PAGE_LABEL_XPATH, by = Lookup.XPATH, waitSecondsForElement = 40)
public interface PLK008_S001_ChangeInfoOfDebitCard extends WebFlow<PLK008_S001_ChangeInfoOfDebitCard>, StoreAccessor {

    String PLK008_S001_PAGE_LABEL_XPATH = "//div[text()='PLK008_S001']";
    String POS_LIMIT_INPUT = "PLK008_S001_U006";
    String SAVE_BUTTON = "PLK008_S001_B003";
    String ATM_LIMIT_INPUT = "PLK008_S001_U005";
    String INT_LIMIT_INPUT = "PLK008_S001_E014";

    @Click(value = SAVE_BUTTON, jsClick = true)
    PLK008_S001_ChangeInfoOfDebitCard clickSaveFalse();

    @Click(value = SAVE_BUTTON, jsClick = true)
    OBR001_S001_PrintInstructionPage clickSaveTrue();

    @DismissAlert(waitSecondsForAlert = 2)
    PLK008_S001_ChangeInfoOfDebitCard dismissAlert();

    @TypeInto(clear = true, value = ATM_LIMIT_INPUT)
    PLK008_S001_ChangeInfoOfDebitCard changeAtmLimit(String atmLimit);

    @TypeInto(clear = true, value = INT_LIMIT_INPUT)
    PLK008_S001_ChangeInfoOfDebitCard changeIntLimit(String intLimit);

    @TypeInto(clear = true, value = POS_LIMIT_INPUT)
    PLK008_S001_ChangeInfoOfDebitCard changePosLimit(String posLimit);

    @CustomAction
    default PLK008_S001_ChangeInfoOfDebitCard checkAndChangeLimits(String atmLimitExpected, String intLimitExpected, String posLimitExpected, Harness harness) {
        String atmLimitActual = harness.get("LIM_BANK_PAYMENT_CURRENT");
        String intLimitActual = harness.get("LIM_INT_PAYMENT_CURRENT");
        String posLimitActual = harness.get("LIM_MOTO_PAYMENT_CURRENT");

        if (atmLimitActual.equals(atmLimitExpected)) {
            atmLimitExpected = atmLimitActual.replace("0", "1");
        }
        changeAtmLimit(atmLimitExpected);
        harness.store("LIM_BANK_PAYMENT_CHANGED", atmLimitExpected);

        if (intLimitActual.equals(intLimitExpected)) {
            intLimitExpected = intLimitActual.replace("0", "1");
        }
        changeIntLimit(intLimitExpected);
        harness.store("LIM_INT_PAYMENT_CHANGED", intLimitExpected);

        if (posLimitActual.equals(posLimitExpected)) {
            posLimitExpected = posLimitActual.replace("0", "1");
        }
        changePosLimit(posLimitExpected);
        harness.store("LIM_MOTO_PAYMENT_CHANGED", posLimitExpected);

        return null;
    }
}