package kr.co.kihyun.moumi;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.PropertyResourceBundle;
import java.util.ResourceBundle;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class MoumiConfig {
	private static final Logger LOG = LoggerFactory.getLogger(MoumiConfig.class);

	private static ResourceBundle moumiConfig;
	private static ResourceBundle messageBundle;
	private static Properties pmfProperties;
	private static Properties handyProperties;
	private static Properties ecrossProperties;

	static {
		try {
			MoumiConfig.setMoumiConfig(new PropertyResourceBundle(MoumiConfig.class.getResourceAsStream("/moumi.properties")));
		} catch (IOException e) {
			throw new RuntimeException("unable to read moumi.properties file.");
		}

		MoumiConfig.setMessageBundle(ResourceBundle.getBundle("moumi_messages"));
		LOG.debug("msg bundle: {}", MoumiConfig.getMessageBundle());

		MoumiConfig.setPmfProperties(new Properties());
		InputStream stream = MoumiConfig.class.getResourceAsStream("/moumi-pmf.properties");
		try {
			MoumiConfig.getPmfProperties().load(stream);
		} catch (IOException e) {
			throw new RuntimeException("unable to read moumi-pmf.properties file.");
		}

		MoumiConfig.setHandyProperties(new Properties());
		stream = MoumiConfig.class.getResourceAsStream("/handy-pmf.properties");
		try {
			MoumiConfig.getHandyProperties().load(stream);
		} catch (IOException e) {
			throw new RuntimeException("unable to read handy-pmf.properties file.");
		}

		MoumiConfig.setECrossProperties(new Properties());
		stream = MoumiConfig.class.getResourceAsStream("/ecross-pmf.properties");
		try {
			MoumiConfig.getECrossProperties().load(stream);
		} catch (IOException e) {
			throw new RuntimeException("unable to read ecross-pmf.properties file.");
		}
	}

	/******** 집계문서 접근권한 상수값 ********/
	@Deprecated
	public static final int PER = 0;
	@Deprecated
	public static final int DEPT = 1;
	@Deprecated
	public static final int PART = 2;
	@Deprecated
	public static final int ALL = 3;
	/******** 사용자 권한 상수 정의 *********/
	@Deprecated
	public static final int REPOPER = 0; // 보고자
	@Deprecated
	public static final int SUB_TOTPER = 1; // 집계자
	@Deprecated
	public static final int TOTPER = 2; // 상위집계자
	@Deprecated
	public static final int PART_REPOADM = 4; // 기관보고관리자
	@Deprecated
	public static final int REPOADM = 5; // 학교관리자	20170929 주석에 보고관리자를 학교관리자로 수정 by wonseok Lee
	@Deprecated
	public static final int SUB_TOTADM = 6; // 부서관리자	20170929 주석에 기관집계관리자를 부서관리자로 수정 by wonseok Lee
	@Deprecated
	public static final int SUB_SYSADM = 7; // 기관시스템관리자
	@Deprecated
	public static final int TOTADM = 8; // 집계관리자
	@Deprecated
	public static final int SYSADM = 9; // 시스템관리자

	public static ResourceBundle getMessageBundle() {
		return messageBundle;
	}

	public static String getInitParameter(String paramName) {
		return getMoumiConfig().getString(paramName);
	}

	/******** Setup values ********/
	public static File getRoot() {
		try {
			String moumiPath = MoumiConfig.class.getResource("MoumiConfig.class").getPath();
			String rootDirectory = moumiPath.substring(0, moumiPath.indexOf("WEB-INF")).replace("file:", "");
			LOG.debug("rootDir: {}", rootDirectory);
			return new File(rootDirectory);
		} catch (Exception e) {
			return new File("/tmp");
		}
	}

	static File getDir(String fileDir) {
		return MoumiConfig.getDir(new File(fileDir));
	}

	private static File getDir(File dir) {
		LOG.debug("dir.isDirectory: {}, dir.getParent(): {}, dir.getParent().exists(): {}, dir.getParentFile().isDirectory: {}",
				new Object[] {dir.isDirectory(), dir.getParent(), dir.getParentFile().exists(), dir.getParentFile().isDirectory()});
		if (!dir.isDirectory())
			if (!dir.mkdirs())
				throw new RuntimeException(dir + " is not a directory.");
		return dir;
	}

	public static File getFileDirectory() {
		try {
			return getDir(getInitParameter("moumi.dir.upload"));
		} catch (java.util.MissingResourceException mre) {
			return getDir(new File(getRoot(), "uploads"));
		}
	}

	public static File getTotRoot() {
		return getDir(new File(MoumiConfig.getRoot().getPath(), "totsys"));
	}

	public static File getTotPerRoot() {
		return getDir(new File(MoumiConfig.getTotRoot().getPath(), "totper"));
	}

	public static File getTotPerTmp() {
		return getDir(new File(MoumiConfig.getTotPerRoot().getPath(), "tmp"));
	}

	public static File getRepoPerRoot() {
		File repoperDir = getDir(new File(MoumiConfig.getTotRoot().getPath(), "repoper"));
		File fileDir = getDir(new File(repoperDir, "fileUpload"));
		return repoperDir;
	}

	public static File getRepoPerTmp() {
		return getDir(new File(MoumiConfig.getRepoPerRoot().getPath(), "tmp"));
	}

	public static File getBoardFileRoot() {
		return getDir(new File(getFileDirectory().getPath(), "board"));
	}

	public static File getDocFileRoot() {
		return getDir(new File(getFileDirectory().getPath(), "document"));
	}

	public static File getCsvFileRoot() {
		return getDir(new File(getFileDirectory().getPath(), "csv"));
	}

	public static File getSysAdmFileRoot() {
		File dir = getDir(new File(getFileDirectory().getPath(), "sysadm"));
		File deptDir = getDir(new File(dir, "dept"));
		File upDir = getDir(new File(dir, "upfiles"));
		return dir;
	}

	public static String getTitle() {
		return getMessageBundle().getString("moumi.message.title");
	}

	public static String getSysAdmIP() {
		return getInitParameter("moumi.sysAdmIp");
	}

	public static boolean getUseRecog() {
		return Boolean.parseBoolean(getInitParameter("moumi.use.recog"));
	}

	public static boolean getUsePasswdEncrypt() {
		return Boolean.parseBoolean(getInitParameter("moumi.use.passwdEncrypt"));
	}

	public static boolean isSysAdmLoginConstraint() {
		return Boolean.parseBoolean(getInitParameter("moumi.isSysAdmLoginConstraint"));
	}

	public static boolean getUseAssign() {
		return Boolean.parseBoolean(getInitParameter("moumi.use.assign"));
	}

	public static boolean is2Step() {
		return Boolean.parseBoolean(getInitParameter("moumi.is2Step"));
	}

	public static boolean isMonitor() {
		return Boolean.parseBoolean(getInitParameter("moumi.isMonitor"));
	}

	public static boolean is2StepDirect() {
		return Boolean.parseBoolean(getInitParameter("moumi.is2StepDirect"));
	}

	public static boolean isOnLeftMenu() {
		return Boolean.parseBoolean(getInitParameter("moumi.turnOnLeftMenu"));
	}

	public static boolean isOnTopMenu() {
		return Boolean.parseBoolean(getInitParameter("moumi.turnOnTopMenu"));
	}

	public static String getDtdUri() {
		String dtdUri = "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd";
		return getInitParameter("xhtml.dtd") == null ? dtdUri : getInitParameter("xhtml.dtd");
	}

	public static String getMessengerServerAddress() {
		return getInitParameter("im.address");
	}

	public static int getMessengerServerPort() {
		return Integer.parseInt(getInitParameter("im.port"));
	}

	public static Properties getPMFProperties() {
		return getPmfProperties();
	}

	public static void setPmfProperties(Properties pmfProperties) {
		MoumiConfig.pmfProperties = pmfProperties;
	}

	public static Properties getPmfProperties() {
		return pmfProperties;
	}

	public static void setMoumiConfig(ResourceBundle moumiConfig) {
		MoumiConfig.moumiConfig = moumiConfig;
	}

	public static ResourceBundle getMoumiConfig() {
		return moumiConfig;
	}

	public static void setMessageBundle(ResourceBundle messageBundle) {
		MoumiConfig.messageBundle = messageBundle;
	}

	public static void setHandyProperties(Properties handyProperties) {
		MoumiConfig.handyProperties = handyProperties;
	}

	public static Properties getHandyProperties() {
		return handyProperties;
	}

	public static void setECrossProperties(Properties ecrossProperties) {
		MoumiConfig.ecrossProperties = ecrossProperties;
	}

	public static Properties getECrossProperties() {
		return ecrossProperties;
	}

}
