package cn.com.origin.autocode.jfom.util;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import com.teamcenter.rac.kernel.TCComponent;
import com.teamcenter.rac.kernel.TCComponentDataset;
import com.teamcenter.rac.kernel.TCComponentFormType;
import com.teamcenter.rac.kernel.TCComponentItemRevisionType;
import com.teamcenter.rac.kernel.TCComponentItemType;
import com.teamcenter.rac.kernel.TCComponentTcFile;
import com.teamcenter.rac.kernel.TCException;
import com.teamcenter.rac.kernel.TCPreferenceService;
import com.teamcenter.rac.kernel.TCSession;
import com.teamcenter.rac.util.FileUtility;

public class JFomMethodUtil {

	
	/**
	 * 下载文件
	 * 
	 * @param comps
	 * @return
	 * @throws TCException
	 * @throws IOException
	 */
	public static String downLoadFile(TCComponent comp) {
		if (comp == null) {
			return "";
		}
		String value = "";
		String tempPath = System.getenv("TEMP");
		// MessageBox.post(" tempPath =
		// "+tempPath,"INFO",MessageBox.INFORMATION);
		if (tempPath == null) {
			tempPath = "";
		} else if (!tempPath.endsWith("\\")) {
			tempPath = tempPath + "\\";
		}
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
		// for(TCComponent comp : comps){
		try {
			if (comp instanceof TCComponentDataset) {
				TCComponentTcFile[] tcFiles = ((TCComponentDataset) comp)
						.getTcFiles();
				File file = null;
				if (tcFiles != null && tcFiles.length != 0) {
					file = tcFiles[0].getFmsFile();
					String fileName = file.getName();
					String fileDix = fileName.substring(
							fileName.lastIndexOf("."), fileName.length());
					fileName = tempPath + sdf.format(new Date()) + fileDix;
					File dirFile = new File(fileName);
					FileUtility.copyFile(file, dirFile);

					return fileName;
				}
			}
		} catch (TCException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// }
		return value;
	}
	
	/**
	 * 获取首选项的值
	 * @param prefName
	 * @param session
	 * @return
	 */
	public static String getOptionValue(String prefName, TCSession session) {
		String value = null;
		TCPreferenceService service = session.getPreferenceService();
		 value = service.getString(TCPreferenceService.TC_preference_site, prefName);
		
		return value;
	}
	
	/**
	 * 获取mapping表
	 * name:value
	 * @param prefName
	 * @param session
	 * @return
	 */
	public static HashMap<String, String> getMapping(String prefName, TCSession session) {
		HashMap<String, String> map = new HashMap<String, String>();
		TCPreferenceService service = session.getPreferenceService();
		String[] prefValues = service.getStringArray(TCPreferenceService.TC_preference_site, prefName);
		if (prefValues != null) {
			for (String value : prefValues) {
				String[] values = value.split("\\:");
				if (values != null && values.length == 2)
					map.put(values[0], values[1]);
			}
		}
		return map;
	}
	
	/**
	 * 获取BZJ的FORM属性对照表
	 * TYPE|ZH:CN/ZH:CN/zh:en
	 * @param prefName
	 * @param session
	 * @return
	 */
	public static HashMap<String,JFomBZJBean> getBZJFormMapping(String prefName, TCSession session){
		HashMap<String,JFomBZJBean> formMap = new HashMap<String, JFomBZJBean>();
		//HashMap<String, String> map = new HashMap<String, String>();
		TCPreferenceService service = session.getPreferenceService();
		String[] prefValues = service.getStringArray(TCPreferenceService.TC_preference_site, prefName);
		if (prefValues != null) {
			for (String value : prefValues) {
				JFomBZJBean bean = new JFomBZJBean();
				List<String> zhNameList = new ArrayList<String>();
				List<String> enNameList = new ArrayList<String>();
				List<String> lovNameList = new ArrayList<String>();
				String keyStr = null;
				String[] values = value.split("\\|");
				if (values != null && values.length == 2){
					keyStr = values[0];
					String[] values2 = values[1].split("\\/");
					if(values2!=null && values2.length>0){
						for(String names : values2){
							String[] values3 = names.split("\\:");
							if(values3!=null && values3.length==2){
								zhNameList.add(values3[0]);
								enNameList.add(values3[1]);
								lovNameList.add("");
							}else if(values3!=null && values3.length==3){
								zhNameList.add(values3[0]);
								enNameList.add(values3[1]);
								lovNameList.add(values3[2]);
							}
						}
					}
				}
				bean.setZhName(zhNameList);
				bean.setEnName(enNameList);
				bean.setLovName(lovNameList);
				formMap.put(keyStr, bean);
			}
		}
		return formMap;
	}

	public static List<JFomBean> getTcPropInfo(String prefName, TCSession session) {
		List<JFomBean> beanList = new ArrayList<JFomBean>();
		TCPreferenceService service = session.getPreferenceService();
		// Item@true@Item,prop1:属性1/prop2:属性2|Rev,prop1:属性1/prop2:属性2|Form,prop1:属性1/prop2:属性2
		String[] prefValues = service.getStringArray(TCPreferenceService.TC_preference_site, prefName);
		if (prefValues != null) {
			for (String value : prefValues) {
				JFomBean bean = new JFomBean();
				List<String> propDisList = new ArrayList<String>();
				List<String> lovList = new ArrayList<String>();
				List<String> propTypeList = new ArrayList<>();
				List<String> propIsMustList = new ArrayList<>();
				List<String> propDefaultValueList = new ArrayList<>();
				
				String[] values1 = value.split("@");
				if (values1.length != 3) {
					return beanList;
				}
				// 设置类型
				bean.setItemType(values1[0]);
				if (values1[1].trim().toUpperCase().endsWith("TRUE")) {
					// 设置单位
					bean.setIsHaveUnit(true);
				}
				// 分割属性
				String[] values2 = values1[2].split("\\|");
				if (values2.length != 3) {
					return beanList;
				}
				
				// 对象属性
				String[] items = values2[0].split(",");
				if (items != null && items.length == 2) {
					String[] props1 = items[1].split("\\/");
					if (props1 != null && props1.length > 0) {
						List<String> propRelList = new ArrayList<String>();

						for (String prop : props1) {
							String[] props = prop.split("\\:");
							if (props != null && props.length == 5) {
								
								
								
								propRelList.add(props[0]);
								propDisList.add(props[1]);
								propTypeList.add(props[2]);
								propIsMustList.add(props[3]);
								propDefaultValueList.add(props[4]);
								lovList.add("");
							}else if(props != null && props.length == 6){
								
								propRelList.add(props[0]);
								propDisList.add(props[1]);
								propTypeList.add(props[2]);
								propIsMustList.add(props[3]);
								propDefaultValueList.add(props[4]);
								lovList.add(props[5]);
							}
						}
						bean.setItemRelPropList(propRelList);
						// bean.setDisPropList(propDisList);
					}
				}
				// 版本属性
				String[] revs = values2[1].split(",");
				if (revs != null && revs.length == 2) {
					String[] props1 = revs[1].split("\\/");
					if (props1 != null && props1.length > 0) {
						List<String> propRelList = new ArrayList<String>();
						// List<String> propDisList = new ArrayList<String>();
						for (String prop : props1) {
							String[] props = prop.split("\\:");
							if (props != null && props.length == 5) {
								
								propRelList.add(props[0]);
								propDisList.add(props[1]);
								propTypeList.add(props[2]);
								propIsMustList.add(props[3]);
								propDefaultValueList.add(props[4]);
								lovList.add("");
							}else if (props != null && props.length == 6) {
								
								propRelList.add(props[0]);
								propDisList.add(props[1]);
								propTypeList.add(props[2]);
								propIsMustList.add(props[3]);
								propDefaultValueList.add(props[4]);
								lovList.add(props[5]);
							}
						}
						bean.setRevRelPropList(propRelList);
						// bean.setDisPropList(propDisList);
					}
				}
				// 表单属性
				String[] forms = values2[2].split(",");
				if (forms != null && forms.length == 2) {
					String[] props1 = forms[1].split("\\/");
					if (props1 != null && props1.length > 0) {
						List<String> propRelList = new ArrayList<String>();
						// List<String> propDisList = new ArrayList<String>();
						for (String prop : props1) {
							String[] props = prop.split("\\:");
							if (props != null && props.length == 5) {
								
								propRelList.add(props[0]);
								propDisList.add(props[1]);
								propTypeList.add(props[2]);
								propIsMustList.add(props[3]);
								propDefaultValueList.add(props[4]);
								lovList.add("");
							}else if (props != null && props.length == 6) {
								
								propRelList.add(props[0]);
								propDisList.add(props[1]);
								propTypeList.add(props[2]);
								propIsMustList.add(props[3]);
								propDefaultValueList.add(props[4]);
								lovList.add(props[5]);
							}
						}
						bean.setFormRelPropList(propRelList);
						
						// bean.setDisPropList(propDisList);
					}
				}
				bean.setLovPropName(lovList);
				bean.setDisPropList(propDisList);
				bean.setPropType(propTypeList);
				bean.setPropIsMust(propIsMustList);
				bean.setDefaultValue(propDefaultValueList);
				beanList.add(bean);
			}
		}
		return beanList;
	}

}
