package cz.moneta.test.dsl.ufo.banka.pages.products.debit.card;

import cz.moneta.test.dsl.ufo.banka.tasks.accountenums.DebitCardFeePlan;
import cz.moneta.test.dsl.ufo.operations.shared.SharedElements;
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.products.debit.card.NPK001_S001_DebitCardRequestPage.NPK001_S001_PAGE_LABEL_XPATH;
import static cz.moneta.test.dsl.ufo.banka.pages.products.debit.card.NPK001_S001_DebitCardRequestPage.SEND_BUTTON;
import static cz.moneta.test.dsl.ufo.operations.shared.SharedElements.LOAD_IMG_DIV_CLASS;

@Wait(value = NPK001_S001_PAGE_LABEL_XPATH, by = Lookup.XPATH)
@Wait(SEND_BUTTON)
@Wait(value = LOAD_IMG_DIV_CLASS, until = Until.GONE, by = Lookup.CLASSNAME)
public interface NPK001_S001_DebitCardRequestPage extends WebFlow<NPK001_S001_DebitCardRequestPage>, SharedElements, StoreAccessor {
    String NPK001_S001_PAGE_LABEL_XPATH = "//div[text()='NPK001_S001']";

    String SEND_BUTTON = "NPK001_S001_B002";
    String ENTER_BUTTON = "NPK001_S001_B006";
    String CARD_DESIGN_BUTTON = "NPK001_S001_B008";

    String CURRENT_ACCOUNT_SELECT = "NPK001_S001_C001";
    String CARD_TYPE_SELECT = "NPK001_S001_C002";
    String CARD_FEE_PLAN_SELECT = "NPK001_S001_C003";
    String CARD_VARIANT_SELECT = "NPK001_S001_C004";

    String ACTIVATE_PASSWORD = "NPK001_S001_E004";

    String PERMANENT_ADDRESS_RADIO_BUTTON = "NPK001_S001_R001_1";
    String BRANCH_ADDRESS_RADIO_BUTTON = "NPK001_S001_R001_3";
    String PERMANENT_ADDRESS_FOR_PIN_RADIO_BUTTON = "NPK001_S001_R002_1";
    String PLASTIC = "NPK001_S001_R003_1";
    String VIRTUAL = "NPK001_S001_R003_5";
    String CLIENT_DATA_HOLDER_BUTTON = "NPK001_S001_B011";
    String CARD_DELIVERY_BUTTON = "NPK001_S001_B012";
    String PIN_DELIVERY_BUTTON = "NPK001_S001_B013";
    String DESIGN_BUTTON = "NPK001_S001_X026";
    String POTENTIAL_FORMAL_ERROR_BUTTON = "NPK001_S001_B005";
    String CLOSE_POTENTIAL_FORMAL_ERROR_POP_UP_BUTTON = "NPK001_S001_X103";
    String LOG_BUTTON = "XXX001_T001_P102";
    String LOADER_IMG = "CAS001_S001_P001";

    String PRESENCE_OF_CARD_DISABLED_XPATH = "//input[@id='PRESENCE_OF_CARD' and @disabled and not(@checked)]";
    String IS_CARD_TYPE_VISIBLE_XPATH = "//select[@id='NPK001_S001_C002']/option[contains(text(),'CARD_TYPE')]";

    @Select(value = CURRENT_ACCOUNT_SELECT, andWait = @Wait(value = LOAD_IMG_DIV_CLASS, until = Until.GONE, by = Lookup.CLASSNAME, waitSecondsForElement = 60))
    NPK001_S001_DebitCardRequestPage selectCurrentAccount(long currentAccountNumber);

    @Select(value = CURRENT_ACCOUNT_SELECT, andWait = @Wait(value = LOAD_IMG_DIV_CLASS, until = Until.GONE, by = Lookup.CLASSNAME))
    NPK001_S001_DebitCardRequestPage selectCurrentAccountByName(String currentAccountName);

    @Select(value = CURRENT_ACCOUNT_SELECT, selectByOrder = true, andWait = @Wait(value = LOAD_IMG_DIV_CLASS, until = Until.GONE, by = Lookup.CLASSNAME, waitSecondsForElement = 80))
    NPK001_S001_DebitCardRequestPage selectCurrentAccountByIndex(int index);

    @Select(value = CARD_TYPE_SELECT, andWait = @Wait(value = LOAD_IMG_DIV_CLASS, until = Until.GONE, by = Lookup.CLASSNAME, waitSecondsForElement = 60))
    NPK001_S001_DebitCardRequestPage selectDebitCardType(String debitCardType);

    @Select(value = CARD_TYPE_SELECT, andWait = @Wait(value = LOAD_IMG_DIV_CLASS, until = Until.GONE), selectByOrder = true)
    NPK001_S001_DebitCardRequestPage selectDebitCardTypeByIndex(int index);

    @Wait(value = LOAD_IMG_DIV_CLASS, until = Until.GONE, by = Lookup.CLASSNAME)
    @CustomAction
    default NPK001_S001_DebitCardRequestPage clickPresenceOfCardAndPinCardDelivery(String presenceOfCard) {
        UfoEndpoint endpoint = getEndpoint(UfoEndpoint.class);
        if (presenceOfCard.equals("Virtual")) {
            endpoint.jsClick((() -> VIRTUAL));
            endpoint.waitForElementsToLoad(5, Lookup.CLASSNAME, Until.GONE, LOAD_IMG_DIV_CLASS);
        } else {
            endpoint.jsClick((() -> PLASTIC));
            endpoint.click(() -> PIN_DELIVERY_BUTTON, Lookup.ID);
            endpoint.jsClick(() -> PERMANENT_ADDRESS_FOR_PIN_RADIO_BUTTON, Lookup.ID);
            endpoint.acceptAlert(5);
            endpoint.waitForElementsToLoad(5, Lookup.CLASSNAME, Until.GONE, LOAD_IMG_DIV_CLASS);
            endpoint.click(() -> CARD_DELIVERY_BUTTON, Lookup.ID);
            endpoint.isElementVisible(CARD_DELIVERY_BUTTON, Lookup.ID);
            endpoint.jsClick(() -> BRANCH_ADDRESS_RADIO_BUTTON, Lookup.ID);
            endpoint.waitForElementsToLoad(5, Lookup.CLASSNAME, Until.GONE, LOAD_IMG_DIV_CLASS);
        }
        return null;
    }

    @Wait(value = LOAD_IMG_DIV_CLASS, until = Until.GONE, by = Lookup.CLASSNAME)
    @CustomAction
    default NPK001_S001_DebitCardRequestPage checkThatCardCantBeOnboarded(String presenceOfCard, String cardType) {
        UfoEndpoint endpoint = getEndpoint(UfoEndpoint.class);
        if (endpoint.isElementVisible(5, IS_CARD_TYPE_VISIBLE_XPATH.replace("CARD_TYPE", cardType), Lookup.XPATH)) {
            endpoint.selectByVisibleText(CARD_TYPE_SELECT, cardType);
            if (presenceOfCard.equals("Virtual")) {
                endpoint.isElementPresent(PRESENCE_OF_CARD_DISABLED_XPATH.replace("PRESENCE_OF_CARD", VIRTUAL), Lookup.XPATH);
            } else {
                endpoint.isElementPresent(PRESENCE_OF_CARD_DISABLED_XPATH.replace("PRESENCE_OF_CARD", PLASTIC), Lookup.XPATH);
            }
        }
        return null;
    }

    @Select(value = CARD_FEE_PLAN_SELECT, andWait = @Wait(value = LOAD_IMG_DIV_CLASS, until = Until.GONE, by = Lookup.CLASSNAME, waitSecondsForElement = 60))
    NPK001_S001_DebitCardRequestPage selectDebitCardFeePlan(DebitCardFeePlan debitCardFeePlan);

    @Click(value = DESIGN_BUTTON, andWait = @Wait(value = LOAD_IMG_DIV_CLASS, until = Until.GONE, by = Lookup.CLASSNAME))
    NPK001_S001_DebitCardRequestPage clickDesign();

    @Select(value = CARD_FEE_PLAN_SELECT, selectByOrder = true, andWait = @Wait(value = LOAD_IMG_DIV_CLASS, until = Until.GONE, by = Lookup.CLASSNAME))
    NPK001_S001_DebitCardRequestPage selectDebitCardFeePlanByIndex(int feePlanInt);

    @Select(value = CARD_VARIANT_SELECT, andWait = @Wait(value = LOAD_IMG_DIV_CLASS, until = Until.GONE, by = Lookup.CLASSNAME))
    NPK001_S001_DebitCardRequestPage selectCardVariant(String debitCardVariant);

    @TypeInto(value = ACTIVATE_PASSWORD, clear = true, andWait = @Wait(value = LOAD_IMG_DIV_CLASS, until = Until.GONE, by = Lookup.CLASSNAME))
    NPK001_S001_DebitCardRequestPage typeActivatePassword(String activationPassword);

    @Click(value = PERMANENT_ADDRESS_FOR_PIN_RADIO_BUTTON, jsClick = true)
    NPK001_S001_DebitCardRequestPage clickPermanentAddressForPinRadio();

    @Click(value = CLIENT_DATA_HOLDER_BUTTON, by = Lookup.ID, andWait = @Wait(ENTER_BUTTON))
    NPK001_S001_DebitCardRequestPage clickClientDataHolderButton();

    @Click(ENTER_BUTTON)
    NPK001_S001_DebitCardRequestPageTransitions clickEnterButton();

    @Click(value = PIN_DELIVERY_BUTTON, by = Lookup.ID)
    @Click(value = PERMANENT_ADDRESS_FOR_PIN_RADIO_BUTTON, jsClick = true)
    NPK001_S001_DebitCardRequestPage clickPinDeliveryToPermanentAddress();

    @CustomAction
    default NPK001_S001_DebitCardRequestPage dismissPinChargeAlert() {
        UfoEndpoint endpoint = getEndpoint(UfoEndpoint.class);
        endpoint.dismissAlert(10);
        return null;
    }

    @Click(value = CARD_DELIVERY_BUTTON, by = Lookup.ID)
    @Click(value = PERMANENT_ADDRESS_RADIO_BUTTON, jsClick = true)
    NPK001_S001_DebitCardRequestPage clickCardDeliveryToPermanentAddress();

    @Wait(value = LOAD_IMG_DIV_CLASS, until = Until.GONE)
    @Click(SEND_BUTTON)
    NPK001_S001_DebitCardRequestPageTransitions waitForLoadAndClickSend();

    @Wait(value = LOAD_IMG_DIV_CLASS, until = Until.GONE)
    @CustomAction
    default NPK001_S001_DebitCardRequestPageTransitions clickSendAndDismissPossibleAlerts() {
        UfoEndpoint endpoint = getEndpoint(UfoEndpoint.class);
        endpoint.click(() -> SEND_BUTTON);
        //Alert který vzniká při špatném zakliknutí doručení plastu
        if (endpoint.isAlertPresent(3) && endpoint.getAlertText(3).equals("Vyberte adresu pro doručení plastu.")) {
            endpoint.acceptAlert(3);
            endpoint.jsClick(() -> PERMANENT_ADDRESS_RADIO_BUTTON, Lookup.ID);
            if (endpoint.isAlertPresent(3)) {
                endpoint.acceptAlert(3);
            }
            endpoint.click(() -> SEND_BUTTON);
        }
        //Obecný alert, který vyzívá pouze ke kontrole správnosti uvedených informací
        if (endpoint.isAlertPresent(3)) {
            endpoint.acceptAlert(3);
            if (endpoint.isElementVisible(POTENTIAL_FORMAL_ERROR_BUTTON)) {
                endpoint.click(() -> POTENTIAL_FORMAL_ERROR_BUTTON);
                endpoint.click(() -> CLOSE_POTENTIAL_FORMAL_ERROR_POP_UP_BUTTON);
            }
            endpoint.click(() -> SEND_BUTTON);
        }
        return null;
    }

    @CustomAction
    default NPK001_S001_DebitCardRequestPage clickDesignButtonIfSelectIsNotEnabled() {
        UfoEndpoint endpoint = getEndpoint(UfoEndpoint.class);
        if (!endpoint.isElementEnabled(CARD_VARIANT_SELECT, Lookup.ID)) {
            endpoint.click(() -> CARD_DESIGN_BUTTON);
            endpoint.waitForElementsToLoad(5, Lookup.CLASSNAME, Until.GONE, LOAD_IMG_DIV_CLASS);
        }
        return null;
    }

    @CustomAction
    default NPK001_S001_DebitCardRequestPageTransitions clickContinueAndWaitUntilLoaded() {
        UfoEndpoint endpoint = getEndpoint(UfoEndpoint.class);
        endpoint.waitForElementsToLoad(25, Lookup.CLASSNAME, Until.GONE, LOAD_IMG_DIV_CLASS);
        endpoint.click(() -> SEND_BUTTON);
        endpoint.waitForElementsToLoad(20, Until.VISIBLE, LOADER_IMG);
        endpoint.waitForElementsToLoad(150, Until.VISIBLE, LOG_BUTTON);
        return null;
    }

    @Click(SEND_BUTTON)
    NPK001_S001_DebitCardRequestPageTransitions clickSend();
}
