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

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.Click;
import cz.moneta.test.harness.support.web.CustomAction;
import cz.moneta.test.harness.support.web.Key;
import cz.moneta.test.harness.support.web.TypeInto;

public interface DirectDebitPage extends NewIbPageFlow<DirectDebitPage>, NewPaymentMenuPage, StoreAccessor {
    String PREFIX_ACCOUNT_NUMBER = "//input[@data-testid='destinationAccountPrefix']";
    String BASE_ACCOUNT_NUMBER = "//input[@data-testid='destinationAccountNumber']";
    String DIRECT_DEBIT_NAME = "//input[@name='description']";
    String DIRECT_DEBIT_LIMIT = "//input[@name='amountLimit']";
    String DATE_OF_LAST_PAYMENT = "//input[@name='endDate']";
    String CONTINUE_BUTTON_XPATH = "//button[@data-testid='submit']";
    String FREQUENCY_BTN_XPATH = "//button[@id='frequency_button']";
    String FREQUENCY_OPTION_XPATH = "//div[normalize-space()='%s']";
    String BANK_CODE_BUTTON_XPATH = "//button[@name='destinationAccountBank']";
    String BANK_CODE_INPUT_XPATH = "//input[@name='destinationAccountBank']";


    @TypeInto(PREFIX_ACCOUNT_NUMBER)
    DirectDebitPage fillPrefixAccountNumber(String prefixNumberOfAccount);

    @TypeInto(BASE_ACCOUNT_NUMBER)
    DirectDebitPage fillAccountNumber(String numberOfAccount);

    @TypeInto(DIRECT_DEBIT_NAME)
    DirectDebitPage fillNameOfDirectDebit(String nameOfDirectDebit);

    @TypeInto(DIRECT_DEBIT_LIMIT)
    DirectDebitPage fillDirectDebitLimit(String directDebitLimit);

    @TypeInto(DATE_OF_LAST_PAYMENT)
    DirectDebitPage fillDateOfLastPayment(String dateoflastPayment);

    @Click(CONTINUE_BUTTON_XPATH)
    DirectDebitSummaryPage clickOnContinue();

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

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