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

import cz.moneta.test.dsl.Harness;
import cz.moneta.test.dsl.newib.NewIbPageFlow;
import cz.moneta.test.dsl.newib.components.VerticalMainMenu;
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.NewPaymentDomesticPage.*;
@Wait(value = {DETAIL_INFORMATION_XPATH, FROM_ACCOUNT_TEXT_XPATH, NUMBER_ACCOUNT_XPATH, BANK_CODE_XPATH, AMOUNT_XPATH}, waitSecondsForElement = 30)
public interface NewPaymentDomesticPage extends NewIbPageFlow<NewPaymentDomesticPage>, NewPaymentMenuPage, StoreAccessor, VerticalMainMenu {
    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 FROM_ACCOUNT_BUTTON_XPATH = "//button[@id='sourceAccountId_button']";
    String NUMBER_ACCOUNT_XPATH = "//label[@for='destinationAccountNumber']//following-sibling::div//input[@data-testid='destinationAccountNumber']";
    String BANK_CODE_XPATH = "//button[@id='destinationAccountBank_button']";//"//input[@id='destinationAccountBank_input']";
    String BANK_CODE_MENU_ITEM_XPATH = "//li[@class='c-dropdown__item']//div[text()='%s']";
    String AMOUNT_XPATH = "//label[@for='amount']//following-sibling::div//input[@id='amount']";
    String TYPE_OF_PAYMENT_XPATH = "//button[@id='paymentPriority_button']";
    String CHOOSE_TYPE_OF_PAYMENT_XPATH = "//button[@id='paymentPriority_button']//span[contains(@class, 'f-control2__valueTitle')]";
    String OPTION_TYPE_OF_PAYMENT_XPATH = "//li[@role='option' and not(@disabled) and .//div[text()='%s']]";
    String VARIABLE_SYMBOL_XPATH = "//label[@for='variableSymbol']//following-sibling::div//input[@id='variableSymbol']";
    String CONSTANT_SYMBOL_XPATH = "//label[@for='constantSymbol']//following-sibling::div//input[@id='constantSymbol']";
    String SPECIFIC_SYMBOL_XPATH = "//label[@for='specificSymbol']//following-sibling::div//input[@id='specificSymbol']";
    String MESSAGE_RECIPIENT_XPATH = "//div[@class='c-collapse__content']//following-sibling::div//textarea[@id='noteForRecipient']";
    String NOTE_FOR_ME_XPATH = "//div[@class='c-collapse__content']//following-sibling::div//textarea[@id='noteForPayer']";
    String CONTINUE_BUTTON_XPATH = "//button[@data-testid='domestic-payment-continue']";
    String CLIENT_ACCOUNT_NUMBER = "CLIENT_ACCOUNT_NUMBER";
    String CURRENCY_XPATH = "//button[@id='currencyCode_button']";
    String CURRENCY_MENU_ITEM_XPATH = "//li[@class='c-dropdown__item']//div[text()='%s']";
    String ESTIMATED_AMOUNT_XPATH  = "//small[contains(., 'Orientační částka transakce je')]";
    String FEE_XPATH = "//small[contains(., 'Poplatek za konverzi je')]";
    String SUBMIT_XPATH = "//small[contains(., 'Odešlete o')]";
    String INDIVIDUAL_RATE_ON_XPATH = "//span[@class='f-checkbox__indicator']";
    String INDIVIDUAL_RATE_OFF_XPATH = "//span[@class='c-avatar c-avatar--tickFill c-avatar--transparent']";
    String POPUP_TITLE_XPATH = "//h2[text()='Individuální směnný kurz']";
    String INDVIDUAL_INFO_XPATH = "//small[text()='Dostatek prostředků pro umožnění platby je počítán podle platného kurzovního lístku banky. Skutečně odečtené prostředky se budou lišit dle smluveného individuálního kurzu.']";
    String DECLINE_XPATH = "//button[@data-testid='declineRate']";
    String CONFIRM_XPATH = "//button[@data-testid='confirmRate']";
    String DUE_DATE_XPATH = "//input[@data-testid='Component'][@name='dueDate']";
    String FIRST_ACCOUNT_XPATH = "//li[@role='option' and .//div[@class='c-dropdown__itemContent' and contains(., 'Běžný účet CZK')]]";
    String SECOND_ACCOUNT_XPATH = "//li[@role='option' and .//div[@class='c-dropdown__itemContent' and contains(., 'Spořicí účet CZK')]]";
    String MY_ACCOUNTS_XPATH = "//button[@id='template_button']";
    String FIRST_MY_ACCOUNT_XPATH = "//li[@role='option' and .//div[@class='c-dropdown__itemContent' and contains(., 'Spořicí účet CZK')]]";
    String PRIORITY_PAYMENT_TYPE_DISABLED_XPATH = "//div[@class='c-dropdown__itemContent' and contains(., 'Prioritní') and contains(., 'Mezi účty MONETA Money Bank prioritní platbu nelze poslat.')]";
    String CATEGORY_BUTTON_XPATH = "//div[@data-testid='categoryId']";
    String SELECT_CATEGORY_XPATH = "//div[@class='c-dropdown__itemContent' and text()='%s']";
    String SELECT_FROM_ACCOUNT_XPATH = "//li[@role='option' and .//div[@class='c-dropdown__itemContent' and contains(., 'Běžný účet CZK')]]";
    String TEMPLATE_CHECKBOX_XPATH = "//span[@class='f-checkbox__over']/span[@class='f-checkbox__indicator']";
    String TEMPLATE_NAME_XPATH = "//div[@class='f-controlWrap']//input[@id='createTemplateName']";
    String SELECT_TEMPLATE_XPATH = "//ul[@id='template_list-1']//div[@class='c-dropdown__itemContent' and contains(normalize-space(.), '%s')]";
    String FOREIGN_BTN_XPATH = "//span[@data-testid='foreign-tab']";
    String DESTINATION_ACCOUNT_NUMBER_XPATH = "//input[@id='destinationAccountNumber']";
    String DESTINATION_ACCOUNT_BANK_XPATH = "//button[@id='destinationAccountBank_button']";
    String SELECT_SAVINGS_ACCOUNT_XPATH = "//ul[@id='template_list-0']//div[@class='c-dropdown__itemContent' and contains(normalize-space(.), 'Spořicí účet CZK')]";
    String NEW_PAYMENT_TITLE_XPATH = "//h1[text()='Nová platba']";

    @TypeInto(value = NUMBER_ACCOUNT_XPATH, clear = true)
    NewPaymentDomesticPage fillNumberOfAccount(String numberOfAccount);

    @CustomAction
    default NewPaymentDomesticPage fillBankCode(String bankCode) {
        NewIbEndpoint newIbEndpoint = getEndpoint(NewIbEndpoint.class);
        newIbEndpoint.click(() -> "//body");
        newIbEndpoint.click(() -> BANK_CODE_XPATH);
        newIbEndpoint.click(() -> String.format(BANK_CODE_MENU_ITEM_XPATH, bankCode));
        return null;
    }

    @CustomAction
    default NewPaymentDomesticPage getAccountNumber(Harness harness) {
        NewIbEndpoint newIbEndpoint = getEndpoint(NewIbEndpoint.class);
        String accountNumber = newIbEndpoint.getAttribute(FROM_ACCOUNT_BUTTON_XPATH, "data-value");
        harness.store(CLIENT_ACCOUNT_NUMBER, accountNumber);
        return null;
    }

    @TypeInto(AMOUNT_XPATH)
    NewPaymentDomesticPage fillAmount(String amount);

    @CustomAction
    default NewPaymentDomesticPage selectTypeOfPayment(String name) {
        NewIbEndpoint newIbEndpoint = getEndpoint(NewIbEndpoint.class);
        newIbEndpoint.click(() -> TYPE_OF_PAYMENT_XPATH);
        newIbEndpoint.sleepSeconds(1);
        newIbEndpoint.click(() -> String.format(OPTION_TYPE_OF_PAYMENT_XPATH, name));
        newIbEndpoint.waitForElementsToLoad(5, String.format(CHOOSE_TYPE_OF_PAYMENT_XPATH, name));
        return null;
    }

    @TypeInto(value = VARIABLE_SYMBOL_XPATH)
    NewPaymentDomesticPage fillVariableSymbol(String variableSymbol);

    @TypeInto(value = CONSTANT_SYMBOL_XPATH)
    NewPaymentDomesticPage fillConstantSymbol(String constantSymbol);

    @TypeInto(value = SPECIFIC_SYMBOL_XPATH)
    NewPaymentDomesticPage fillSpecificSymbol(String specificSymbol);

    @TypeInto(value = MESSAGE_RECIPIENT_XPATH)
    NewPaymentDomesticPage fillRecipientMessage(String recipientMessage);

    @TypeInto(value = NOTE_FOR_ME_XPATH)
    NewPaymentDomesticPage fillMyNote(String myNote);

    @Wait(value = CONTINUE_BUTTON_XPATH, until = Until.CLICKABLE)
    @Click(CONTINUE_BUTTON_XPATH)
    NewPaymentSummaryPage clickContinue();

    @CustomAction
    default NewPaymentDomesticPage fillCurrency(String currency) {
        NewIbEndpoint newIbEndpoint = getEndpoint(NewIbEndpoint.class);
        newIbEndpoint.click(() -> CURRENCY_XPATH);
        newIbEndpoint.click(() -> String.format(CURRENCY_MENU_ITEM_XPATH, currency));
        return null;
    }

    @Click(INDIVIDUAL_RATE_ON_XPATH)
    NewPaymentDomesticPage clickOnIndividualRateOn();

    @Click(INDIVIDUAL_RATE_OFF_XPATH)
    NewPaymentDomesticPage clickOnIndividualRateOff();

    @CheckElementPresent(INDVIDUAL_INFO_XPATH)
    NewPaymentDomesticPage checkIndividualInfoExist();

    @CheckElementPresent(POPUP_TITLE_XPATH)
    NewPaymentDomesticPage checkIndividualPopupExist();

    @Click(DECLINE_XPATH)
    NewPaymentDomesticPage clickOnDecline();

    @Click(CONFIRM_XPATH)
    NewPaymentSummaryPage clickOnConfirm();

    @Click(CONTINUE_BUTTON_XPATH)
    NewPaymentDomesticPage clickContinueIndividual();

    @CheckElementsPresent(value = {
            @CheckElementPresent(ESTIMATED_AMOUNT_XPATH),
            @CheckElementPresent(FEE_XPATH),
            @CheckElementPresent(SUBMIT_XPATH)})
    NewPaymentDomesticPage checkAllInfoBlocksPresent();

    @TypeInto(value = DUE_DATE_XPATH, clear = true)
    NewPaymentDomesticPage fillDate (String dateOfPayment);

    @Click(FROM_ACCOUNT_BUTTON_XPATH)
    NewPaymentDomesticPage clickOnFromAccount();

    @CheckElementsPresent(value = {
            @CheckElementPresent(FIRST_ACCOUNT_XPATH),
            @CheckElementPresent(SECOND_ACCOUNT_XPATH)})
    NewPaymentDomesticPage checkFromAccount();

    @Click(SELECT_FROM_ACCOUNT_XPATH)
    NewPaymentDomesticPage selectFromAccount();

    @Click(TYPE_OF_PAYMENT_XPATH)
    NewPaymentDomesticPage clickOnTypeOfPayment();

    @CheckElementPresent(PRIORITY_PAYMENT_TYPE_DISABLED_XPATH)
    NewPaymentDomesticPage checkPriorityPaymentTypeDisabled();

    @CustomAction
    default NewPaymentDomesticPage selectCategory(String category) {
        NewIbEndpoint newIbEndpoint = getEndpoint(NewIbEndpoint.class);
        newIbEndpoint.click(() -> CATEGORY_BUTTON_XPATH);
        newIbEndpoint.sleepSeconds(1);
        newIbEndpoint.click(() -> String.format(SELECT_CATEGORY_XPATH, category));
        return null;
    }

    @Click(TEMPLATE_CHECKBOX_XPATH)
    NewPaymentDomesticPage selectSaveAsTemplateCheckbox();

    @TypeInto(value = TEMPLATE_NAME_XPATH)
    NewPaymentDomesticPage fillTemplateName(String templateName);

    @Click(MY_ACCOUNTS_XPATH)
    NewPaymentDomesticPage clickOnMyAccounts();

    @Click(value = SELECT_TEMPLATE_XPATH, isStringDynamicXpath = true)
    NewPaymentDomesticPage selectTemplate(String templateName);

    @Click(FOREIGN_BTN_XPATH)
    NewForeignPaymentPage clickOnForeignTab();

    @CheckElementPresent(DESTINATION_ACCOUNT_NUMBER_XPATH)
    NewPaymentDomesticPage checkDestinationAccountNumber(String number);

    @CheckElementPresent(DESTINATION_ACCOUNT_BANK_XPATH)
    NewPaymentDomesticPage checkDestinationAccountBank(String number);

    @Click(value = SELECT_SAVINGS_ACCOUNT_XPATH)
    NewPaymentDomesticPage selectSavingsAccount();

    @CheckElementPresent(NEW_PAYMENT_TITLE_XPATH)
    NewPaymentDomesticPage checkNewPaymentTitlePresent();

    @CustomAction
    default NewPaymentDomesticPage scrollToBottom() {
        NewIbEndpoint ep = getEndpoint(NewIbEndpoint.class);
        ep.sendKeysOneAtATime(Key.DOWN);
        ep.sendKeysOneAtATime(Key.END);
        return null;
    }
}