package cz.moneta.test.dsl.exevido.pages;

import cz.moneta.test.dsl.exevido.ExevidoWebFlow;
import cz.moneta.test.dsl.exevido.components.ExevidoPanels;
import cz.moneta.test.dsl.ufo.banka.pages.fon.UploadUtil;
import cz.moneta.test.harness.context.StoreAccessor;
import cz.moneta.test.harness.endpoints.exevido.ExevidoEndpoint;
import cz.moneta.test.harness.support.web.*;

import static cz.moneta.test.dsl.exevido.pages.NewIncomingMessagePage.PAGE_LABEL_B;

@Wait(PAGE_LABEL_B)
public interface NewIncomingMessagePage extends ExevidoWebFlow<NewIncomingMessagePage>, StoreAccessor {

    String INCOMING_MESSAGE_ID_KEY = "INCOMING_MESSAGE_ID";
    String PAGE_LABEL_B = "outgoing_message_label";
    String ANSWER_BUTTON = "answer";
    String SUBJECT_INPUT = "subject";
    String SENDER_XPATH = "//iq-select2[@id='_selectId']//input";
    String FILE_UPLOAD_XPATH = "(//input[@type='file' and @multiple='multiple'])";
    String UPLOAD_SUCCESSFUL_ANNOUNCEMENT_XPATH = "//p[text()='Upload byl úspěšný']";
    String ASSIGNEE_USER_GROUP_XPATH = "//iq-select2[@id='new_assignee_user_group']//input";
    String RECIPIENT_NUMBER_INPUT = "legal_title_point";
    String FIRST_ITEM_USER_GROUP_XPATH = "//div[@id='item_0' and contains(@title, '%s')]";
    String SAVE_MESSAGE_BUTTON = "incoming_save_button";
    String MESSAGE_ID_SPAN = "message_id";
    String ALERT_YES_BUTTON = "unsaved_data_confirm_button";
    String TYPE_OF_MESSAGE = "//iq-select2[@id='_type_option']//input";
    String ITEM_TYPE_OF_MESSAGE = "//*[starts-with(@id,'item_') and @title= '%s']";
    String SENDER_ITEM = "item_0";
    String DELEGATE_BUTTON = "delegate_button_id";
    String CREATE_COPY_BUTTON = "create_copy";
    String CONFIRM_ATTACHMENT_BUTTON = "//app-actis-button//button[normalize-space()='Potvrdit']";
    String DROPDOWN_CREATE_DUPLICATE_BUTTON = "//app-actis-button[@buttonid='create_copy']/following-sibling::button";
    String SEND_TO_ANOTHER_DATA_BOX_BUTTON = "//app-actis-button//button[contains(., 'Do jiné datové schránky')]";
    String UNIVERSAL_SINGLE_SELECT = "singleSelect";
    String OTHER_SELECTION_XPATH = "//iq-select2[@id='singleSelect']//div[contains(@class, 'select2-dropdown-item') and contains(@title, '%s')]";
    String UNIVERSAL_SUBMIT_SINGLE_SELECT_BUTTON = "submitSingleSelect";
    String CONFIRM_BUTTON = "//button[normalize-space()='Potvrdit']";
    String CHANGE_MESSAGE_TYPE = "change_message_type";
    String FIELD_VALIDATION_LABEL = "//b[text()='Kontrola polí']";
    String FOLDER_FILE_NUMBER_XPATH = "//iq-select2[@id='folderFileNumber']//input";
    String FOLDER_FILE_NUMBER_ITEM_0 = "(//iq-select2[@id='folderFileNumber']//div[@id='item_0'])";
    String SAVE_CHECKED_FIELD = "save_checked_field";

    @Click(value = ANSWER_BUTTON, andWait = @Wait(value = ExevidoPanels.LOADER_DIV, until = Until.GONE))
    NewOutgoingMessagePage clickAnswer();

    @TypeInto(SUBJECT_INPUT)
    NewIncomingMessagePage fillSubject(String subject);

    @TypeInto(value = TYPE_OF_MESSAGE, by = Lookup.XPATH)
    @Click(value = ITEM_TYPE_OF_MESSAGE, by = Lookup.XPATH, isStringDynamicXpath = true)
    NewIncomingMessagePage fillTypeMessage(String messageType);

    @TypeInto(value = SENDER_XPATH, by = Lookup.XPATH, andWait = @Wait(value = ExevidoPanels.LOADER_DIV, until = Until.GONE))
    @KeyPress(value = Key.ENTER)
    NewIncomingMessagePage fillSender(String sender);

    @CustomAction
    default NewIncomingMessagePage uploadAttachment(String documentPath) {
        String path = UploadUtil.getResourcePath(documentPath);
        ExevidoEndpoint endpoint = getEndpoint(ExevidoEndpoint.class);

        endpoint.type(() -> FILE_UPLOAD_XPATH, path, false, Lookup.XPATH);
        endpoint.waitForElementsToLoad(10, Lookup.XPATH, Until.VISIBLE, UPLOAD_SUCCESSFUL_ANNOUNCEMENT_XPATH);
        return null;
    }

    @CustomAction
    default NewIncomingMessagePage acceptAlertIfVisible() {
        ExevidoEndpoint endpoint = getEndpoint(ExevidoEndpoint.class);
        if (endpoint.isElementVisible(0, ALERT_YES_BUTTON, Lookup.ID)) {
            endpoint.click(() -> ALERT_YES_BUTTON);
        }
        return null;
    }

    @CustomAction
    default NewIncomingMessagePage fillAssigneeUserGroup(String group) {
        ExevidoEndpoint endpoint = getEndpoint(ExevidoEndpoint.class);
        endpoint.type(() -> ASSIGNEE_USER_GROUP_XPATH, group, false, Lookup.XPATH);
        endpoint.scrollIntoView(RECIPIENT_NUMBER_INPUT, Lookup.ID);
        String xPath = String.format(FIRST_ITEM_USER_GROUP_XPATH, group);
        endpoint.click(() -> xPath, Lookup.XPATH);
        return null;
    }

    @Click(value = SAVE_MESSAGE_BUTTON, andWait = @Wait(value = ExevidoPanels.LOADER_DIV, until = Until.GONE))
    NewIncomingMessagePage clickSave();

    @CustomAction
    default NewIncomingMessagePage storeMessageId() {
        ExevidoEndpoint endpoint = getEndpoint(ExevidoEndpoint.class);
        String messageId = endpoint.getText(MESSAGE_ID_SPAN).replaceAll("ID:(\\d+)", "$1");
        store(INCOMING_MESSAGE_ID_KEY, messageId);
        return null;
    }

    @CustomAction
    default IncomingMessagesPage clickDelegateMessage() {
        ExevidoEndpoint endpoint = getEndpoint(ExevidoEndpoint.class);
        endpoint.scrollToPageTop();
        endpoint.scrollIntoView(ASSIGNEE_USER_GROUP_XPATH, Lookup.XPATH);
        endpoint.click(() -> DELEGATE_BUTTON);
        return null;
    }

    @Click(CREATE_COPY_BUTTON)
    NewIncomingMessagePage clickCreateCopy();

    @Click(value = CONFIRM_ATTACHMENT_BUTTON, by = Lookup.XPATH, andWait = @Wait(value = ExevidoPanels.LOADER_DIV, until = Until.GONE))
    NewIncomingMessagePage clickConfirmAttachment();

    @Click(value = DROPDOWN_CREATE_DUPLICATE_BUTTON, by = Lookup.XPATH)
    @Click(value = SEND_TO_ANOTHER_DATA_BOX_BUTTON, by = Lookup.XPATH)
    NewIncomingMessagePage sendToAnotherDataBox();

    @Click(UNIVERSAL_SINGLE_SELECT)
    NewIncomingMessagePage clickUniversalSingleSelect();

    @Click(value = OTHER_SELECTION_XPATH, by = Lookup.XPATH, isStringDynamicXpath = true)
    NewIncomingMessagePage fillSingleSelect(String inputValue);

    @Click(value = UNIVERSAL_SUBMIT_SINGLE_SELECT_BUTTON)
    NewIncomingMessagePage clickSubmitSingleSelect();

    @Click(value = CONFIRM_BUTTON, by = Lookup.XPATH)
    DetailIncomingMessagePage clickConfirm();

    @Click(CHANGE_MESSAGE_TYPE)
    NewIncomingMessagePage clickChangeMessageType();

    @CustomAction
    default NewIncomingMessagePage clickFolderFileNumber() {
        ExevidoEndpoint endpoint = getEndpoint(ExevidoEndpoint.class);
        endpoint.scrollIntoView(FIELD_VALIDATION_LABEL, Lookup.XPATH);
        endpoint.click(() -> FOLDER_FILE_NUMBER_XPATH, Lookup.XPATH);
        return null;
    }

    @Click(value = FOLDER_FILE_NUMBER_ITEM_0, by = Lookup.XPATH, andWait = @Wait(value = ExevidoPanels.LOADER_DIV, until = Until.GONE))
    NewIncomingMessagePage clickFirstFolderFile();

    @Click(value = SAVE_CHECKED_FIELD, andWait = @Wait(value = ExevidoPanels.LOADER_DIV, until = Until.GONE))
    NewIncomingMessagePage clickSaveCheckedField();
}