package com.enactor.pos;

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

import com.enactor.core.utilities.StringUtils;

/**
 * Configuration for the POS test data - read the property file and get the property values.
 * 
 * @author Charith Gunathilaka
 *
 */

public class TestDataConfig {
	
	/** Instance. */
	private static TestDataConfig instance = null;

	/**
	 * Resource bundle.
	 */
	private final Map<String, String> configProperties = new HashMap<>();

	/** POS Configuration Directory*/
	private String posConfigDirectory = null;
	
	private final String POS_CONFIG_DIRECTORY="autClient.configFolder";
		
	/**
	 * Returns the current instance of the AUTClientConfig.
	 *
	 * @return
	 */
	public static TestDataConfig getInstance() {
		if(instance == null){
			instance = new TestDataConfig();
		}
	    return instance;
	}

	/**
	 * Default constructor.
	 */
	private TestDataConfig() {

		String[] fileNames = StringUtils.splitCommaDelimitedString(getPropertiesFileNames());

		for (String fileName : fileNames) {
			ResourceBundle resourceBundle = ResourceBundle.getBundle(fileName);
			for (String key : resourceBundle.keySet()) {
				configProperties.put(key, resourceBundle.getString(key));
			}
		}

		posConfigDirectory = this.getConfigDirectoryPath();
	}

	/**
	 * Returns an item from the test data config.
	 *
	 * @param key
	 * @param defaultVal
	 * @return
	 */
	public String get(String key, String defaultVal) {
		// First try and retrieve system property
		String value = System.getProperty(key);
		if (StringUtils.isEmpty(value)) {
			value = configProperties.get(key);
		}
		if (StringUtils.isEmpty(value)) {
			return defaultVal;
		}
		return value;
	}
	
	public int getInt(String key, int defaultVal) {
		return Integer.valueOf(get(key,String.valueOf(defaultVal)));
	}
	
	/**
	 * Returns the string value for the given key.
	 * 
	 * @param key
	 * @return
	 */
	public String getString(String key) {
		return get(key, null);
	}
	
	/**
	 * Returns the int value for the given key.
	 * 
	 * @param key
	 * @return
	 */
	public int getInt(String key) {
		return getInt(key, 0);
	}
	
	/**
	 * Returns the properties file name.
	 * 
	 * @return
	 */
	private String getPropertiesFileNames() {
		String fileNames = System.getProperty("testDataPropertyFileNames");
		return StringUtils.isEmpty(fileNames)
				? System.getProperty("testDataPropertyFileName", "TestDataConfig")
				: fileNames;
	}
	
	/**
	 * Returns the POS Configuration directory path.
	 * 
	 * @return
	 */
	public String getConfigDirectoryPath(){
		return System.getProperty(POS_CONFIG_DIRECTORY);
	}

}
