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

import java.util.concurrent.TimeUnit;
import org.junit.jupiter.api.Assertions;
import cz.moneta.test.dsl.kasanova.KSNPageFlow;
import cz.moneta.test.dsl.kasanova.KSNUtils;
import cz.moneta.test.harness.endpoints.WebEndpoint;
import cz.moneta.test.harness.endpoints.kasanova.KasanovaEndpoint;
import cz.moneta.test.harness.support.web.Click;
import cz.moneta.test.harness.support.web.CustomAction;
import cz.moneta.test.harness.support.web.Lookup;
import cz.moneta.test.harness.support.web.Until;
import cz.moneta.test.harness.support.web.Wait;

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

    String CURRENCY_TITLE = "//legend[@class='csBilanceLegend']";
    String BUTTON_COUNT = "count";
    String BUTTON_CHECK_CASH = "checkCashBtn";

    default void loadPage(WebEndpoint endpoint) {
        KSNPageFlow.loadLocalPage(endpoint, "tillMain_result");
    }

    @CustomAction
    default KSN88Page checkCurrencyAndGoNext(String currency) {
        KasanovaEndpoint endpoint = getEndpoint(KasanovaEndpoint.class);
        String pageCurrency = "";
        for (int i = 0; i < 10; i++) {
            endpoint.waitForElementsToLoad(20, Lookup.XPATH, CURRENCY_TITLE);
            String title = endpoint.getText(CURRENCY_TITLE, Lookup.XPATH);
            String[] titleArray = title.split(" ");
            pageCurrency = titleArray[1];
            if (currency.equals(pageCurrency)) {
                break;
            } else {
                endpoint.sleep(TimeUnit.SECONDS, 2);
            }
        }
        Assertions.assertEquals(currency, pageCurrency);
        endpoint.click(() -> BUTTON_COUNT);

        fillTableBilance("csPapersBilance");

        if ("CZK".equals(pageCurrency)) {
            fillTableBilance("csCoinsBilance");
        }
        endpoint.click(() -> BUTTON_NEXT);
        return null;
    }

    @CustomAction
    default KSN88Page fillTableBilance(String moneyType) {
        KasanovaEndpoint endpoint = getEndpoint(KasanovaEndpoint.class);
        String tbodySection = "//div[@id='tillMain']//div[@class='" + moneyType + "']//table//tbody//tr";
        int rows = endpoint.findAndGetListOfElements(tbodySection, Lookup.XPATH).size();
        char minus = '-';

        for (int i = 2; i <= rows; i++) {
            String firstCellInput = "//div[@id='tillMain']//div[@class='" + moneyType + "']//table//tbody//tr[" + i + "]//td[1]//input";
            String lastCellInput = "//div[@id='tillMain']//div[@class='" + moneyType + "']//table//tbody//tr[" + i + "]//td[4]//input[@name='tillMainDiffH']";
            String lastCellInputValue = endpoint.getAttribute(lastCellInput, Lookup.XPATH, "value");

            if (!"0".equals(lastCellInputValue)) {
                if (minus == lastCellInputValue.charAt(0)) {
                    lastCellInputValue = lastCellInputValue.substring(1);
                    endpoint.type(() -> firstCellInput, lastCellInputValue, true, Lookup.XPATH);
                }
            }
        }
        return null;
    }

    @CustomAction
    default KSN88Page fillVerification(String verifyUid, String verifySso, String verifyPass) {
        KasanovaEndpoint endpoint = getEndpoint(KasanovaEndpoint.class);
        endpoint.waitForElementsToLoad(30, Lookup.XPATH, Until.PRESENT_IN_DOM, VERIFY_DIALOG_IFRAME);
        endpoint.switchToFrame(VERIFY_DIALOG_IFRAME, Lookup.XPATH);
        endpoint.waitForElementsToLoad(30, Lookup.XPATH, Until.PRESENT_IN_DOM, VERIFY_DIALOG_UID);
        endpoint.type(() -> VERIFY_DIALOG_UID, verifyUid, false, Lookup.XPATH);
        endpoint.type(() -> VERIFY_DIALOG_SSOID, verifySso, false, Lookup.XPATH);
        endpoint.type(() -> VERIFY_DIALOG_PASSWORD, verifyPass, false, Lookup.XPATH);
        endpoint.switchToDefaultContent();
        endpoint.switchToFrame(MAIN_IFRAME_NAME);
        endpoint.click(() -> VERIFY_BUTTON, Lookup.XPATH);
        return null;
    }

    @CustomAction
    default KSNMainMenu dismissPrintWindow(int timeout) {
        KasanovaEndpoint endpoint = getEndpoint(KasanovaEndpoint.class);
        if (endpoint.isAlertPresent(timeout)) {
            endpoint.dismissAlert(1);
        }
        return null;
    }

    @CustomAction
    default KSN88Page kasaDialogClickOK(int timeoutSeconds) {
        KasanovaEndpoint endpoint = getEndpoint(KasanovaEndpoint.class);
        endpoint.waitForElementsToLoad(timeoutSeconds, Lookup.XPATH, Until.PRESENT_IN_DOM, BUTTON_KASADIALOG);
        endpoint.click(() -> BUTTON_KASADIALOG_OK, Lookup.XPATH);
        return null;
    }

    @CustomAction
    default KSNMainMenu verificationInfoDialogClose() {
        KasanovaEndpoint endpoint = getEndpoint(KasanovaEndpoint.class);
        endpoint.waitForElementsToLoad(10, Lookup.XPATH, Until.PRESENT_IN_DOM, BUTTON_KASADIALOG);
        endpoint.click(() -> BUTTON_KASADIALOG_OK, Lookup.XPATH);
        return null;
    }

    @Click(BUTTON_NEXT)
    KSN88Page clickNext();

    // button "Kontrola hotovosti
    @Wait(value = BUTTON_CHECK_CASH, until = Until.CLICKABLE)
    @Click(BUTTON_CHECK_CASH)
    KSN88Page clickCashBalance();

}
