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.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.DetailIncomingMessagePage.PAGE_LABEL;

@Wait(value = PAGE_LABEL)
public interface DetailIncomingMessagePage extends ExevidoWebFlow<DetailIncomingMessagePage>, StoreAccessor {

    String PAGE_LABEL = "outgoing_message_label";
    String ASSIGN_BUTTON = "assign_to_me";
    String PROCESSED_BUTTON = "set_as_processed";
    String RETURN_TO_MAILROOM_BUTTON = "return_to_podatelna";
    String RETURN_FOR_REASON = "podatelnaReason";
    String ADD_REASON_BUTTON = "addReasonPodatelnaButton";
    String DELEGATE_BUTTON = "delegate_button_id";
    String ASSIGNEE_XPATH = "//iq-select2[@id='new_assignee']//input";
    String ASSIGNEE_USER_GROUP_XPATH = "//iq-select2[@id='new_assignee_user_group']//input";
    String ASSIGNEE_ITEM = "//*[@id='item_0' and contains(@title,'%s')]";
    String PROCESSING_SETTINGS_LABEL_XPATH = "//b[text()='Nastavení zpracování']";

    String SAVE_BUTTON = "save_message_recipient";

    String WARNING = "col-lg-12";
    String DOWNLOAD_ATTACHMENTS_BUTTON = "mass_download_button";
    String DOWNLOAD_ATTACHMENT_DIV = "file-name-cell-buttons";
    String MORE_INFORMATION = "unpack_message_informations";
    String GENERATE_PDF_BUTTON_XPATH = "//button[contains(@class, 'btn-light') and contains(., 'Generovat Doručenku v PDF')]";
    String DOWNLOAD_PDF = "//button[contains(@class, 'btn-light') and contains(., 'Stáhnout Doručenku v PDF')]";
    String DOWNLOAD_MSG = "//button[contains(@class, 'btn-light') and contains(., ' Stáhnout MSG')]";
    String DOWNLOAD_ZFO_DELIVERY_RECEIPT_XPATH = "//button[contains(., 'Stáhnout ZFO Doručenku')]";
    String DOWNLOAD_ZFO = "//button[normalize-space()='Stáhnout ZFO']";
    String MESSAGE_RETURN_P = "//p[text()='Zpráva byla vrácena na podatelnu k dalšímu zpracování']";
    String INTERNAL_REFERENCE_NUMBER = "//accordion-group[.//b[text()='Spisy']]//table//tr/td[2][normalize-space()='%s']";
    String ADD_MESSAGE_FOLDER_BUTTON = "add_message_folder_button";
    String ADD_FOLDER_ISSUE_NUMBER_INPUT = "add_folder_issue_number";
    String SAVE_FOLDER_ISSUE_NUMBER = "//td//i[@title='Přiřadit ke spisu']";
    String REMOVE_FOLDER_ISSUE_NUMBER = "//tr[td[2][normalize-space(text())='%s']]//td[4]//i[@title='Odebrat spis']";
    String CONFIRM_BUTTON = "confirm_button";
    String MESSAGE_ID_SPAN = "message_id";

    @Click(ASSIGN_BUTTON)
    DetailIncomingMessagePage clickAssign();

    @Click(PROCESSED_BUTTON)
    DetailIncomingMessagePage clickProcessed();

    @Click(RETURN_TO_MAILROOM_BUTTON)
    DetailIncomingMessagePage clickReturn();

    @TypeInto(RETURN_FOR_REASON)
    DetailIncomingMessagePage fillReason(String reason);

    @Click(value = ADD_REASON_BUTTON, andWait = @Wait(value = ExevidoPanels.LOADER_DIV, until = Until.GONE))
    DetailIncomingMessagePage clickAddReason();

    @Click(value = DELEGATE_BUTTON, andWait = @Wait(value = ExevidoPanels.LOADER_DIV, until = Until.GONE))
    IncomingMessagesPage clickDelegate();

    @CustomAction
    private DetailIncomingMessagePage fillUserSelector(String value, String inputXpath) {
        ExevidoEndpoint endpoint = getEndpoint(ExevidoEndpoint.class);
        endpoint.type(() -> inputXpath, value, false, Lookup.XPATH);
        endpoint.scrollIntoView(PROCESSING_SETTINGS_LABEL_XPATH, Lookup.XPATH);
        endpoint.waitForElementsToLoad(1, Lookup.XPATH, Until.VISIBLE, String.format(ASSIGNEE_ITEM, value));
        endpoint.sendKeysOneAtATime(Key.ENTER);
        return null;
    }

    @CustomAction
    default DetailIncomingMessagePage fillAssignee(String assignee) {
        return fillUserSelector(assignee, ASSIGNEE_XPATH);
    }

    @CustomAction
    default DetailIncomingMessagePage fillGroup(String group) {
        return fillUserSelector(group, ASSIGNEE_USER_GROUP_XPATH);
    }

    @CheckElementContent(value = WARNING, by = Lookup.CLASSNAME)
    DetailIncomingMessagePage checkWarning(String warning);

    @CheckElementContent(value = MESSAGE_RETURN_P, by = Lookup.XPATH)
    DetailIncomingMessagePage checkMessageReturn(String warning);

    @Click(value = DOWNLOAD_ATTACHMENTS_BUTTON, andWait = @Wait(value = ExevidoPanels.LOADER_DIV, until = Until.GONE))
    DetailIncomingMessagePage downloadAttachments();

    @Click(value = DOWNLOAD_ATTACHMENT_DIV, by = Lookup.CLASSNAME, andWait = @Wait(value = ExevidoPanels.LOADER_DIV, until = Until.GONE))
    DetailIncomingMessagePage downloadAttachment();

    @Click(value = MORE_INFORMATION, andWait = @Wait(value = ExevidoPanels.LOADER_DIV, until = Until.GONE))
    DetailIncomingMessagePage clickMoreInformation();

    @CustomAction
    default DetailIncomingMessagePage generatePdf() {
        ExevidoEndpoint endpoint = getEndpoint(ExevidoEndpoint.class);
        endpoint.scrollIntoView(DOWNLOAD_MSG, Lookup.XPATH);
        endpoint.click(() -> GENERATE_PDF_BUTTON_XPATH, Lookup.XPATH);
        return null;
    }

    @Click(value = DOWNLOAD_PDF, by = Lookup.XPATH, andWait = @Wait(value = ExevidoPanels.LOADER_DIV, until = Until.GONE))
    DetailIncomingMessagePage downloadPdf();

    @Click(value = DOWNLOAD_MSG, by = Lookup.XPATH, andWait = @Wait(value = ExevidoPanels.LOADER_DIV, until = Until.GONE))
    DetailIncomingMessagePage downloadMsg();

    @CustomAction
    default DetailIncomingMessagePage downloadZfoDeliveryReceipt() {
        ExevidoEndpoint endpoint = getEndpoint(ExevidoEndpoint.class);
        endpoint.scrollIntoView(DOWNLOAD_MSG, Lookup.XPATH);
        endpoint.click(() -> DOWNLOAD_ZFO_DELIVERY_RECEIPT_XPATH, Lookup.XPATH);
        return null;
    }

    @Click(value = DOWNLOAD_ZFO, by = Lookup.XPATH, andWait = @Wait(value = ExevidoPanels.LOADER_DIV, until = Until.GONE))
    DetailIncomingMessagePage downloadZfo();

    @CheckElementPresent(value = INTERNAL_REFERENCE_NUMBER, by = Lookup.XPATH, isStringDynamicXpath = true)
    DetailIncomingMessagePage checkInternalReferenceNumber(String referenceNumber);

    @CustomAction
    default DetailIncomingMessagePage clickAddMessageFolder() {
        ExevidoEndpoint endpoint = getEndpoint(ExevidoEndpoint.class);
        endpoint.moveToElement(ADD_MESSAGE_FOLDER_BUTTON);
        endpoint.click(() -> ADD_MESSAGE_FOLDER_BUTTON);
        return null;
    }

    @TypeInto(value = ADD_FOLDER_ISSUE_NUMBER_INPUT, clear = true, andWait = @Wait(value = ExevidoPanels.LOADER_DIV, until = Until.GONE))
    @KeyPress(Key.ENTER)
    DetailIncomingMessagePage fillFolderIssueNumber(String folderIssueNumber);

    @Click(value = SAVE_FOLDER_ISSUE_NUMBER, by = Lookup.XPATH, andWait = @Wait(value = ExevidoPanels.LOADER_DIV, until = Until.GONE))
    DetailIncomingMessagePage saveFolderIssueNumber();

    @Click(value = REMOVE_FOLDER_ISSUE_NUMBER, by = Lookup.XPATH, isStringDynamicXpath = true)
    @Click(value = CONFIRM_BUTTON, andWait = @Wait(value = ExevidoPanels.LOADER_DIV, until = Until.GONE))
    DetailIncomingMessagePage removeFolderIssueNumber(String folderIssueNumber);

    @CheckElementContent(value = MESSAGE_ID_SPAN)
    NewIncomingMessagePage checkMessageId(String id);
}