package cz.moneta.test.dsl.newib.payments;

import cz.moneta.test.dsl.newib.NewIbPageFlow;
import cz.moneta.test.dsl.newib.components.HorizontalMainMenu;
import cz.moneta.test.harness.context.StoreAccessor;
import cz.moneta.test.harness.endpoints.ib.NewIbEndpoint;
import cz.moneta.test.harness.support.web.*;

import static cz.moneta.test.dsl.newib.payments.StandingOrderPage.*;

@Wait(value = {DETAIL_INFORMATION_XPATH, FROM_ACCOUNT_TEXT_XPATH, ACCOUNT_NUMBER_INPUT_XPATH, BANK_CODE_INPUT_XPATH, AMOUNT_INPUT_XPATH}, waitSecondsForElement = 30)
public interface StandingOrderPage extends NewIbPageFlow<StandingOrderPage>, NewPaymentMenuPage, HorizontalMainMenu , StoreAccessor {
    String DETAIL_INFORMATION_XPATH = "//span[contains(text(), 'Symboly, zpráva pro příjemce, poznámka a další')]";
    String FROM_ACCOUNT_TEXT_XPATH = "//label[@for='sourceAccountId_button'][text()='Z účtu']";
    String ACCOUNT_NUMBER_INPUT_XPATH = "//input[@id='destinationAccountNumber']";
    String ACCOUNT_NUMBER_PREFIX_INPUT_XPATH = "//input[@data-testid='destinationAccountPrefix']";
    String BANK_CODE_BUTTON_XPATH = "//button[@name='destinationAccountBank']";
    String BANK_CODE_INPUT_XPATH = "//input[@name='destinationAccountBank']";
    String NAME_OF_THE_STANDING_ORDER_INPUT_XPATH = "//input[@id='description']";
    String AMOUNT_INPUT_XPATH = "//input[@name='amount']";
    String DATE_OF_LAST_PAYMENT_INPUT_XPATH = "//input[@name='endDate']";
    String FREQUENCY_BTN_XPATH = "//button[@id='frequency_button']";
    String FREQUENCY_OPTION_XPATH = "//li[@role='option']//div[contains(@class, 'c-dropdown__itemContent') and normalize-space(text())='%s']";
    String VARIABLE_SYMBOL_INPUT_XPATH = "//input[@name='variableSymbol']";
    String CONSTANT_SYMBOL_INPUT_XPATH = "//input[@name='constantSymbol']";
    String SPECIFIC_SYMBOL_INPUT_XPATH = "//input[@name='specificSymbol']";
    String MESSAGE_RECIPIENT_INPUT_XPATH = "//textarea[@name='noteForRecipient']";
    String CONTINUE_BUTTON_XPATH = "//button[@data-testid='submit']";

    @TypeInto(ACCOUNT_NUMBER_INPUT_XPATH)
    StandingOrderPage setAccountNumber(String accountNumber);

    @TypeInto(ACCOUNT_NUMBER_PREFIX_INPUT_XPATH)
    StandingOrderPage setAccountNumberPrefix(String accountNumberPrefix);

    @CustomAction
    default StandingOrderPage setBankCode(String bankCode) {
        NewIbEndpoint newIbEndpoint = getEndpoint(NewIbEndpoint.class);
        newIbEndpoint.click(() -> BANK_CODE_BUTTON_XPATH);
        newIbEndpoint.type(() -> BANK_CODE_INPUT_XPATH, bankCode, true);
        newIbEndpoint.sendKeysOneAtATime(Key.ENTER);
        return null;
    }

    @TypeInto(NAME_OF_THE_STANDING_ORDER_INPUT_XPATH)
    StandingOrderPage setNameOfTheStandingOrder(String nameOfTheStandingOrder);

    @TypeInto(AMOUNT_INPUT_XPATH)
    StandingOrderPage setAmount(String amount);

    @TypeInto(DATE_OF_LAST_PAYMENT_INPUT_XPATH)
    StandingOrderPage setDateOfLastPayment(String dateOfLastPayment);

    @CustomAction
    default StandingOrderPage setFrequency(String frequency) {
        NewIbEndpoint newIbEndpoint = getEndpoint(NewIbEndpoint.class);
        newIbEndpoint.click(() -> FREQUENCY_BTN_XPATH);
        newIbEndpoint.sleepSeconds(1);
        newIbEndpoint.click(() -> String.format(FREQUENCY_OPTION_XPATH, frequency));
        return null;
    }

    @TypeInto(VARIABLE_SYMBOL_INPUT_XPATH)
    StandingOrderPage setVariableSymbol(String variableSymbol);

    @TypeInto(CONSTANT_SYMBOL_INPUT_XPATH)
    StandingOrderPage setConstantSymbol(String constantSymbol);

    @TypeInto(SPECIFIC_SYMBOL_INPUT_XPATH)
    StandingOrderPage setSpecificSymbol(String specificSymbol);

    @TypeInto(MESSAGE_RECIPIENT_INPUT_XPATH)
    StandingOrderPage setRecipientMessage(String recipientMessage);

    @Click(CONTINUE_BUTTON_XPATH)
    StandingOrderSummaryPage clickOnContinue();
}
