package cz.moneta.test.dsl.newib.settings.communication.contacts;

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.communication.contacts.CreateNewContactPage.*;

@Wait(value = {SUBMIT_BTN_XPATH, BACK_BTN_XPATH}, waitSecondsForElement = 30)
public interface CreateNewContactPage extends NewIbPageFlow<CreateNewContactPage>, StoreAccessor {

    String NAME_INPUT_XPATH = "//input[@id='name']";
    String PHONE_NUMBER_INPUT_XPATH = "//input[@id='phoneNumber']";
    String MOBILE_OPERATOR_BTN_XPATH = "//button[@id='mobileOperator_button']";
    String MOBILE_OPERATION_OPTION_XPATH = "//li[@class='c-dropdown__item']//following::div[text()='%s']";
    String SUBMIT_BTN_XPATH = "//button[@data-testid='submit']";
    String BACK_BTN_XPATH = "//button[@data-testid='back']";
    String NAME_VALIDATION_TEXT_XPATH = "//div[@id='name_errorMessage' and normalize-space(text())='Toto pole je povinné.']";
    String NUMBER_VALIDATION_TEXT_XPATH = "//div[@id='phoneNumber_errorMessage' and normalize-space(text())='Toto pole je povinné.']";
    String OPERATOR_VALIDATION_TEXT_XPATH = "//div[@id='mobileOperator_errorMessage' and normalize-space(text())='Toto pole je povinné.']";

    @TypeInto(value = NAME_INPUT_XPATH, clear = true)
    CreateNewContactPage setName(String name);

    @TypeInto(PHONE_NUMBER_INPUT_XPATH)
    CreateNewContactPage setPhoneNumber(String phoneNumber);

    @CustomAction
    default CreateNewContactPage setOperator(String operator) {
        NewIbEndpoint newIbEndpoint = getEndpoint(NewIbEndpoint.class);
        newIbEndpoint.click(() -> MOBILE_OPERATOR_BTN_XPATH);
        newIbEndpoint.sleepSeconds(1);
        newIbEndpoint.click(() -> String.format(MOBILE_OPERATION_OPTION_XPATH, operator));
        return null;
    }

    @Click(value = SUBMIT_BTN_XPATH, andWait = @Wait(explicitWaitSeconds = 2))
    CreateContactVictoryPage submitNewContact();

    @Click(value = SUBMIT_BTN_XPATH, andWait = @Wait(explicitWaitSeconds = 2))
    CreateNewContactPage  clickSubmitStayOnPage();

    @CheckElementsPresent(value = {
            @CheckElementPresent(NAME_VALIDATION_TEXT_XPATH),
            @CheckElementPresent(NUMBER_VALIDATION_TEXT_XPATH),
            @CheckElementPresent(OPERATOR_VALIDATION_TEXT_XPATH)})
    CreateNewContactPage checkValidationTextsArePresent();
}
