package cz.moneta.test.dsl.payment_engine.parts_of_menu.menu_transaction;

import cz.moneta.test.dsl.payment_engine.enums.ForeignChannelIdentities;
import cz.moneta.test.dsl.payment_engine.home.PaymentEngineHomePage;
import cz.moneta.test.harness.context.StoreAccessor;
import cz.moneta.test.harness.endpoints.payment_engine.PaymentEngineEndpoint;
import cz.moneta.test.harness.support.web.*;

public interface PaymentEngineManualCreateOutgoingXctClientPaymentPage extends WebFlow<PaymentEngineManualCreateOutgoingXctClientPaymentPage>, StoreAccessor {
    String INVALID_ICON = "//ancestor::div[@class='x-form-field-wrap']//following-sibling::div";
    String AMOUNT = "//input[@name = 'amount']";
    String DEBTOR_FORM = "//input[@name = 'orAccIdent']//following-sibling::img";
    String CREDITOR_ACCOUNT = "//input[@name = 'cpAccIdent']";
    String MESSAGE = "//textarea[@name = 'rmtInfUnstr']";
    String SELECT_CHANNEL_ID = "//input[@name = 'channelIdent']//following-sibling::img";
    String SELECT_PRIORITY_OF_TRX = "//input[@name = 'instrPriority']//following-sibling::img";
    String PRIORITY_HIGH = "//div[text()='Prioritní']";
    String CHOOSEN_VALUE = "//div[text() = '%s']";
    String SUBMIT_BUTTON = "//button[text() = 'OK']";
    String EXECUTION_DATE_CALENDAR = "//input[@name = 'receiptDatetime']//following-sibling::img";
    String CALENDAR_TODAY = "//button[text() = 'Dnes']";
    String NO_BUTTON = "//button[text() = 'Ne']";
    String CURRENCY = "//input[@name='curr']//following-sibling::input";
    String INVALID_CURRENCY = CURRENCY + INVALID_ICON;
    String CREDITOR_NAME = "//input[@name='cpName']";
    String BIC = "//input[@name='cpBankBic']";
    String BANK_NAME = "//input[@name='cpBankName']";
    String DEBTOR_FORM_IBAN = "//input[@name='iban']";
    String DEBTOR_FORM_ACCOUNT = "//input[@name='accIdent']";
    String DEBTOR_FORM_SEARCH_BUTTON = "//button[text() = 'Hledat']";
    String DEBTOR_FORM_FIRST_ROW = "//div[@class='x-grid3-cell-inner x-grid3-col-checker']//div";
    String DEBTOR_FORM_OK_BUTTON = "//div[*//span[text()='Vybrat účet příkazce']]//button[text() = 'OK']";
    String CREDITOR_IBAN = "//input[@name='cpAccIban']";

    String TRANSACTION_ID_KEY = "TRANSACTION_ID_KEY";
    String TRANSACTION_ID_XPATH = "//div[@class = 'ext-mb-content']/span[@class = 'ext-mb-text']";

    @TypeIntos({@TypeInto(AMOUNT), @TypeInto(CREDITOR_ACCOUNT), @TypeInto(CREDITOR_IBAN), @TypeInto(MESSAGE),
            @TypeInto(value = CURRENCY, andWait = @Wait(value = INVALID_CURRENCY, until = Until.GONE)),
            @TypeInto(CREDITOR_NAME), @TypeInto(BIC), @TypeInto(BANK_NAME)})
    PaymentEngineManualCreateOutgoingXctClientPaymentPage fillManualData(String amount, String creditorAccount, String creditorIban,
                                                                         String message, String currency, String creditorName,
                                                                         String bic, String bankName);

    @Click(SELECT_CHANNEL_ID)
    PaymentEngineManualCreateOutgoingXctClientPaymentPage clickOnChannelIdentity();

    @Wait(value = CHOOSEN_VALUE)
    @Click(value = CHOOSEN_VALUE)
    PaymentEngineManualCreateOutgoingXctClientPaymentPage selectChannelIdentity(ForeignChannelIdentities foreignChannelIds);

    @Click(value = SELECT_PRIORITY_OF_TRX, andWait = @Wait(PRIORITY_HIGH))
    PaymentEngineManualCreateOutgoingXctClientPaymentPage selectPriorityOfTransaction();

    @Click(PRIORITY_HIGH)
    PaymentEngineManualCreateOutgoingXctClientPaymentPage selectPriorityHigh();

    @Click(value = SUBMIT_BUTTON, andWait = @Wait(value = TRANSACTION_ID_XPATH, until = Until.VISIBLE))
    PaymentEngineManualCreateOutgoingXctClientPaymentPage clickSubmit();

    @Click(value = EXECUTION_DATE_CALENDAR, andWait = @Wait(CALENDAR_TODAY))
    PaymentEngineManualCreateOutgoingXctClientPaymentPage clickToExecutionDateCalendar();

    @Click(CALENDAR_TODAY)
    PaymentEngineManualCreateOutgoingXctClientPaymentPage chooseTodayExecution();

    @StoreElementContent(storeKey = TRANSACTION_ID_KEY, value = TRANSACTION_ID_XPATH, by = Lookup.XPATH)
    PaymentEngineManualCreateOutgoingXctClientPaymentPage storeTransactionId();

    @Wait(value = NO_BUTTON, until = Until.VISIBLE)
    @Click(value = NO_BUTTON, andWait = @Wait(value = NO_BUTTON, until = Until.GONE))
    PaymentEngineHomePage clickNo();

    @CustomAction()
    default PaymentEngineManualCreateOutgoingXctClientPaymentPage selectDebtorByIban(String iban) {
        PaymentEngineEndpoint endpoint = getEndpoint(PaymentEngineEndpoint.class);
        endpoint.click(() -> DEBTOR_FORM);
        endpoint.type(() -> DEBTOR_FORM_IBAN, iban, false);
        endpoint.click(() -> DEBTOR_FORM_SEARCH_BUTTON);
        endpoint.click(() -> DEBTOR_FORM_FIRST_ROW);
        endpoint.click(() -> DEBTOR_FORM_OK_BUTTON);
        return null;
    }

    @CustomAction()
    default PaymentEngineManualCreateOutgoingXctClientPaymentPage selectDebtorByAccount(String account) {
        PaymentEngineEndpoint endpoint = getEndpoint(PaymentEngineEndpoint.class);
        endpoint.click(() -> DEBTOR_FORM);
        endpoint.type(() -> DEBTOR_FORM_ACCOUNT, account, false);
        endpoint.click(() -> DEBTOR_FORM_SEARCH_BUTTON);
        endpoint.click(() -> DEBTOR_FORM_FIRST_ROW);
        endpoint.click(() -> DEBTOR_FORM_OK_BUTTON);
        return null;
    }
}
