package com.enactor.pos;

import java.util.HashMap;
import java.util.Map;

import com.enactor.core.automation.AUT.AUTConfig;
import com.enactor.core.utilities.ReflectionException;
import com.enactor.core.utilities.ReflectionFunctions;
import com.enactor.core.utilities.StringUtils;
import com.enactor.pos.swing.SwingPosStepsProvider;
import com.enactor.pos.web.WebPosStepsProvider;
import com.enactor.pos.web.embedded.EmbeddedChromiumBrowserStepsProvider;

/**
 * Automation test manager to share the objects between steps in different steps classes.
 *
 * @author Thushara.Ramasinghe
 */
public class PosTestManager {

	private static final String POS_APPLICATION_TYPE = "POS_APPLICATION_TYPE";
	private static final String STEPS_PROVIDER_CLASS_NAME = "STEPS_PROVIDER_CLASS_NAME";

	private enum PosApplicationType {
		WEB,
		SWING,
        HTML_FIXED
	}

	private static PosTestManager instance;

	private IPosStepsProvider stepsProvider;
	private static AUTConfig config;

	private Map<String, String> properties;

	public static PosTestManager getInstance() {
		if (instance == null) {
			instance = new PosTestManager();
		}
		return instance;
	}

	private PosTestManager() {
		init();
	}

    private void init() {
        properties = new HashMap<>();
        config = AUTConfig.getInstance();
        stepsProvider = createStepsProvider();
    }

    private IPosStepsProvider createStepsProvider() {
		String stepsProviderClassName = config.getString(STEPS_PROVIDER_CLASS_NAME);
		System.out.println("dhskgfhksgfhs -------------------------------->" + stepsProviderClassName);
    	if(StringUtils.notEmpty(stepsProviderClassName)) {
    		try {
				return (IPosStepsProvider) ReflectionFunctions.createObject(stepsProviderClassName);
			} catch (ReflectionException e) {
				e.printStackTrace();
			}
    	}
		String applicationType = config.getString(POS_APPLICATION_TYPE);
		if(PosApplicationType.HTML_FIXED.name().equalsIgnoreCase(applicationType)) {
			return new EmbeddedChromiumBrowserStepsProvider();
		} else if (PosApplicationType.WEB.name().equalsIgnoreCase(applicationType)) {
			return new WebPosStepsProvider();
		}
		return new SwingPosStepsProvider();
	}

	/**
	 * @return
	 */

	public IPosStepsProvider getStepsProvider() {
		return stepsProvider;
	}

	/**
	 * @return
	 */
	public Map<String, String> getProperties() {
		return properties;
	}

	public AUTConfig getConfig() {
		return config;
	}

	public static String getWebPosDeviceId() {
		return config.get("webPosDeviceId", "aut.WebPOS1@0001.enactor");
	}

	public static String getScreenshotURLBase() {
		return config.get("autScreenshotURLBase", "");
	}

	public static String getWebPosHostName() {
		return config.get("webPosHostName", "");
	}

	public static int getWaitingTimeForHardRestartInMillis() {
		return Integer.valueOf(config.get("waitingTimeForHardRestartInMillis", "120000"));
	}
}
