package com.enactor.pos.web.embedded;

import com.enactor.core.automation.AUT.AutomationException;
import com.enactor.core.automation.component.IComponentAccessor;
import com.enactor.core.automation.component.IWebBrowserAccessor;
import com.enactor.core.utilities.StringUtils;
import com.enactor.pos.PosTestManager;
import com.enactor.pos.swing.ISwingAppStepsProvider;
import com.enactor.pos.web.WebPosStepsProvider;
import org.openqa.selenium.By;

public class EmbeddedChromiumBrowserStepsProvider extends WebPosStepsProvider {

    private static final String BROWSER_COMPONENT_ID_KEY = "FULL_SCREEN_BROWSER_COMPONENT_NAME";
    private static final String BROWSER_COMPONENT_DEFAULT_VALUE = "JSON_THIN_CLIENT";
    private static final String POS_APPLICATION_WINDOW_KEY = "APPLICATION_WINDOW_PREFIX";
    private static final String POS_APPLICATION_DEFAULT_VALUE = "Point-of-Sale Application";

    @Override
    public void beforeTest() {
    }

    @Override
    public void afterTestCheckEnterUserScreenShown(String testName, boolean clearTestPrinter, boolean testFailed) throws Exception {
        try {
            TRANSACTION_IDS.clear();
            if (clearTestPrinter) {
                clearPrinter();
            }
            if (testFailed) {
                failTest(testName);
            }
            checkWindow(ENTER_USER_WINDOW_NAME);
        } catch (Exception e) {
            restartApplication();
        }
    }

    @Override
    public void checkWindow(String windowName, long timeout) throws AutomationException {
        if (isPeripheralWindow(windowName)) {
            swingAppStepsProvider.waitForWindow(getWindowId(windowName), timeout);
        } else {
            IComponentAccessor componentAccessor =
                    swingAppStepsProvider.waitsForComponent(
                            PosTestManager.getInstance().getConfig().get(BROWSER_COMPONENT_ID_KEY, BROWSER_COMPONENT_DEFAULT_VALUE),
                            PosTestManager.getInstance().getConfig().get(POS_APPLICATION_WINDOW_KEY, POS_APPLICATION_DEFAULT_VALUE),
                            timeout);
            if (componentAccessor instanceof IWebBrowserAccessor) {
                String currentURL = ((IWebBrowserAccessor) componentAccessor).getCurrentUrl();
                if (!StringUtils.isEmpty(currentURL)) {
                    getWebController().switchToPageWithGivenURL(currentURL);
                } else {
                    throw new AutomationException("Current URL of the browser is empty");
                }
            } else {
                throw new AutomationException("Instance of " + IWebBrowserAccessor.class + " is expected. But found " + componentAccessor);
            }
            String windowId = getWindowId(windowName);
            getWebController().waitForElement(By.id(windowId));
        }
    }

    @Override
    public void restartApplication() throws AutomationException {
        restartSwingApp(getSwingAppStepsProvider());
    }

    @Override
    protected void restartSwingApp(ISwingAppStepsProvider swingAppStepsProvider) throws AutomationException {
        super.restartSwingApp(swingAppStepsProvider);
        if (swingAppStepsProvider != null) {
            swingAppStepsProvider.waitForWindow(
                    PosTestManager.getInstance().getConfig().get(POS_APPLICATION_WINDOW_KEY, POS_APPLICATION_DEFAULT_VALUE),
                    WAITING_TIME_IN_MILLIS_HARD_RESTART);
            resetBrowserPool();
            waits(10000);
        }
    }
}
