package com.eactive.eai.transformer.dao.excel;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import org.apache.commons.lang3.StringUtils;

import com.eactive.eai.common.util.Logger;

public class LayoutExcelLoadFormatManager {
	static Logger logger = Logger.getLogger(Logger.LOGGER_DEFAULT);

	private static LayoutExcelLoadFormatManager instance;
	public static String LAYOUT_EXCEL_LOAD_FORMAT_CONFIG = "excel.layout.load.format.config";
	private String DEFAULT_CONFIG_PATH = "./resources/excel-layout-load-format.properties";
	private String DEFAULT_CONFIG_FILE = "excel-layout-load-format.properties";

	private Map<String, String> map = new HashMap<>();;

	private LayoutExcelLoadFormatManager() {
		map = new HashMap<>();
	}

	public static LayoutExcelLoadFormatManager getInstance() {
		if (instance == null) {
			instance = new LayoutExcelLoadFormatManager();
			instance.init();
		}
		return instance;
	}

	public String getData(String name) {
		return map.get(name);
	}

	public int getInt(String name) {
		String s = map.get(name);
		int r = 0;
		try {
			r = Integer.parseInt(s);
		} catch (Exception e) {

		}
		return r;
	}

	@SuppressWarnings("rawtypes")
	public void init() {
		try {
			Properties config = loadConfigFile();
			logger.debug(config.toString());
			map = propertyToMap(config);
		} catch (Exception ex) {
			ex.printStackTrace();
			logger.error("init failed", ex);
		}
	}

	private HashMap<String, String> propertyToMap(Properties prop) {
		HashMap<String, String> retMap = new HashMap<>();
		for (Map.Entry<Object, Object> entry : prop.entrySet()) {
			retMap.put(String.valueOf(entry.getKey()).trim(), String.valueOf(entry.getValue()).trim());
		}
		return retMap;
	}

	private Properties loadConfigFile() throws Exception {
		String configFile = getConfigFile();
		return loadConfigFileProperties(configFile);
	}

	private Properties loadConfigFileProperties(String configFile) throws Exception {
		try {
			return loadFileProperties(configFile);
		} catch (Exception e) {
			logger.warn("LayoutExcelLoadFormatManager | loadFileProperties failed. - " + configFile + ", "
					+ e.getMessage());
		}
		// try classpath file
		return loadClassPathProperties(configFile);
	}

	private Properties loadFileProperties(String filePath) throws Exception {
		logger.debug("configFile : ", filePath);
		Properties p = new Properties();
		try (InputStream in = new FileInputStream(filePath)) {
			p.load(in);
			return p;
		} catch (Exception e) {
			throw new Exception("LayoutExcelLoadFormatManager | Cannot load config file. - " + filePath, e);
		}
	}

	private Properties loadClassPathProperties(String filePath) throws Exception {
		Properties p = new Properties();
		ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
		logger.warn("LayoutExcelLoadFormatManager | ClassLoader : load. - " + filePath);
		try (InputStream in = classLoader.getResourceAsStream(filePath)) {
			p.load(in);
			return p;
		} catch (Exception e) {
			if (logger.isWarn()) {
				logger.warn("LayoutExcelLoadFormatManager | ClassLoader : Cannot load config file. - " + filePath + ", "
						+ e.getMessage());
			}
		}
		logger.warn("LayoutExcelLoadFormatManager | ClassLoader : load default - " + DEFAULT_CONFIG_FILE);
		try (InputStream in = classLoader.getResourceAsStream(DEFAULT_CONFIG_FILE)) {
			p.load(in);
			logger.warn("LayoutExcelLoadFormatManager | ClassLoader : load default success - " + DEFAULT_CONFIG_FILE);
			return p;
		} catch (Exception e) {
			logger.error("LayoutExcelLoadFormatManager | ClassLoader : Cannot load default config file. - "
					+ DEFAULT_CONFIG_FILE, e);
			throw new Exception("LayoutExcelLoadFormatManager | ClassLoader : Cannot load default config file. - "
					+ DEFAULT_CONFIG_FILE, e);
		}
	}

	private String getConfigFile() {
		String configFilePath = System.getProperty(LAYOUT_EXCEL_LOAD_FORMAT_CONFIG);
		if (StringUtils.isEmpty(configFilePath)) {
			return DEFAULT_CONFIG_PATH;
		} else {
			return configFilePath;
		}
	}

	public static void main(String[] args) throws Exception {
		LayoutExcelLoadFormatManager.getInstance();
	}
}
