package cn.com.origin.autocodemanager.common;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import javax.swing.ImageIcon;
import com.teamcenter.rac.common.TCUtilities;
import com.teamcenter.rac.common.create.DisplayTypeInfo;
import com.teamcenter.rac.kernel.TCComponent;
import com.teamcenter.rac.kernel.TCComponentGroup;
import com.teamcenter.rac.kernel.TCComponentGroupMember;
import com.teamcenter.rac.kernel.TCComponentGroupMemberType;
import com.teamcenter.rac.kernel.TCComponentGroupType;
import com.teamcenter.rac.kernel.TCComponentRole;
import com.teamcenter.rac.kernel.TCException;
import com.teamcenter.rac.kernel.TCPreferenceService;
import com.teamcenter.rac.kernel.TCSession;

public class BusinessType {
	private TCSession session;
	protected Map typeToIcon;
	private String origin_codeNeedLoadItemStartWords = "origin_codeNeedLoadItemStartWords";
	private String origin_codeNeedLoadItemTypes = "origin_codeNeedLoadItemTypes";

	public BusinessType(TCSession session) {
		this.session = session;
	}

	public void getItemTypes(String rootType) {
		com.teamcenter.services.rac.core._2007_06.DataManagement.BaseClassInput abaseclassinput[] = new com.teamcenter.services.rac.core._2007_06.DataManagement.BaseClassInput[1];
		abaseclassinput[0] = new com.teamcenter.services.rac.core._2007_06.DataManagement.BaseClassInput();
		abaseclassinput[0].baseClass = rootType;
		try {
			HashMap	hashmap = (HashMap) TCUtilities.getDisplayableTypeNamesWithDisplayInfo(session, abaseclassinput);
		} catch (TCException e) {
			e.printStackTrace();
		}
	}
	
	public Map getTypes(String rootType, String exclusionTypes[]) {
		HashMap hashmap = new HashMap();
		com.teamcenter.services.rac.core._2007_06.DataManagement.BaseClassInput abaseclassinput[] = new com.teamcenter.services.rac.core._2007_06.DataManagement.BaseClassInput[1];
		abaseclassinput[0] = new com.teamcenter.services.rac.core._2007_06.DataManagement.BaseClassInput();
		abaseclassinput[0].baseClass = rootType;
		// abaseclassinput[0].exclusionTypes = exclusionTypes;
		// TCUtilities.getDisplayTypeInfoFromTypeNames(tcsession, as)
		try {
			hashmap = (HashMap) TCUtilities.getDisplayableTypeNamesWithDisplayInfo(session, abaseclassinput);
			
			Set key = hashmap.keySet();
			Iterator it = key.iterator();
			Set value = hashmap.entrySet();
			Iterator it2 = value.iterator();
		} catch (TCException e) {
			e.printStackTrace();
		}
		return hashmap;
	}

	/**
	 * 
	 * 获取系统用户组
	 * 
	 * @return
	 * @throws TCException
	 */
	public TCComponent[] getGroup() throws TCException {
		return ((TCComponentGroupType) session.getTypeComponent("Group")).extent();
	}

	/**
	 * 获取根组结构
	 * 
	 * @return
	 * @throws TCException
	 */
	public TCComponent[] getRootGroups() throws TCException {
		return ((TCComponentGroupType) session.getTypeComponent("Group")).getRootGroups();
	}

	/**
	 * 获取系统成员
	 * 
	 * @return
	 * @throws TCException
	 */
	public TCComponent[] getGroupaArrayList() throws TCException {

		TCComponent[] tccomponentGroups = null;
		TCComponentGroupMemberType groupMember = (TCComponentGroupMemberType) session.getTypeComponent("GroupMember");
		tccomponentGroups = groupMember.extent();
		if (tccomponentGroups != null && tccomponentGroups.length > 0) {
			for (int i = 0; i < tccomponentGroups.length; i++) {
				TCComponentGroupMember tccomponentGroupMembers = (TCComponentGroupMember) tccomponentGroups[i];
				TCComponentGroup group = tccomponentGroupMembers.getGroup();
			}
		}
		return tccomponentGroups;
	}

	public TCComponent[] getGroupRoles(TCComponentGroup componentGroup) {
		if (componentGroup == null) {
			return null;
		}
		TCComponent[] components = null;
		try {
			components = componentGroup.getRolesAndGroups();
		} catch (TCException e) {
			e.printStackTrace();
		}
		return components;
	}

	public TCComponent[] getRoleUsers(TCComponentGroup componentGroup, TCComponentRole role) {
		if ((componentGroup == null) || (role == null)) {
			return null;
		}

		TCComponent[] components = null;
		try {
			components = role.getUsers(componentGroup);
		} catch (TCException e) {
			e.printStackTrace();
		}
		return components;
	}

	public ImageIcon getSelectedTypeIcon(String s) {
		ImageIcon imageicon = null;
		if (typeToIcon == null)
			typeToIcon = new HashMap(0);
		imageicon = (ImageIcon) typeToIcon.get(s);
		return imageicon;
	}

	public Map loadTypes(String s1[], String s2[]) {
		Map map = buildInput(s1, s2);
		Map newHashMap = new HashMap();
		long time11 = System.nanoTime();
		//Map hashMap = TCUtilities.loadSubClassesTypesAndIcons(session, map);
//		20130913  mod
		Map hashMap = TCUtilities.loadSubTypesAndIcons(session, map);
		long time12 = System.nanoTime();
		System.out.println("加载所有类型用时(ms)： " + (time12 - time11) / 1000000);
		if (hashMap != null && hashMap.size() > 0) {
			Set<?> entry = hashMap.entrySet();
			Object entryValue[] = entry.toArray();
			Set<?> key = hashMap.keySet();
			Object keyValue[] = key.toArray();
			for (int m = 0; m < keyValue.length; m++) {
				DisplayTypeInfo displaytypeinfo = (DisplayTypeInfo) keyValue[m];
				String typeName = displaytypeinfo.getTypeName();
				// String[] startWithTypes = AutoCodeConstants.startWithTypes;
				String[] needLoadItemTypes = TCPreferenceUitl.getPreference(session, TCPreferenceService.TC_preference_site, origin_codeNeedLoadItemTypes);
				String[] startWithTypes = TCPreferenceUitl.getPreference(session, TCPreferenceService.TC_preference_site, origin_codeNeedLoadItemStartWords);
				if (needLoadItemTypes.length > 0) {
					for (int i = 0; i < needLoadItemTypes.length; i++) {
						if (typeName.equals(needLoadItemTypes[i].trim())) {
							if (!newHashMap.containsKey(keyValue[m])) {
								newHashMap.put(keyValue[m], hashMap.get(keyValue[m]));
							}
						} else {
							continue;
						}
					}
				} else {
					if (startWithTypes.length > 0) {
						for (int i = 0; i < startWithTypes.length; i++) {
							// System.out.println("startWithTypes[i].trim()=================" + startWithTypes[i].trim());
							if (startWithTypes[i].trim().length() > 0) {
								if (typeName.startsWith(startWithTypes[i].trim())) {
									if (!newHashMap.containsKey(keyValue[m])) {
										newHashMap.put(keyValue[m], hashMap.get(keyValue[m]));
									}
								} else {
									continue;
								}
							}
						}
					} else {
						newHashMap.put(keyValue[m], hashMap.get(keyValue[m]));
					}
				}
			}
		}
		return newHashMap;
	}

	private Map buildInput(String s[], String ss[]) {
		HashMap hashmap = null;
		String as[] = s;
		if (as != null && as.length > 0) {
			hashmap = new HashMap(as.length);
			String as1[] = as;
			int i = as1.length;
			for (int j = 0; j < i; j++) {
				String s1 = as1[j];
				if (s1 == null || s1.trim().length() <= 0)
					continue;
				String as2[] = ss;
				ArrayList arraylist = new ArrayList(0);
				if (as2 != null && as2.length > 0) {
					String as3[] = as2;
					int k = as3.length;
					for (int l = 0; l < k; l++) {
						String s3 = as3[l];
						if (s3 != null && s3.trim().length() > 0)
							arraylist.add(s3.trim());
					}
				}
				hashmap.put(s1, arraylist);
			}
		}
		return hashmap;
	}
}
