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

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.messages.NewMessagePage.*;

@Wait(value = {SUBJECT_INPUT_XPATH, MESSAGE_INPUT_XPATH, TOPIC_BTN_XPATH}, waitSecondsForElement = 30)
public interface NewMessagePage extends NewIbPageFlow<NewMessagePage>, StoreAccessor, Builder {

    String SUBJECT_INPUT_XPATH = "//input[@id='subject']";
    String TOPIC_BTN_XPATH = "//button[@class='f-control2 f-control2--select'][@name='type']";
    String TOPIC_OPTION_XPATH = "//div[@class='c-dropdown__itemContent'][text()='%s']";
    String MESSAGE_INPUT_XPATH = "//textarea[@class='f-control2 f-control2--textarea f-control2__placeholder']";
    String SEND_MESSAGE_BTN_XPATH = "//button[@data-testid='submit' and text()='Odeslat zprávu']";
    String AMOUNT_INPUT_XPATH = "//label[@for='amount']//following::input[@id='amount']";
    String WITHDRAWAL_DATE_INPUT_XPATH = "//input[@id='dateTime']";
    String CLIENT_PHONE_NUMBER_INPUT_XPATH = "//input[@id='clientPhoneNumber']";
    String BRANCH_XPATH = "//button[@id='branchId_button']";
    String BRANCH_OPTION_XPATH = "//div[@class='c-dropdown__itemContent'][text()='%s']";
    String AGREEMENT_CHECKBOX_XPATH = "//label[@for='agreement']";
    String AGREEMENT_CHECKBOX_OVERLIMIT_WITHDRAW_XPATH = "//input[@id='agreement']//following::span[contains(text(), 'Potvrzuji')]";
    String IBAN_INPUT_XPATH = "//input[@id='destinationAccountIban']";
    String CURRENCY_BTN_XPATH = "//div[@data-testid='currency']";
    String CURRENCY_OPTION_XPATH = "//div[@class='c-dropdown__itemContent'][text()='%s']";
    String DUE_DATE_INPUT_XPATH = "//input[@id='dueDate']";
    String TRANSACTION_DATE_INPUT_XPATH = "//input[@id='dueDate']";
    String DESTINATION_ACC_NUMBER_INPUT_XPATH = "//input[@id='destinationAccountNumber']";
    String BANK_CODE_INPUT_XPATH = "//input[@id='destinationAccountBank_input']";
    String BANK_CODE_BUTTON_XPATH = "//div[@data-testid='destinationAccountBank-select']";
    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 ACC_NUMBER_FOR_REFUND_INPUT_XPATH = "//input[@id='refundedAccountNumber']";
    String BANK_CODE_FOR_REFUND_INPUT_XPATH = "//input[@id='refundedAccountBank_input']";
    String BANK_CODE_FOR_REFUND_BUTTON_XPATH = "//div[@data-testid='refundedAccountBank-select']";
    String PHONE_NUMBER_FOR_REFUND_INPUT_XPATH = "//input[@id='phoneNumber']";
    String RECIPIENT_MESSAGE_INPUT_XPATH = "//input[@id='text']";
    String REPEATED_REQUEST_CHECKBOX_XPATH = "//label[@for='urgentMessage']";
    String TYPE_OF_COMPLAINTS_AND_CLAIMS = "//button[@id='subtype_button']";
    String COMPLAINTS_CLAIMS_OPTION = "//div[@class='c-dropdown__itemContent'][text()='%s']";
    String TITLE_TOPIC_XPATH = "//span[@class='f-control2__valueTitle']";
    String SUBJECT_OVERDRAFT_DISABLED_XPATH = "//input[@value='Nadlimitní výběr (= výběr nad 100.000 Kč nebo ekvivalent v cizí měně)'][@disabled]";

    @TypeInto(SUBJECT_INPUT_XPATH)
    NewMessagePage setSubject(String subject);

    @TypeInto(MESSAGE_INPUT_XPATH)
    NewMessagePage setMessage(String message);

    @CustomAction
    default NewMessagePage setTopic(String topic) {
        NewIbEndpoint newIbEndpoint = getEndpoint(NewIbEndpoint.class);
        newIbEndpoint.click(() -> TOPIC_BTN_XPATH);
        newIbEndpoint.sleepSeconds(1);
        newIbEndpoint.click(() -> String.format(TOPIC_OPTION_XPATH, topic));
        return null;
    }

    @CustomAction
    default NewMessagePage setTypeOfComplaintsAndClaims(String typeOfComplaintsAndClaims) {
        NewIbEndpoint newIbEndpoint = getEndpoint(NewIbEndpoint.class);
        newIbEndpoint.click(() -> TYPE_OF_COMPLAINTS_AND_CLAIMS);
        newIbEndpoint.sleepSeconds(1);
        newIbEndpoint.click(() -> String.format(COMPLAINTS_CLAIMS_OPTION, typeOfComplaintsAndClaims));
        return null;
    }

    @CustomAction
    default NewMessagePage setCurrency(String currency) {
        NewIbEndpoint newIbEndpoint = getEndpoint(NewIbEndpoint.class);
        newIbEndpoint.click(() -> CURRENCY_BTN_XPATH);
        newIbEndpoint.sleepSeconds(1);
        newIbEndpoint.click(() -> String.format(CURRENCY_OPTION_XPATH, currency));
        return null;
    }

    @Click(value = SEND_MESSAGE_BTN_XPATH, andWait = @Wait(explicitWaitSeconds = 1))
    NewMessageVictoryPage clickOnSendMessage();

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

    @TypeInto(WITHDRAWAL_DATE_INPUT_XPATH)
    NewMessagePage setWithdrawalDate(String withdrawalDate);

    @TypeInto(CLIENT_PHONE_NUMBER_INPUT_XPATH)
    NewMessagePage setClientPhoneNumber(String clientPhoneNumber);

    @Click(AGREEMENT_CHECKBOX_XPATH)
    NewMessagePage clickOnAgreement();

    @Click(AGREEMENT_CHECKBOX_OVERLIMIT_WITHDRAW_XPATH)
    NewMessagePage clickOnAgreementW();

    @Click(REPEATED_REQUEST_CHECKBOX_XPATH)
    NewMessagePage clickOnUrgent();

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

    @TypeInto(DUE_DATE_INPUT_XPATH)
    NewMessagePage setDueDate(String dueDate);

    @TypeInto(TRANSACTION_DATE_INPUT_XPATH)
    NewMessagePage setTransactionDate(String transactionDate);

    @CustomAction
    default NewMessagePage 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;
    }

    @CustomAction
    default NewMessagePage setBranch(String branch) {
        NewIbEndpoint newIbEndpoint = getEndpoint(NewIbEndpoint.class);
        newIbEndpoint.click(() -> BRANCH_XPATH);
        newIbEndpoint.sleepSeconds(1);
        newIbEndpoint.click(() -> String.format(BRANCH_OPTION_XPATH, branch));
        return null;
    }

    @CustomAction
    default NewMessagePage setRefundedBankCode(String refundedBankCode) {
        NewIbEndpoint newIbEndpoint = getEndpoint(NewIbEndpoint.class);
        newIbEndpoint.click(() -> BANK_CODE_FOR_REFUND_BUTTON_XPATH);
        newIbEndpoint.type(() -> BANK_CODE_FOR_REFUND_INPUT_XPATH, refundedBankCode, true);
        newIbEndpoint.sendKeysOneAtATime(Key.ENTER);
        return null;
    }

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

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

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

    @TypeInto(DESTINATION_ACC_NUMBER_INPUT_XPATH)
    NewMessagePage setDestinationAccNumber(String destinationAccNumber);

    @TypeInto(ACC_NUMBER_FOR_REFUND_INPUT_XPATH)
    NewMessagePage setAccNumberForRefund(String accNumberForRefund);

    @TypeInto(PHONE_NUMBER_FOR_REFUND_INPUT_XPATH)
    NewMessagePage setPhoneNumberForRefund(String phoneNumberForRefund);

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

    @CheckElementContent(TITLE_TOPIC_XPATH)
    NewMessagePage checkTitleTopic(String titleTopic);

    @CheckElementPresent(SUBJECT_OVERDRAFT_DISABLED_XPATH)
    NewMessagePage checkSubjectOverdraftDisabled();
}
