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

import cz.moneta.test.dsl.newib.NewIbPageFlow;
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.settings.payments.templates.CreateNewTemplatePage.*;

@Wait(value = {CREATE_NEW_TEMPLATE_TITLE_XPATH, DOMESTIC_TAB_XPATH, FOREIGN_TAB_XPATH}, waitSecondsForElement = 30)
public interface CreateNewTemplatePage extends NewIbPageFlow<CreateNewTemplatePage>, StoreAccessor, Builder {

    String CREATE_NEW_TEMPLATE_TITLE_XPATH = "//h2[@class='t-title t-title--h3'][text()='Vytvořit novou šablonu']";
    String DOMESTIC_TAB_XPATH = "//div[@data-testid='create-domestic-template']";
    String FOREIGN_TAB_XPATH = "//div[@data-testid='create-foreign-template']";
    String TEMPLATE_NAME_INPUT_XPATH = "//input[@id='templateName']";
    String DESTINATION_ACC_PREFIX_INPUT_XPATH = "//input[@id='destinationAccountPrefix']";
    String DESTINATION_ACC_NUMBER_INPUT_XPATH = "//input[@id='destinationAccountNumber']";
    String DESTINATION_ACC_BANK_INPUT_XPATH = "//input[@id='destinationAccountBank']";
    String AMOUNT_INPUT_XPATH = "//input[@id='amount']";
    String VARIABLE_SYMBOL_INPUT_XPATH = "//input[@id='variableSymbol']";
    String CONSTANT_SYMBOL_INPUT_XPATH = "//input[@id='constantSymbol']";
    String SPECIFIC_SYMBOL_INPUT_XPATH = "//input[@id='specificSymbol']";
    String RECIPIENT_MESSAGE_INPUT_XPATH = "//textarea[@id='noteForRecipient']";
    String NOTE_FOR_PAYER_INPUT_XPATH = "//textarea[@id='noteForPayer']";
    String CATEGORY_INPUT_XPATH = "//input[@data-testid='categoryId']";
    String OPTION_XPATH = "//div[@class='f-option__label'][text()='%s']";
    String CREATE_BTN_XPATH = "//button[text()='Vytvořit']";
    String IBAN_INPUT_XPATH = "//input[@id='recipientAccountNumber']";
    String CURRENCY_INPUT_XPATH = "//button[@name='currencyCode']";
    String CHARGE_BTN_XPATH = "//button[@id='chargeBearer']";
    String RECIPIENT_BANK_COUNTRY_INPUT_XPATH = "//input[@name='recipientBankCountry']";
    String RECIPIENT_NAME_INPUT_XPATH = "//input[@id='recipientName']";
    String RECIPIENT_ADDRESS_STREET_INPUT_XPATH = "//input[@id='recipientAddressStreet']";
    String RECIPIENT_ADDRESS_CITY_INPUT_XPATH = "//input[@id='recipientAddressCity']";
    String RECIPIENT_ADDRESS_COUNTRY_INPUT_XPATH = "//input[@name='recipientAddressCountry']";
    String RECIPIENT_NOTE_INPUT_XPATH = "//input[@id='recipientNote']";
    String CONFIRMATION_EMAIL_CHECKBOX_XPATH = "//label[@for='confirmationEmailCheckbox']";
    String CONFIRMATION_EMAIL_INPUT_XPATH = "//input[@id='confirmationEmail']";

    @CustomAction
    default CreateNewTemplatePage setCategory(String category) {
        NewIbEndpoint newIbEndpoint = getEndpoint(NewIbEndpoint.class);
        newIbEndpoint.click(() -> CATEGORY_INPUT_XPATH);
        newIbEndpoint.sleepSeconds(1);
        newIbEndpoint.click(() -> String.format(OPTION_XPATH, category));
        newIbEndpoint.waitForElementsToLoad(5, String.format(OPTION_XPATH, category));
        return null;
    }

    @CustomAction
    default CreateNewTemplatePage setCurrency(String currency) {
        NewIbEndpoint newIbEndpoint = getEndpoint(NewIbEndpoint.class);
        newIbEndpoint.click(() -> CURRENCY_INPUT_XPATH);
        newIbEndpoint.sleepSeconds(1);
        newIbEndpoint.click(() -> String.format(OPTION_XPATH, currency));
        newIbEndpoint.waitForElementsToLoad(5, String.format(OPTION_XPATH, currency));
        return null;
    }

    @CustomAction
    default CreateNewTemplatePage setFees(String fees) {
        NewIbEndpoint newIbEndpoint = getEndpoint(NewIbEndpoint.class);
        newIbEndpoint.click(() -> CHARGE_BTN_XPATH);
        newIbEndpoint.sleepSeconds(1);
        newIbEndpoint.click(() -> String.format(OPTION_XPATH, fees));
        newIbEndpoint.waitForElementsToLoad(5, String.format(OPTION_XPATH, fees));
        return null;
    }

    @CustomAction
    default CreateNewTemplatePage setRecipientBankCountry(String recipientBankCountry) {
        NewIbEndpoint newIbEndpoint = getEndpoint(NewIbEndpoint.class);
        newIbEndpoint.click(() -> RECIPIENT_BANK_COUNTRY_INPUT_XPATH);
        newIbEndpoint.sleepSeconds(1);
        newIbEndpoint.click(() -> String.format(OPTION_XPATH, recipientBankCountry));
        newIbEndpoint.waitForElementsToLoad(5, String.format(OPTION_XPATH, recipientBankCountry));
        return null;
    }

    @CustomAction
    default CreateNewTemplatePage setRecipientAddressCountry(String recipientAddressCountry) {
        NewIbEndpoint newIbEndpoint = getEndpoint(NewIbEndpoint.class);
        newIbEndpoint.click(() -> RECIPIENT_ADDRESS_COUNTRY_INPUT_XPATH);
        newIbEndpoint.sleepSeconds(1);
        newIbEndpoint.click(() -> String.format(OPTION_XPATH, recipientAddressCountry));
        newIbEndpoint.waitForElementsToLoad(5, String.format(OPTION_XPATH, recipientAddressCountry));
        return null;
    }

    @TypeInto(TEMPLATE_NAME_INPUT_XPATH)
    CreateNewTemplatePage setTemplateName(String templateName);

    @TypeInto(DESTINATION_ACC_PREFIX_INPUT_XPATH)
    CreateNewTemplatePage setDestinationAccPrefix(String prefixAccountNumber);

    @TypeInto(DESTINATION_ACC_NUMBER_INPUT_XPATH)
    CreateNewTemplatePage setDestinationAccNumber(String recipientAccountNumber);

    @CustomAction
    default CreateNewTemplatePage setDestinationAccBank(String bankCode) {
        NewIbEndpoint newIbEndpoint = getEndpoint(NewIbEndpoint.class);
        newIbEndpoint.type(() -> DESTINATION_ACC_BANK_INPUT_XPATH, bankCode, true);
        newIbEndpoint.sendKeysOneAtATime(Key.ENTER);
        return null;
    }

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

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

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

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

    @TypeInto(RECIPIENT_MESSAGE_INPUT_XPATH)
    CreateNewTemplatePage setRecipientMessage(String recipientMessage);

    @TypeInto(NOTE_FOR_PAYER_INPUT_XPATH)
    CreateNewTemplatePage setNoteForPayer(String myNote);

    @Click(CREATE_BTN_XPATH)
    TemplatesVictoryPage clickOnCreate();

    @TypeInto(IBAN_INPUT_XPATH)
    CreateNewTemplatePage setIban(String iban);

    @TypeInto(RECIPIENT_NAME_INPUT_XPATH)
    CreateNewTemplatePage setRecipientName(String recipientName);

    @TypeInto(RECIPIENT_ADDRESS_STREET_INPUT_XPATH)
    CreateNewTemplatePage setRecipientAddressStreet(String recipientAddressStreet);

    @TypeInto(RECIPIENT_ADDRESS_CITY_INPUT_XPATH)
    CreateNewTemplatePage setRecipientAddressCity(String recipientAddressCity);

    @TypeInto(RECIPIENT_NOTE_INPUT_XPATH)
    CreateNewTemplatePage setRecipientNote(String recipientMessage);

    @Click(CONFIRMATION_EMAIL_CHECKBOX_XPATH)
    CreateNewTemplatePage clickOnConfirmationEmailCheckbox();

    @TypeInto(value = CONFIRMATION_EMAIL_INPUT_XPATH, andWait = @Wait(explicitWaitSeconds = 10))
    CreateNewTemplatePage setConfirmationEmail(String confirmationEmail);
}
