package com.connor.erp.toolkit;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Vector;

import cn.com.origin.autocodemanager.common.encrypt.EncryptOperations;
import cn.com.origin.autocodemanager.common.operations.TreeAndListCodeOperation;

import com.teamcenter.rac.kernel.TCPreferenceService;
import com.teamcenter.rac.kernel.TCSession;

public class TCPreferenceUitl {

	public static String origin_isListValueCanFilter = "origin_isListValueCanFilter";
	
	public String getEnv(String name) {
		String str = "";
		Iterator it = System.getenv().entrySet().iterator();
		while (it.hasNext()) {
			Map.Entry entry = (Map.Entry) it.next();
			// logger.info("entry-" + entry.getKey() + ":" + entry.getValue());
			if (entry.getKey().toString().equals(name)) {
				str = entry.getValue() + "";
				if (!str.endsWith("\\")) {
					str = str + "\\";
				}
				break;
			}
		}
		return str;
	}

	/**
	 * @param tcsession
	 * @param preferenceName
	 * @param isSelectName
	 * @return
	 */
	public static boolean isSelectExsitToTCPreferenceValues(TCSession tcsession, String preferenceName, String isSelectName) {
		boolean flag = false;
		TCPreferenceService preferenceService = tcsession.getPreferenceService();
		String preferenceValues[] = preferenceService.getStringArray(TCPreferenceService.TC_preference_site, preferenceName);
		if (preferenceValues == null) {
			return false;
		}
		if (preferenceValues != null && preferenceValues.length > 0) {
			for (int i = 0; i < preferenceValues.length; i++) {
				// System.out.println(preferenceValues[i]);
				if (preferenceValues[i].trim().equals(isSelectName)) {
					flag = true;
					return flag;
				}
			}
		}
		return flag;
	}

	private static boolean isWriteExpire = false;

	public static synchronized boolean isWriteExpire() {
		return isWriteExpire;
	}

	public static synchronized void setWriteExpire(boolean isWriteExpire) {
		TCPreferenceUitl.isWriteExpire = isWriteExpire;
	}

	public static boolean isExpire() {
		// tyl
		boolean isExpire = true;
		if (!TCPreferenceUitl.isWriteExpire) {
			TCPreferenceUitl.setWriteExpire(true);
			int value = new TreeAndListCodeOperation().isDateExpire();
			if (value == 0) {
				isExpire = false;
			} else {
				isExpire = true;
			}
			EncryptOperations.setLicenseExpire(isExpire);
		}
		return (EncryptOperations.isLicenseExpire());
		// tyl
		// return false;
	}

	public static boolean isTrueTCPreferenceValue(TCSession tcsession, String preferenceName) {
		boolean flag = false;
		TCPreferenceService preferenceService = tcsession.getPreferenceService();
		String value = preferenceService.getString(TCPreferenceService.TC_preference_site, preferenceName);
		if (value == null) {
			return false;
		}
		if (value.equalsIgnoreCase("true")) {
			flag = true;
		}
		return flag;
	}

	public static String getTCPreferenceValue(TCSession tcsession, String preferenceName) {
		TCPreferenceService preferenceService = tcsession.getPreferenceService();
		String value = preferenceService.getString(TCPreferenceService.TC_preference_site, preferenceName);
		return value;
	}

	public static String[] getListPreference(TCSession session, int scope, String pid) {
		try {
			String options[] = session.getPreferenceService().getStringArray(scope, pid);
			return options;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public static HashMap<String, String> getPreferenceHashMap(TCSession session, int scope, String preferenceName) {
		HashMap<String, String> hm = new HashMap<String, String>();
		Vector<String> preferencekeyNames = new Vector<String>();
		try {
			String options[] = getPreference(session, scope, preferenceName);
			if (options == null) {
				return hm;
			}
			if (options.length == 0) {
				return null;
			}
			String key = "";
			String value = "";
			for (int i = 0; i < options.length; i++)
				if (options[i].indexOf("=") > 0) {
					key = options[i].substring(0, options[i].indexOf("="));
					value = options[i].substring(options[i].indexOf("=") + 1);
					hm.put(key, value);
					preferencekeyNames.add(key);
				} else {
					hm.put((new StringBuilder()).append(i).toString(), options[i]);
				}
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return hm;
	}

	public static String[] getPreference(TCSession session, int scope, String pid) {
		try {
			String options[] = session.getPreferenceService().getStringArray(scope, pid);
			return options;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}
