package cz.moneta.test.dsl.kasanova;

import static cz.moneta.test.dsl.kasanova.components.KSNMainMenu.ROOT_MENU;

import org.junit.jupiter.api.Assertions;

import cz.moneta.test.harness.context.StoreAccessor;
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.Until;

public interface KSNStorno<A> extends KSNCommonElements, StoreAccessor {

    /**
     * Method for executing storno of various Kasanova functions.
     * @param isStornoOn - true/false - if you want execute storno or not
     * @param verifyAmount - amount of money
     * @param verifyAccount - account number
     * @param verifyUid - tellerId of verify user
     * @param verifySso - sso of verify user
     * @param verifyPass - password of verify user
     * @param reduceNumber - reduce last transaction number by given number
     * @return
     */
    @CustomAction
    default A doStorno(boolean isStornoOn, String verifyAmount, String verifyAccount, String verifyUid, String verifySso, String verifyPass, int reduceNumber) {

        if (!isStornoOn) {
            return null;
        }

        String LTN = "//div[@class='userPanel']//span[@id='ltn']";
        String MENU_09_BUTTON = "//button[text()='Údržbové operace']";
        String MENU_09_SUBMENU_06_BUTTON = "//td[text()='Údržbové operace']//ancestor::tbody//button[text()='Žurnál']";
        String BUTTON_JOURNAL_DETAIL_PRINT = "//form[@name='journalDetailForm']//button[@id='print']";
        KasanovaEndpoint endpoint = getEndpoint(KasanovaEndpoint.class);

        // main menu loading
        endpoint.switchToDefaultContent();
        endpoint.switchToFrame(MAIN_IFRAME_NAME);
        endpoint.waitForElementsToLoad(40, Lookup.XPATH, Until.PRESENT_IN_DOM, ROOT_MENU);

        // reading last transaction number from userPanel
        String lastTransactionNumber = endpoint.getText(LTN, Lookup.XPATH);
        if (lastTransactionNumber == null || lastTransactionNumber.isEmpty()) {
            Assertions.fail("lastTransactionNumber not found");
        }

        int lastTransactionNumberInt = Integer.parseInt(lastTransactionNumber) - reduceNumber;
        String STORNO_TRANSACTION_BUTTON_ID = "storno_transakcebtn" + lastTransactionNumberInt;

        // enter journal page
        endpoint.click(() -> MENU_09_BUTTON, Lookup.XPATH);
        endpoint.click(() -> MENU_09_SUBMENU_06_BUTTON, Lookup.XPATH);
        // click storno button in journal
        endpoint.waitForElementsToLoad(80, Until.GONE, LOADER_DIV);
        endpoint.waitForElementsToLoad(80, Until.VISIBLE, STORNO_TRANSACTION_BUTTON_ID);
        endpoint.click(() -> STORNO_TRANSACTION_BUTTON_ID);

        // click print button
        endpoint.waitForElementsToLoad(80, Until.GONE, LOADER_DIV);
        endpoint.waitForElementsToLoad(30, Lookup.XPATH, Until.VISIBLE, BUTTON_JOURNAL_DETAIL_PRINT);
        endpoint.click(() -> BUTTON_JOURNAL_DETAIL_PRINT, Lookup.XPATH);

        // verification
        endpoint.waitForElementsToLoad(80, Until.GONE, LOADER_DIV);
        endpoint.waitForAnyElementToLoad(30, Lookup.XPATH, BUTTON_KASADIALOG, VERIFY_DIALOG_IFRAME);
        endpoint.switchToFrame(VERIFY_DIALOG_IFRAME, Lookup.XPATH);
        endpoint.waitForElementsToLoad(30, Lookup.XPATH, Until.VISIBLE, VERIFY_DIALOG_AMOUNT);
        endpoint.type(() -> VERIFY_DIALOG_AMOUNT, verifyAmount, false, Lookup.XPATH);
        endpoint.type(() -> VERIFY_DIALOG_ACCOUNT, verifyAccount, false, Lookup.XPATH);
        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);
        endpoint.sleepSeconds(3);

        // click save button and accept alerts
        endpoint.waitForElementsToLoad(10, Until.CLICKABLE, BUTTON_SAVE_TMP);
        endpoint.click(() -> BUTTON_SAVE_TMP);

        //"Opravdu chcete stornovat tuto transakci ?"
        if (endpoint.isAlertPresent(30)) {
            endpoint.acceptAlert(1);
        }

        //"Transakce byla úspěšně stornována."
        endpoint.waitForElementsToLoad(80, Lookup.XPATH, Until.PRESENT_IN_DOM, BUTTON_KASADIALOG_OK);
        endpoint.click(() -> BUTTON_KASADIALOG_OK, Lookup.XPATH);

        // click back button in journal
        endpoint.waitForElementsToLoad(80, Until.VISIBLE, BUTTON_BACK);
        endpoint.click(() -> BUTTON_BACK);

        return null;
    }

}
