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

import cz.moneta.test.dsl.kasanova.KSNPageFlow;
import cz.moneta.test.dsl.kasanova.KSNUtils;
import cz.moneta.test.harness.endpoints.kasanova.KasanovaEndpoint;
import cz.moneta.test.harness.support.web.CustomAction;
import cz.moneta.test.harness.support.web.Lookup;
import cz.moneta.test.harness.support.web.SwitchToFrame;
import cz.moneta.test.harness.support.web.Until;
import org.junit.jupiter.api.Assertions;

public interface KSN95Page extends KSNPageFlow<KSN95Page>, KSNUtils<KSN95Page> {

    @CustomAction
    default KSNMainMenu executeStartDay(String tillId) {

        String ROOT_MENU = "//div[@id='root_menu']";
        String IFRAME = "//iframe[@id='ifr']";
        String MENU_09_BUTTON = "//button[text()='Údržbové operace']";
        String MENU_09_SUBMENU_01_BUTTON = "//td[text()='Údržbové operace']//ancestor::tbody//button[text()='Start dne']";
        String FUNCTION_TITLE = "//div[@class='cTitle']";
        String START_DAY_TILL_ID = "//input[@name='tillId']";
        String START_DAY_TILL_CASH_BILANCE = "//input[@name='tillCacheBilance']";
        String START_DAY_BTN_CONTINUE = "//button[text()='Pokračovat']";
        String START_DAY_BTN_OK = "//button[@accesskey='O']";
        String START_DAY_FUNCTION_TITLE = "91 - Start dne";
        String BACK_TO_MAIN_MENU = "//button[@accesskey='E']";
        String CELL_TILL_SUMMARY = "//div[@class='cItem']//fieldset//table//tbody//tr[2]//td[3]";

        KasanovaEndpoint endpoint = getEndpoint(KasanovaEndpoint.class);
        endpoint.switchToDefaultContent();
        endpoint.switchToFrame(MAIN_IFRAME_XPATH, Lookup.XPATH);

        // Function 95 - "Celkový sumář pokladny"
        endpoint.waitForElementsToLoad(30, Lookup.XPATH, CELL_TILL_SUMMARY);
        String tillSummary = endpoint.getText(CELL_TILL_SUMMARY, Lookup.XPATH); // 1.390,95
        String tillSummaryWithoutDots = tillSummary.replaceAll("\\.", ""); // 1390,95
        String finalStringSummary = tillSummaryWithoutDots.substring(0, tillSummaryWithoutDots.length() - 3); // 1390
        endpoint.click(() -> BACK_TO_MAIN_MENU, Lookup.XPATH);

        // Function 91 - "Start dne"
        endpoint.waitForElementsToLoad(30, Lookup.XPATH, ROOT_MENU);
        endpoint.click(() -> MENU_09_BUTTON, Lookup.XPATH);
        endpoint.click(() -> MENU_09_SUBMENU_01_BUTTON, Lookup.XPATH);
        endpoint.waitForElementsToLoad(30, Lookup.XPATH, Until.PRESENT_IN_DOM, IFRAME);
        endpoint.switchToFrame(IFRAME, Lookup.XPATH);
        endpoint.waitForElementsToLoad(20, Lookup.XPATH, START_DAY_TILL_ID);
        endpoint.type(() -> START_DAY_TILL_ID, tillId, false, Lookup.XPATH);
        endpoint.type(() -> START_DAY_TILL_CASH_BILANCE, finalStringSummary, false, Lookup.XPATH);
        endpoint.click(() -> START_DAY_BTN_CONTINUE, Lookup.XPATH);
        endpoint.switchToDefaultContent();
        endpoint.switchToFrame(MAIN_IFRAME_XPATH, Lookup.XPATH);
        endpoint.waitForElementsToLoad(30, Lookup.XPATH, FUNCTION_TITLE);
        String titleStartDay = endpoint.getText(FUNCTION_TITLE, Lookup.XPATH);
        Assertions.assertEquals(START_DAY_FUNCTION_TITLE, titleStartDay);
        endpoint.click(() -> START_DAY_BTN_OK, Lookup.XPATH);
        return null;
    }

    @SwitchToFrame(MAIN_IFRAME_NAME)
    KSN95Page switchToMainFrame();

}
