package cz.moneta.test.dsl.ufo.banka.pages.cards.credit.service;

import cz.moneta.test.dsl.Harness;
import cz.moneta.test.dsl.ufo.banka.UfoBankaPageFlow;
import cz.moneta.test.dsl.ufo.banka.pages.common.pages.TIS004_S002_SignerSelectionPage;
import cz.moneta.test.dsl.ufo.banka.pages.fon.TIS050_S001_SignerSelectionPage;
import cz.moneta.test.dsl.ufo.banka.pages.fon.TIS050_S002_SignerSelectionPage;
import cz.moneta.test.harness.context.StoreAccessor;
import cz.moneta.test.harness.endpoints.ufo.UfoEndpoint;
import cz.moneta.test.harness.support.web.*;

import static cz.moneta.test.dsl.ufo.banka.pages.cards.credit.service.POZ001_S008_BlockingPage.PAGE_ID_XPATH;

@Wait(value = PAGE_ID_XPATH, by = Lookup.XPATH, waitSecondsForElement = 60)
public interface POZ001_S008_BlockingPage extends UfoBankaPageFlow<POZ001_S008_BlockingPage>, StoreAccessor {

    String PAGE_ID_XPATH = "//div[text()='POZ001_S008']";
    String ATM_LIMIT_INPUT = "POZ001_S008_E003";
    String INT_LIMIT_INPUT = "POZ001_S008_E017";
    String MOTO_LIMIT_INPUT = "POZ001_S008_E012";
    String CHANGE_LIMIT_BUTTON = "POZ001_S008_B007";
    String BLOCK_CARD_BUTTON = "POZ001_S008_B003";
    String REASON_OF_BLOCK_SELECT = "POZ001_S008_C002";
    String BLOCK_DATE_INPUT = "POZ001_S008_E019";
    String CURRENT_DATE_LABEL = "XXX001_T001_L002";
    String BLOCK_TIME_INPUT = "POZ001_S008_E020";
    String TELL_NUM_INPUT = "POZ001_S008_E021";

    @Select(REASON_OF_BLOCK_SELECT)
    POZ001_S008_BlockingPage selectBlockingReason(String reason);

    @Click(BLOCK_CARD_BUTTON)
    TIS004_S002_SignerSelectionPage clickBlockCard();

    @Click(BLOCK_CARD_BUTTON)
    TIS050_S002_SignerSelectionPage clickBlockCardToSignerPage();

    @Click(BLOCK_CARD_BUTTON)
    TIS050_S001_SignerSelectionPage clickBlockCreditCardToSignerPage();

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

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

    @TypeInto(clear = true, value = MOTO_LIMIT_INPUT)
    POZ001_S008_BlockingPage changeMotoLimit(String motoLimit);

    @Click(value = CHANGE_LIMIT_BUTTON, jsClick = true)
    @DismissAlert(waitSecondsForAlert = 5)
    POZ001_S008_BlockingPage changeLimitsWithAlert();

    @Click(value = CHANGE_LIMIT_BUTTON, jsClick = true)
    TIS050_S001_SignerSelectionPage changeLimits();

    @CustomAction
    default POZ001_S008_BlockingPage checkAndChangeLimits(String atmLimitExpected, String intLimitExpected, String motoLimitExpected, 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(motoLimitExpected)) {
            motoLimitExpected = posLimitActual.replace("0","1");
        }
        changeMotoLimit(motoLimitExpected);
        harness.store("LIM_MOTO_PAYMENT_CHANGED", motoLimitExpected);

        return null;
    }

    @CustomAction
    default POZ001_S008_BlockingPage fillDate() {
        UfoEndpoint endpoint = getEndpoint(UfoEndpoint.class);
        endpoint.type(() -> BLOCK_DATE_INPUT, endpoint.getText(CURRENT_DATE_LABEL), true);
        return null;
    }

    @TypeInto(value = BLOCK_TIME_INPUT, clear = true)
    POZ001_S008_BlockingPage fillTime(String time);

    @TypeInto(TELL_NUM_INPUT)
    POZ001_S008_BlockingPage fillTellNum(String tellNum);
}
