package cz.moneta.test.dsl.ufo.banka.pages.products.current.account;

import cz.moneta.test.dsl.Harness;
import cz.moneta.test.dsl.ufo.banka.pages.main.PSP004_S001_PrintPage;
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.current.account.ZPL006_S001_ForeignPaymentInputPage.SAVE_PAYMENT_BUTTON;
import static cz.moneta.test.dsl.ufo.banka.pages.products.current.account.ZPL006_S001_ForeignPaymentInputPage.ZPL006_S001_PAGE_LABEL_XPATH;
import static cz.moneta.test.dsl.ufo.operations.shared.SharedElements.LOAD_IMG_DIV_CLASS;

@Wait(value = ZPL006_S001_PAGE_LABEL_XPATH, by = Lookup.XPATH, waitSecondsForElement = 90)
@Wait(value = SAVE_PAYMENT_BUTTON, waitSecondsForElement = 90)
public interface ZPL006_S001_ForeignPaymentInputPage extends WebFlow<ZPL006_S001_ForeignPaymentInputPage>, StoreAccessor {

    String ZPL006_S001_PAGE_LABEL_XPATH = "//div[text()='ZPL006_S001']";
    String IDENTIFICATION_BANK_CODE_KEY = "IDENTIFICATION_BANK_CODE";
    String CURRENCY_SELECT = "ZPL006_S001_C002";
    String AMOUNT_INPUT = "ZPL006_S001_E014";
    String NO_INDIVIDUAL_EXCHANGE_RATE_RADIO = "ZPL006_S001_R004_0";
    String RECEIVER_ACC_INPUT = "ZPL006_S001_E018";
    String RECEIVER_NAME_INPUT = "ZPL006_S001_E003";
    String RECEIVER_ADRESS_STREET_INPUT = "ZPL006_S001_E001";
    String RECEIVER_ADRESS_CITY_INPUT = "ZPL006_S001_E002";
    String RECEIVER_ADRESS_COUNTRY_CODE_INPUT = "ZPL006_S001_E047";
    String IDENTIFICATION_BANK_CODE_INPUT = "ZPL006_S001_E013";
    String SAVE_PAYMENT_BUTTON = "ZPL006_S001_B007";

    @CustomAction
    default ZPL006_S001_ForeignPaymentInputPage dismissInformationAlert() {
        UfoEndpoint endpoint = getEndpoint(UfoEndpoint.class);
        endpoint.dismissAlert(20);
        return null;
    }

    @Select(value = CURRENCY_SELECT, andWait = @Wait(value = LOAD_IMG_DIV_CLASS, by = Lookup.CLASSNAME, until = Until.GONE))
    ZPL006_S001_ForeignPaymentInputPage selectCurrency(String currency);

    @TypeInto(value = AMOUNT_INPUT, andWait = @Wait(value = LOAD_IMG_DIV_CLASS, by = Lookup.CLASSNAME, until = Until.GONE))
    ZPL006_S001_ForeignPaymentInputPage fillAmount(String amount);

    @Click(value =NO_INDIVIDUAL_EXCHANGE_RATE_RADIO, andWait = @Wait(value = LOAD_IMG_DIV_CLASS, by = Lookup.CLASSNAME, until = Until.GONE))
    ZPL006_S001_ForeignPaymentInputPage clickNo();

    @TypeInto(RECEIVER_ACC_INPUT)
    ZPL006_S001_ForeignPaymentInputPage fillAccount(String accountNumber);

    @CustomAction
    default ZPL006_S001_ForeignPaymentInputPage fillAccountAndWaitUntilLoaded(String accountNumber) {
        UfoEndpoint endpoint = getEndpoint(UfoEndpoint.class);
        endpoint.type(() -> RECEIVER_ACC_INPUT, accountNumber, true);
        endpoint.click(() -> RECEIVER_NAME_INPUT, Lookup.ID);
        endpoint.waitForElementsToLoad(10, Lookup.CLASSNAME, Until.GONE, LOAD_IMG_DIV_CLASS);
        return null;
    }

    @TypeInto(value = RECEIVER_NAME_INPUT, andWait = @Wait(value = LOAD_IMG_DIV_CLASS, by = Lookup.CLASSNAME, until = Until.GONE))
    ZPL006_S001_ForeignPaymentInputPage fillReceiverName(String receiverName);

    @TypeInto(value = RECEIVER_ADRESS_STREET_INPUT, andWait = @Wait(value = LOAD_IMG_DIV_CLASS, by = Lookup.CLASSNAME, until = Until.GONE))
    ZPL006_S001_ForeignPaymentInputPage fillReceiverAdressStreet(String receiverAdressStreet);

    @TypeInto(value = RECEIVER_ADRESS_CITY_INPUT, andWait = @Wait(value = LOAD_IMG_DIV_CLASS, by = Lookup.CLASSNAME, until = Until.GONE))
    ZPL006_S001_ForeignPaymentInputPage fillReceiverAdressCity(String receiverAdressCity);

    @TypeInto(value = RECEIVER_ADRESS_COUNTRY_CODE_INPUT, andWait = @Wait(value = LOAD_IMG_DIV_CLASS, by = Lookup.CLASSNAME, until = Until.GONE))
    ZPL006_S001_ForeignPaymentInputPage fillReceiverAdressCountryCode(String receiverAdressCountryCode);

    @Wait(value = LOAD_IMG_DIV_CLASS, until = Until.GONE, waitSecondsForElement = 60, by = Lookup.CLASSNAME)
    @Click(SAVE_PAYMENT_BUTTON)
    ZPL_OBR001_SmoothPaymentInfo saveCreatedForeignPayment();

    @Wait(value = LOAD_IMG_DIV_CLASS, until = Until.GONE, waitSecondsForElement = 60, by = Lookup.CLASSNAME)
    @CustomAction
    default PSP004_S001_PrintPage saveCreatedForeignPaymentAndDismissAllAlertsIfPresent() {
        UfoEndpoint endpoint = getEndpoint(UfoEndpoint.class);
        endpoint.click(() -> SAVE_PAYMENT_BUTTON);
        for (int i = 0; i < 3; i++) {
            if (endpoint.isAlertPresent(3)) {
                endpoint.dismissAlert(1);
            } else break;
        }
        return null;
    }

    @CustomAction
    default ZPL006_S001_ForeignPaymentInputPage storeIdentificationBankCode() {
        UfoEndpoint endpoint = getEndpoint(UfoEndpoint.class);
        store(IDENTIFICATION_BANK_CODE_KEY, endpoint.getAttribute(IDENTIFICATION_BANK_CODE_INPUT, "value"));
        return null;
    }

}