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

import cz.moneta.test.dsl.newib.NewIbPageFlow;
import cz.moneta.test.dsl.newib.mainpage.DashboardPage;
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.messages.MessagesModalPage.*;
import static org.junit.jupiter.api.Assertions.assertTrue;

@Wait(value = {MESSAGES_TITLE_XPATH, NEW_MESSAGE_BTN_XPATH}, waitSecondsForElement = 30)
public interface MessagesModalPage extends NewIbPageFlow<MessagesModalPage>, StoreAccessor {

    String MESSAGES_TITLE_XPATH = "//h1[@class='t-title t-title--h3 u-mb--0'][text()='Zprávy']";
    String NEW_MESSAGE_BTN_XPATH = "//button[@data-testid='new-message-button'][text()='Napsat novou zprávu']";
    String SENT_MESSAGES_BTN_XPATH = "//button[@data-testid='Button'][text()='Odeslané zprávy']";
    String UNREAD_MESSAGES_BTN_XPATH = "//button[@class='c-btn c-btn--transparent c-btn--primary c-pills__listButton'][text()='Nepřečtené zprávy']";
    String INBOX_MESSAGES_BTN_XPATH = "//button[@class='c-btn c-btn--transparent c-btn--primary c-pills__listButton'][text()='Příchozí zprávy']";
    String MESSAGE_ITEM_XPATH = "//span[text()='%s']";
    String CLOSE_MODAL_BTN_XPATH = "//button[@data-testid='close-messages-button']";
    String TOOLTIP_XPATH = "//span[@class='c-avatar__icon']";
    String SUBJECT_OF_THE_RECEIVED_MESSAGE_XPATH = "//td[contains(., 'Test') and contains(., 'Hypotéky')]";
    String SUBJECT_OF_THE_SENT_MESSAGE_XPATH = "//td[contains(., '%s') and contains(., 'Zahraniční platby upřesnění/zrušení')]";
    String TEXT_OF_THE_RECEIVED_MESSAGE_XPATH = "//div[@class='custom-container']/div[normalize-space()='Test OK.']";
    String ANSWER_BUTTON_XPATH = "//button[@class='c-btn c-btn--border c-btn--primary' and text()='Odpovědět']";
    String TITLE_NEW_MESSAGE_XPATH = "//h1[@class='t-title t-title--h1' and text()='Napsat novou zprávu']";
    String VIEW_PREVIOUS_COMMUNICATION = "//span[@class='u-fontWeightNormal styles_threadTitle__7F1cI' and contains(normalize-space(), 'Zobrazit předchozí komunikaci')]";
    String DELETE_BUTTON_XPATH = "//button[@data-testid='delete-button']";
    String DELETE_MESSAGE_TITLE_XPATH = "//h2[@class='t-title t-title--h3 c-modal__title' and text()='Smazat zprávu?']";
    String CONFIRM_DELETE_MESSAGE_XPATH = "//button[@data-testid='confirm-button' and text()='Smazat']";
    String CONFIRM_DELETE_MODAL_XPATH = "//h2[@class='t-title t-title--h3 c-modal__title' and text()='Zpráva je smazaná']";
    String CLOSE_CONFIRM_DELETE_MODAL_XPATH = "//button[@data-testid='confirm-button' and text()='Zavřít']";

    @CheckElementPresent(MESSAGES_TITLE_XPATH)
    MessagesModalPage checkMessagesTitle();

    @CheckElementPresent(CLOSE_MODAL_BTN_XPATH)
    MessagesModalPage checkCloseBtnIsPresent();

    @CheckElementPresent(NEW_MESSAGE_BTN_XPATH)
    MessagesModalPage checkNewMessageBtnIsPresent();

    @Click(NEW_MESSAGE_BTN_XPATH)
    NewMessagePage clickOnNewMessage();

    @Wait(value = MESSAGE_ITEM_XPATH, isStringDynamicXpath = true)
    @CheckElementPresent(value = MESSAGE_ITEM_XPATH, isStringDynamicXpath = true)
    MessagesModalPage checkSentMessagePresent(String sentMessage);

    @Click(value = MESSAGE_ITEM_XPATH, isStringDynamicXpath = true)
    MessageDetailPage goToMessageDetail(String subject);

    @CustomAction
    default MessagesModalPage checkUrl() {
        NewIbEndpoint newIbEndpoint = getEndpoint(NewIbEndpoint.class);
        String actualUrl = newIbEndpoint.getCurrentUrl();
        String urlContains = "modal/messages";
        assertTrue(actualUrl.contains(urlContains));
        return null;
    }

    @Click(CLOSE_MODAL_BTN_XPATH)
    DashboardPage closeMessagesModal();

    @CheckElementPresent(TOOLTIP_XPATH)
    MessagesModalPage checkTooltip();

    @Click(UNREAD_MESSAGES_BTN_XPATH)
    MessagesModalPage clickOnUnreadMessages();

    @Click(INBOX_MESSAGES_BTN_XPATH)
    MessagesModalPage clickOnInboxMessages();

    @Wait(value = SENT_MESSAGES_BTN_XPATH, explicitWaitSeconds = 1)
    @Click(SENT_MESSAGES_BTN_XPATH)
    MessagesModalPage clickOnSentMessages();

    @CheckElementPresent(SUBJECT_OF_THE_RECEIVED_MESSAGE_XPATH)
    MessagesModalPage checkSubjectOfTheReceivedMessage();

    @Click(SUBJECT_OF_THE_RECEIVED_MESSAGE_XPATH)
    MessagesModalPage clickOnReceivedMessage();

    @CheckElementPresent(TEXT_OF_THE_RECEIVED_MESSAGE_XPATH)
    MessagesModalPage checkTextOfTheReceivedMessage();

    @Click(ANSWER_BUTTON_XPATH)
    MessagesModalPage clickOnAnswerButton();

    @CheckElementPresent(VIEW_PREVIOUS_COMMUNICATION)
    MessagesModalPage checkViewPreviousCommunication();

    @CheckElementPresent(TITLE_NEW_MESSAGE_XPATH)
    MessagesModalPage checkTitleNewMessage();

    @Wait(value = SUBJECT_OF_THE_SENT_MESSAGE_XPATH, explicitWaitSeconds = 1, isStringDynamicXpath = true)
    @Click(value = SUBJECT_OF_THE_SENT_MESSAGE_XPATH, isStringDynamicXpath = true)
    MessagesModalPage clickOnSentMessage(String lastMessageSubject);

    @Click(DELETE_BUTTON_XPATH)
    MessagesModalPage clickOnDeleteButton();

    @CheckElementPresent(DELETE_MESSAGE_TITLE_XPATH)
    MessagesModalPage checkDeleteMessageTitle();

    @Click(CONFIRM_DELETE_MESSAGE_XPATH)
    MessagesModalPage clickOnConfirmDeleteMessage();

    @CheckElementPresent(CONFIRM_DELETE_MODAL_XPATH)
    MessagesModalPage checkConfirmDeleteModal();

    @Click(CLOSE_CONFIRM_DELETE_MODAL_XPATH)
    MessagesModalPage clickOnCloseConfirmDeleteButton();
}