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

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.MobileOperatorsPaymentPage.*;

@Wait(value = {PHONE_NUMBER_XPATH, AMOUNT_XPATH}, waitSecondsForElement = 30)
public interface MobileOperatorsPaymentPage extends NewIbPageFlow<MobileOperatorsPaymentPage>, VerticalMainMenu, StoreAccessor, Builder {

    String PHONE_NUMBER_XPATH = "//input[@id='phoneNumber']";
    String CHOOSE_OPERATOR_XPATH = "//button[@id='operator_button']";
    String OPERATOR_OPTION_XPATH = "//li[@role='option']//div[@class='c-dropdown__itemContent' and normalize-space(text())='%s']";
    String OPERATOR_OPTION_SELECTED_XPATH = "//span[@class='f-control2__valueTitle'][text()='%s']";
    String AMOUNT_XPATH = "//input[@id='amount']";
    String CHOOSE_CATEGORY_XPATH = "//div[@data-testid='categoryId']";
    String SEND_MESSAGE_FOR_RECIPIENT_XPATH = "//label[@for='sendMessage']";
    String NOTE_FOR_RECIPIENT_XPATH = "//textarea[@id='noteForRecipient']";
    String CONFIRMATION_PHONE_NUMBER_XPATH = "//input[@id='confirmationPhoneNumber']";
    String CONTINUE_BUTTON_XPATH = "//button[@data-testid='submit']";
    String VODAFONE_INVOICE_PAYMENT_BTN_XPATH = "//button[text()='Platba faktury Vodafone']";
    String CATEGORY_OPTION_XPATH = "//div[text()='%s']";
    String SELECTED_CATEGORY_OPTION_XPATH = "//button[@id='categoryId_button']//span[contains(normalize-space(text()), '%s')]";
    String NO_VODAFONE_EXIST_XPATH = "//h4[translate(text(), ' ', '')=concat('Telefonníčíslo+420', '%s', 'neexistuje')]";

    @TypeInto(PHONE_NUMBER_XPATH)
    MobileOperatorsPaymentPage setPhoneNumber(String phoneNumber);

    @TypeInto(AMOUNT_XPATH)
    MobileOperatorsPaymentPage setAmount(String amount);

    @TypeInto(NOTE_FOR_RECIPIENT_XPATH)
    MobileOperatorsPaymentPage setNoteForRecipient(String noteForRecipient);

    @TypeInto(CONFIRMATION_PHONE_NUMBER_XPATH)
    MobileOperatorsPaymentPage setConfirmationPhoneNumber(String confirmationPhoneNumber);

    @Click(CONTINUE_BUTTON_XPATH)
    MobileOperatorsPaymentSummaryPage clickOnContinue();

    @Click(CONTINUE_BUTTON_XPATH)
    MobileOperatorsPaymentPage clickOnContinueVodafone();

    @CheckElementPresent(value = NO_VODAFONE_EXIST_XPATH, isStringDynamicXpath = true)
    MobileOperatorsPaymentPage checkVictoryPageVodafoneOff(String phoneNumber);

    @Click(SEND_MESSAGE_FOR_RECIPIENT_XPATH)
    MobileOperatorsPaymentPage clickOnSendMessageForRecipient();

    @CustomAction
    default MobileOperatorsPaymentPage setOperator(String operator) {
        NewIbEndpoint newIbEndpoint = getEndpoint(NewIbEndpoint.class);
        newIbEndpoint.click(() -> CHOOSE_OPERATOR_XPATH);
        newIbEndpoint.sleepSeconds(1);
        newIbEndpoint.click(() -> String.format(OPERATOR_OPTION_XPATH, operator));
        newIbEndpoint.waitForElementsToLoad(5, String.format(OPERATOR_OPTION_SELECTED_XPATH, operator));
        return null;
    }

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

    @Click(VODAFONE_INVOICE_PAYMENT_BTN_XPATH)
    MobileOperatorsPaymentPage clickOnVodafoneInvoicePaymentBtn();
}
