package com.eactive.eai.rms.common.startup;

import com.eactive.eai.rms.common.util.CommonConstants;
import com.eactive.eai.rms.env.EmsConfig;
import com.eactive.eai.rms.onl.common.service.AdapterStatusSendThreadPool;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;

public class AppInitializer implements InitializingBean, DisposableBean {

	final Logger logger = Logger.getLogger(AppInitializer.class);

	boolean debug = true;

	private String instName;

	private String tableOwner;

	private String systemMode = "D"; // EAI System Mode : [P/T/D/S]

	private String themeColor = "black";

	private String drMode = "";

	private String eaiDatasourceType = "DEV"; // 데이터 소스 타입 :[DEV:개발/STG:스테이징/PRD:운영] ...NSG:내부서버

	public void setInstName(String instName) {
		this.instName = instName;
	}

	public void setTableOwner(String tableOwner) {
		this.tableOwner = tableOwner;
	}

	public void setSystemMode(String systemMode) {
		this.systemMode = systemMode;
	}

	public void setThemeColor(String themeColor) {
		this.themeColor = themeColor;
	}

	public void setDrMode(String drMode) {
		this.drMode = drMode;
	}

	public void setEaiDatasourceType(String eaiDatasourceType) {
		this.eaiDatasourceType = eaiDatasourceType;
	}

	private void init() {
		if (debug) {
			logger.debug("applicationInitialized");
		}
		// MS949로 default encoding 고정
		// System.setProperty("file.encoding","MS949");
		// Field charset = null;
		// try {
		// charset = Charset.class.getDeclaredField("defaultCharset");
		// charset.setAccessible(true);
		// charset.set(null, null);
		// } catch (Exception e) {
		// e.printStackTrace();
		// }

		// set default value
		String serverName = System.getProperty(EmsConfig.INST_NAME);
		if (serverName == null) {
			System.setProperty(EmsConfig.INST_NAME, instName);
		}

		String tOwner = System.getProperty(EmsConfig.TABLE_OWNER);
		if (tOwner == null) {
			System.setProperty(EmsConfig.TABLE_OWNER, tableOwner);
		}

		// 서버 구분 설정(개발:D, 테스트:T, 온라인:P)
		CommonConstants.SERVER_TYPE = systemMode;

		// 모니터링 테마 적용
		String tColor = System.getProperty(EmsConfig.THEME_COLOR);
		if (tColor == null || "".equals(tColor)) {
			System.setProperty(EmsConfig.THEME_COLOR, themeColor);
		}

		if (debug) {
			logger.debug(EmsConfig.DR_MODE + " = " + drMode);
		}
		// env 프라퍼티 파일만 있음 dr 여부 셋팅
		System.setProperty(EmsConfig.DR_MODE, drMode);

		// 데이터 소스타입(DEV:개발 / STG:스테이징 / PRD:운영 ... )
		String eDatasourceType = System.getProperty(EmsConfig.DATASOURCE_TYPE);
		if (eDatasourceType == null || "".equals(eDatasourceType)) {
			System.setProperty(EmsConfig.DATASOURCE_TYPE, eaiDatasourceType);
		}

		if (debug) {
			logger.debug(EmsConfig.DATASOURCE_TYPE + " =" + eaiDatasourceType); // env 프라퍼티
		}

		if (debug) {
			logger.debug("SERVER_TYPE =" + CommonConstants.SERVER_TYPE);
		}

		if (debug) {
			logger.debug("PortalSchemaProviderHolder initialized with tableOwner: " + tableOwner);
		}
	}

	public void destroy() {
		AdapterStatusSendThreadPool.destroy();
		if (debug) {
			logger.debug("############ e-link WebApplication stopping... preStop ############");
		}
	}

	@Override
	public void afterPropertiesSet() throws Exception {
		init();
	}
}
