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 org.junit.jupiter.api.Assertions;

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

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

    String OUTGOING_MESSAGE_ID_KEY = "OUTGOING_MESSAGE_ID";
    String PDF_VIEWER_CONTENT_KEY = "PDF_VIEWER_CONTENT";
    String PAGE_LABEL_B = "outgoing_message_label";
    String INSPECTION_BUTTON = "//button[normalize-space()='Kontrola']";
    String SELECT_REVIEWER_INPUT = "//iq-select2[@id='singleSelect']//input[@type='text']";
    String SELECT_REVIEWER = "//div[contains(@class,'select2-dropdown-item') and contains(@title, '%s')]";
    String SUBMIT_SINGLE_SELECT = "submitSingleSelect";
    String SAVE_CONCEPT_BUTTON = "save_concept";
    String RECIPIENT_XPATH = "//iq-select2[@id='recipient_id']//input";
    String MULTIPLE_RECIPIENT_XPATH = "//input[@placeholder='Vyberte id příjemce']";
    String ASSIGNEE_USER_GROUP_XPATH = "//iq-select2[@id='new_assignee_user_group']//input";
    String FIRST_ITEM_DIV = "//*[@id='item_0' and contains(@class,'select2-dropdown-item')]";
    String FIRST_ITEM_USER_GROUP_XPATH = "//div[@id='item_0' and contains(@title, '%s')]";
    String COST_CENTER_INPUT = "cost_center";
    String SUBJECT_INPUT = "subject";
    String SEND_MESSAGE_DROPDOWN_BUTTON = "send_message_dropdown";
    String FILE_UPLOAD_XPATH = "(//input[@type='file' and @multiple='multiple'])[2]";
    String PDF_VIEWER_IFRAME = "pdfIframe";
    String EDIT_PDF_IFRAME_XPATH = "//iframe[contains(@id, 'tiny-angular_')]";
    String TEMPLATE_FOOTER = "document-footer";
    String PDF_TEMPLATE_GENERATING_BUTTON = "IncomingMessagesActionButton";
    String EMPTY_TEMPLATE = "//ul[@id='pdf_templates_firstLevel']/li//li/a[text()='Prazdná šablona' or text()='Prázdná šablona MMB']";
    String APPLICATION_TEMPLATE = "//ul[@id='pdf_templates_secondLevel']//a[text()='Přihláška']";
    String EDIT_TEMPLATE_BUTTON = "edit_attachment";
    String SAVE_TEMPLATE_XPATH = "//tbody[@id='attachment_table_body']//i[@title='Uložit']/..";
    String UNPACK_MESSAGE_INFORMATIONS = "unpack_message_informations";
    String UPLOAD_SUCCESFUL_ANNOUNCEMENT_XPATH = "//div[contains(@id,'smallbox')]//*[text()='Upload byl úspěšný']";
    String TEMPLATE_CREATING_ANNOUNCEMENT_XPATH = "//div[contains(@id,'smallbox')]//*[text()='Probíhá vytváření šablony']";
    String TEMPLATE_EDIT_SAVE_ANNOUNCEMENT_XPATH = "//div[contains(@id,'smallbox')]//*[text()='Probíhá vytváření odpovědi']";
    String GENERATE_PDF = "waitingForPdfProgressBar";
    String SEND_MESSAGE_NOW_LI = "send_message_now";
    String MESSAGE_IS_SENDING_DIV = "message_identification";
    String MESSAGE_SUCCESSFULLY_SEND_LOG_XPATH = "//span[contains(text(), 'byla odeslána')]";
    String MESSAGE_SEND_FAILURE_LOG_XPATH = "//span[contains(text(), 'selhání odeslání zprávy')]";
    String ATTACHMENT_PATH = "regression/exevido/web/testExevido.txt";
    String SECOND_ATTACHMENT_PATH = "regression/exevido/web/testExevido2.txt";
    String MESSAGE_ID_SPAN = "message_id";
    String MULTIPLE_RECIPIENTS_SPAN = "//label[@for='multiple_recipients']";
    String ADD_RECIPIENT_TO_LIST_BUTTON = "add_to_list_button";
    String BACK_BUTTON = "back_button";
    String PDF_VIEWER = "viewer";

    @Click(value = INSPECTION_BUTTON, by = Lookup.XPATH)
    NewOutgoingMessagePage clickInspection();

    @TypeInto(value = SELECT_REVIEWER_INPUT, by = Lookup.XPATH)
    NewOutgoingMessagePage fillReviewer(String text);

    @Click(value = SELECT_REVIEWER, by = Lookup.XPATH, isStringDynamicXpath = true, andWait = @Wait(value = ExevidoPanels.LOADER_DIV, until = Until.GONE))
    NewOutgoingMessagePage clickReviewer(String reviewer);

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

    @TypeInto(value = RECIPIENT_XPATH, by = Lookup.XPATH, andWait = @Wait(value = ExevidoPanels.LOADER_DIV, until = Until.GONE))
    @KeyPress(value = Key.ENTER)
    NewOutgoingMessagePage fillRecipient(String recipient);

    @TypeInto(value = MULTIPLE_RECIPIENT_XPATH, by = Lookup.XPATH, andWait = @Wait(value = ExevidoPanels.LOADER_DIV, until = Until.GONE))
    NewOutgoingMessagePage fillMultipleRecipient(String recipient);

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

    @KeyPress(Key.ENTER)
    NewOutgoingMessagePage pressEnter();

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

    @Click(SEND_MESSAGE_DROPDOWN_BUTTON)
    NewOutgoingMessagePage clickSendMessageDropdown();

    @CustomAction
    default NewOutgoingMessagePage uploadAttachment(String documentPath) {

        String path = UploadUtil.getResourcePath(documentPath);
        ExevidoEndpoint endpoint = getEndpoint(ExevidoEndpoint.class);

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

    @Click(PDF_TEMPLATE_GENERATING_BUTTON)
    NewOutgoingMessagePage clickPdfTemplateGenerating();

    @Click(value = EMPTY_TEMPLATE, by = Lookup.XPATH, jsClick = true,
            andWait = @Wait(value = TEMPLATE_CREATING_ANNOUNCEMENT_XPATH, by = Lookup.XPATH, until = Until.GONE))
    NewOutgoingMessagePage clickEmptyTemplateMmb();

    @Click(value = APPLICATION_TEMPLATE, by = Lookup.XPATH, jsClick = true, andWait = @Wait(value = TEMPLATE_CREATING_ANNOUNCEMENT_XPATH, by = Lookup.XPATH, until = Until.GONE))
    @Wait(value = GENERATE_PDF, by = Lookup.XPATH, until = Until.GONE)
    NewOutgoingMessagePage clickApplication();

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

    @CustomAction
    default NewOutgoingMessagePage storePdfViewerContent() {
        ExevidoEndpoint endpoint = getEndpoint(ExevidoEndpoint.class);
        endpoint.switchToFrame(PDF_VIEWER_IFRAME);
        this.store(PDF_VIEWER_CONTENT_KEY, PDF_VIEWER);
        endpoint.switchToDefaultContent();
        return null;
    }

    @CustomAction
    default NewOutgoingMessagePage clickSaveTemplate() {
        ExevidoEndpoint endpoint = getEndpoint(ExevidoEndpoint.class);
        endpoint.click(() -> UNPACK_MESSAGE_INFORMATIONS);
        endpoint.sendKeysOneAtATime(Key.PAGE_UP);
        endpoint.moveToElement(SAVE_TEMPLATE_XPATH, Lookup.XPATH);
        endpoint.click(() -> SAVE_TEMPLATE_XPATH, Lookup.XPATH);
        endpoint.waitForElementsToLoad(5, Until.GONE, LOADER_DIV);
        endpoint.waitForElementsToLoad(10, Lookup.XPATH, Until.GONE, TEMPLATE_EDIT_SAVE_ANNOUNCEMENT_XPATH);
        endpoint.waitForElementsToLoad(5, Until.GONE, GENERATE_PDF);
        return null;
    }

    @CustomAction
    default NewOutgoingMessagePage checkChangeOfPdfContent() {
        ExevidoEndpoint endpoint = getEndpoint(ExevidoEndpoint.class);
        endpoint.switchToFrame(PDF_VIEWER_IFRAME);
        String editedTemplate = endpoint.getText(PDF_VIEWER);
        Assertions.assertTrue(editedTemplate.contains("auto test"));
        Assertions.assertNotEquals(this.get(PDF_VIEWER_CONTENT_KEY), endpoint.getText(PDF_VIEWER));
        endpoint.switchToDefaultContent();
        return null;
    }

    @CustomAction
    default NewOutgoingMessagePage editTemplate() {
        ExevidoEndpoint endpoint = getEndpoint(ExevidoEndpoint.class);
        endpoint.switchToFrame(EDIT_PDF_IFRAME_XPATH, Lookup.XPATH);
        endpoint.sendKeysOneAtATime(Key.PAGE_DOWN);
        endpoint.click(() -> TEMPLATE_FOOTER, Lookup.CLASSNAME);
        endpoint.sendKeysOneAtATime(Key.A, Key.U, Key.T, Key.O, Key.SPACE, Key.T, Key.E, Key.S, Key.T);
        endpoint.switchToDefaultContent();
        endpoint.sendKeysOneAtATime(Key.PAGE_UP);
        return null;
    }

    @Click(SAVE_CONCEPT_BUTTON)
    NewOutgoingMessagePage clickSaveConcept();

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

    @CustomAction
    default NewOutgoingMessagePage checkMessageCorrectlySend() {
        ExevidoEndpoint endpoint = getEndpoint(ExevidoEndpoint.class);
        endpoint.refeshPage();
        endpoint.waitForElementAndRefresh(MESSAGE_SUCCESSFULLY_SEND_LOG_XPATH, Lookup.XPATH, endpoint::refeshPage, 30, 10);
        Assertions.assertTrue(endpoint.getText(MESSAGE_IS_SENDING_DIV).contains("Odeslaná"));
        return null;
    }

    @CustomAction
    default NewOutgoingMessagePage checkMessageIncorrectlySend() {
        ExevidoEndpoint endpoint = getEndpoint(ExevidoEndpoint.class);
        endpoint.refeshPage();
        endpoint.waitForElementAndRefresh(MESSAGE_SEND_FAILURE_LOG_XPATH, Lookup.XPATH, endpoint::refeshPage, 30, 10);
        Assertions.assertTrue(endpoint.getText(MESSAGE_IS_SENDING_DIV).contains("Chybná"));
        return null;
    }

    @CustomAction
    default NewOutgoingMessagePage checkMassMessageCorrectlySend() {

        ExevidoEndpoint endpoint = getEndpoint(ExevidoEndpoint.class);
        String message = endpoint.getText(MESSAGE_IS_SENDING_DIV);
        Assertions.assertTrue(message.contains("Odeslaná - Hromadně"));
        return null;
    }

    @CustomAction
    default NewOutgoingMessagePage storeMessageId() {

        ExevidoEndpoint endpoint = getEndpoint(ExevidoEndpoint.class);
        String messageId = endpoint.getText(MESSAGE_ID_SPAN).replaceAll("ID:(\\d+)", "$1");
        store(OUTGOING_MESSAGE_ID_KEY, messageId);
        return null;
    }

    @Click(value = MULTIPLE_RECIPIENTS_SPAN, by = Lookup.XPATH)
    NewOutgoingMessagePage clickMultipleRecipients();

    @Click(value = ADD_RECIPIENT_TO_LIST_BUTTON)
    NewOutgoingMessagePage addRecipient();

    @Click(BACK_BUTTON)
    OutgoingMessagesPage clickBack();
}