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

import cz.moneta.test.dsl.newib.documents.DocumentsModalPage;
import cz.moneta.test.dsl.newib.mainpage.ConfirmLogoutPage;
import cz.moneta.test.dsl.newib.mainpage.SuccessfullyLogoutPage;
import cz.moneta.test.dsl.newib.messages.MessagesModalPage;
import cz.moneta.test.dsl.newib.settings.SettingsModalPage;
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.components.HorizontalMainMenu.CONTACTS_ITEM_XPATH;
import static cz.moneta.test.dsl.newib.components.HorizontalMainMenu.HORIZONTAL_MENU_XPATH;
import static cz.moneta.test.dsl.newib.components.HorizontalMainMenu.SETTINGS_ITEM_XPATH;
import static org.junit.jupiter.api.Assertions.assertTrue;

@Wait(value = {HORIZONTAL_MENU_XPATH, CONTACTS_ITEM_XPATH, SETTINGS_ITEM_XPATH}, waitSecondsForElement = 30)
public interface HorizontalMainMenu extends Builder, StoreAccessor {
    String HORIZONTAL_MENU_XPATH = "//header[@role='banner']//following::ul[@data-testid='MenuList']";

    // Menu items
    String LOGOUT_XPATH = HORIZONTAL_MENU_XPATH + "//span[text()='Odhlásit']";
    String CONTACTS_ITEM_XPATH = HORIZONTAL_MENU_XPATH + "//span[text()='Kontakty']";
    String MESSAGES_ITEM_XPATH = HORIZONTAL_MENU_XPATH + "//span[text()='Zprávy']";
    String MESSAGES_AND_NOTIFICATION_ITEM_XPATH = HORIZONTAL_MENU_XPATH + "//span[text()='Zprávy a oznámení']";
    String DOCUMENTS_ITEM_XPATH = HORIZONTAL_MENU_XPATH + "//span[text()='Dokumenty']";
    String SETTINGS_ITEM_XPATH = HORIZONTAL_MENU_XPATH + "//span[text()='Nastavení']";
    String BLOCK_BANKING_BUTTON_XPATH = HORIZONTAL_MENU_XPATH + "//span[text()='Zablokovat bankovnictví']";
    String BLOCK_BANKING_TITLE_XPATH = "//h1[@class='t-title t-title--h3'][text()='Okamžitá nouzová blokace bankovnictví']";
    String CLOSE_MODAL_BTN_XPATH = "//button[@data-testid='settings_modal-close']";
    String CONTACTS_POPUP_TITLE_XPATH = "//h2[@class='t-title t-title--h3 c-modal__title'][text()='Jak Vám můžeme pomoci?']";
    String CONTINUE_BLOCK_BANKING_BUTTON_XPATH = "//button[@data-testid='continueBlockBanking']";
    String CONTACTS_POPUP_XPATH = "//a[@data-testid='redirectToContacts']";
    String CONTINUE_WORK_BUTTON = "//button[@data-testid='continue-work']";
    String STOP_WORKING_BUTTON = "//button[@data-testid='stop-work']";

    @Click(LOGOUT_XPATH)
    ConfirmLogoutPage clickLogout();

    @Click(value = SETTINGS_ITEM_XPATH, andWait = @Wait(explicitWaitSeconds = 2))
    SettingsModalPage clickOnSettings();

    @Click(value = MESSAGES_ITEM_XPATH, andWait = @Wait(explicitWaitSeconds = 1))
    MessagesModalPage goToMessages();

    @Click(value = MESSAGES_AND_NOTIFICATION_ITEM_XPATH, andWait = @Wait(explicitWaitSeconds = 1))
    MessagesModalPage goToMessagesAndNotification();

    @Click(DOCUMENTS_ITEM_XPATH)
    DocumentsModalPage goToDocuments();

    @CustomAction
    default HorizontalMainMenu goToContactsAndCheckUrl() {
        NewIbEndpoint newIbEndpoint = getEndpoint(NewIbEndpoint.class);
        newIbEndpoint.click(() -> CONTACTS_ITEM_XPATH);
        newIbEndpoint.click(() -> CONTACTS_POPUP_XPATH);
        newIbEndpoint.switchToOtherWindow(5, 2);
        String actualUrl = newIbEndpoint.getCurrentUrl();
        String urlContains = "/kontakt";
        assertTrue(actualUrl.contains(urlContains));
        return null;
    }

    @Click(BLOCK_BANKING_BUTTON_XPATH)
    HorizontalMainMenu clickOnBlockBanking();

    @CheckElementPresent(BLOCK_BANKING_TITLE_XPATH)
    HorizontalMainMenu checkBlockBankingTitleIsPresent();

    @Click(CLOSE_MODAL_BTN_XPATH)
    HorizontalMainMenu closeBlockBanking();

    @Click(CONTACTS_ITEM_XPATH)
    HorizontalMainMenu clickOnHorizontalContacts();

    @CheckElementPresent(CONTACTS_POPUP_TITLE_XPATH)
    HorizontalMainMenu checkContactsPopupTitle();

    @Click(CONTINUE_BLOCK_BANKING_BUTTON_XPATH)
    HorizontalMainMenu clickOnBlockingBanking();

    @Wait(explicitWaitSeconds = 240)
    HorizontalMainMenu waitFourMinutesToLogoutPopup();

    @Wait(explicitWaitSeconds = 300)
    SuccessfullyLogoutPage waitFiveMinutesToLogout();

    @Click(CONTINUE_WORK_BUTTON)
    HorizontalMainMenu clickOnContinueWorking();

    @Click(STOP_WORKING_BUTTON)
    SuccessfullyLogoutPage clickOnStopWorking();
}
