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.IncomingMessagesPage.PAGE_LABEL_XPATH;

@Wait(value = PAGE_LABEL_XPATH, by = Lookup.XPATH)
public interface IncomingMessagesPage extends ExevidoWebFlow<IncomingMessagesPage>, StoreAccessor {

    String PAGE_LABEL_XPATH = "//b[contains(text(),'Příchozí zprávy dle stavu')]";
    String FIRST_INCOMING_MESSAGE_CHECKBOX_XPATH = "//tr[@id='incoming_message_0_row']//td[@class='checkbox-table-cell']";
    String CREATE_NEW_INCOMING_MESSAGE_BUTTON = "create_new_incoming_message";
    String ID_MESSAGE_INPUT = "text_message_id";
    String INCOMING_MESSAGE_ROW = "incoming_message_0_row";
    String INCOMING_MESSAGE_TABLE_BODY = "//tbody[@id='incoming_message_table_body']//*[@title='%s']";
    String DATE_RANGE_ACCEPTANCE_TIME = "date_range_acceptance_time";
    String INCOMING_MESSAGE_ROW_CHECKBOX = "//tr[@id='incoming_message_0_row']//label[input[@type='checkbox']]/span";
    String INCOMING_MESSAGES_ACTION_BUTTON = "incoming_messages_action_button";
    String MARK_AS_IRRELEVANT_BUTTON = "irrelevant";
    String IRRELEVANT_REASON = "irrelevant_reason";
    String ADD_IRRELEVANT_REASON_BUTTON = "add_reason_button";
    String MESSAGE_ID_DIV = "//tr[@id='incoming_message_0_row']/td[3]";
    String MESSAGE_ID_KEY = "MESSAGE_ID";


    @CheckElementPresent(value = FIRST_INCOMING_MESSAGE_CHECKBOX_XPATH, by = Lookup.XPATH)
    IncomingMessagesPage checkFirstIncomingMessagePresence();

    @CheckElementPresent(value = INCOMING_MESSAGE_TABLE_BODY, by = Lookup.XPATH, isStringDynamicXpath = true)
    IncomingMessagesPage checkMessageTitlePresence(String messageTitle);

    @Click(CREATE_NEW_INCOMING_MESSAGE_BUTTON)
    NewIncomingMessagePage clickCreateNewIncomingMessage();

    @TypeInto(value = ID_MESSAGE_INPUT, clear = true)
    @KeyPress(value = Key.ENTER, andWait = @Wait(value = ExevidoPanels.LOADER_DIV, until = Until.GONE))
    IncomingMessagesPage searchByMessageId(String id);

    @CustomAction
    default DetailIncomingMessagePage clickIncomingMessageRow(int timeout) {
        ExevidoEndpoint endpoint = getEndpoint(ExevidoEndpoint.class);
        endpoint.sendKeysOneAtATime(Key.ENTER);
        endpoint.waitForElementAndRefresh(INCOMING_MESSAGE_ROW, Lookup.ID, endpoint::refeshPage, timeout, 2);
        endpoint.click(() -> INCOMING_MESSAGE_ROW, Lookup.ID);
        return null;
    }

    @CustomAction
    default IncomingMessagesPage verifyIncomingMessageRowNotPresent() {
        ExevidoEndpoint endpoint = getEndpoint(ExevidoEndpoint.class);
        if (endpoint.isElementPresent(INCOMING_MESSAGE_ROW, Lookup.ID)) {
            throw new AssertionError("Incoming message row should NOT be present.");
        }
        return null;
    }

    @TypeInto(value = DATE_RANGE_ACCEPTANCE_TIME, clear = true)
    @KeyPress(value = Key.ENTER, andWait = @Wait(value = ExevidoPanels.LOADER_DIV, until = Until.GONE))
    IncomingMessagesPage searchByDateRange(String date);

    @Click(value = INCOMING_MESSAGE_ROW_CHECKBOX, by = Lookup.XPATH)
    IncomingMessagesPage clickIncomingMessageRowCheckbox();

    @Click(INCOMING_MESSAGES_ACTION_BUTTON)
    @Click(MARK_AS_IRRELEVANT_BUTTON)
    IncomingMessagesPage clickMarkAsIrrelevantMessage();

    @TypeInto(IRRELEVANT_REASON)
    @Click(ADD_IRRELEVANT_REASON_BUTTON)
    IncomingMessagesPage addIrrelevantReason(String reason);

    @StoreElementContent(storeKey = MESSAGE_ID_KEY, value = MESSAGE_ID_DIV, by = Lookup.XPATH)
    IncomingMessagesPage saveMessageID();
}