package cn.com.origin.autocode.newitem.origin;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import cn.com.origin.autocodemanager.common.AutoCodeManagerPropertyName;
import cn.com.origin.autocodemanager.common.PackCodeDescInfos;
import cn.com.origin.autocodemanager.common.TCPreferenceUitl;
import cn.com.origin.autocodemanager.common.tree.AbstractTreeData;

import com.teamcenter.rac.aifrcp.AifrcpPlugin;
import com.teamcenter.rac.kernel.TCComponent;
import com.teamcenter.rac.kernel.TCComponentForm;
import com.teamcenter.rac.kernel.TCComponentItem;
import com.teamcenter.rac.kernel.TCComponentItemRevision;
import com.teamcenter.rac.kernel.TCException;
import com.teamcenter.rac.kernel.TCPreferenceService;
import com.teamcenter.rac.kernel.TCProperty;
import com.teamcenter.rac.kernel.TCSession;
import com.teamcenter.rac.kernel.TCUserService;

public class ORSaveObjectDescOperation {
	private TCComponent newComp;
	private TCSession session;
	private String origin_codeNameMatchRevFormProperty = "origin_codeNameMatchRevFormProperty";
	private TCUserService userService;

	public ORSaveObjectDescOperation(TCComponent newComp) {
		this.newComp = newComp;
		getSession();
	}

	private TCSession getSession() {
		if (session == null)
			session = (TCSession) AifrcpPlugin.getSessionService().getDefaultSession();
		return session;
	}

	public void saveUom_tag(TCComponent uom_tag) {
		if ((uom_tag == null) || (this.newComp == null)) {
			return;
		}
		try {
			TCProperty prop = newComp.getTCProperty(AutoCodeManagerPropertyName.Item_uom_tag);
			if (prop != null) {
				prop.setReferenceValue(uom_tag);
			}
		} catch (TCException e) {
			e.printStackTrace();
		}
	}

	public void saveObject_Desc(PackCodeDescInfos packCodeDescInfos) {
		if (this.newComp != null) {
			long time21 = System.nanoTime();
			try {
				if (packCodeDescInfos == null) {
					return;
				}
				AbstractTreeData selectedSegmentNode = packCodeDescInfos.getSelectedSegmentNode();
				if (selectedSegmentNode == null) {
					return;
				}
				String generateID = packCodeDescInfos.getGenerateID();
				String idDesc = packCodeDescInfos.getIdDesc();
				String idjl = packCodeDescInfos.getIdjl();
				String codetz = packCodeDescInfos.getCodetz();
				String codeStyle = packCodeDescInfos.getCodeStyle();
				String ruleName = selectedSegmentNode.getPackCodeNodeInfo().getNode_name();
				List<String> propNames = new ArrayList<String>();
				List<String> propValues = new ArrayList<String>();
				if ((idjl != null) && !("".equals(idjl))) {
					propNames.add(AutoCodeManagerPropertyName.Item_codeID);
					propValues.add(idjl.trim());
					// savePropertyValue(AutoCodeManagerPropertyName.Item_codeID, idjl, newComp);
				}
				if ((generateID != null) && !("".equals(generateID))) {
					propNames.add(AutoCodeManagerPropertyName.Item_generateID);
					propValues.add(generateID.trim());
					// savePropertyValue(AutoCodeManagerPropertyName.Item_generateID, generateID, newComp);
				}
				if ((idDesc != null) && !("".equals(idDesc))) {
					propNames.add(AutoCodeManagerPropertyName.Item_codeDesc);
					propValues.add(idDesc.trim());
					// savePropertyValue(AutoCodeManagerPropertyName.Item_codeDesc, idDesc, newComp);
					saveCodeDescInfo(ruleName, idDesc);
				}
				if ((codetz != null) && !("".equals(codetz))) {
					propNames.add(AutoCodeManagerPropertyName.Item_codePattern);
					propValues.add(codetz.trim());
					// savePropertyValue(AutoCodeManagerPropertyName.Item_codePattern, codetz, newComp);
				}
				if ((ruleName != null) && !("".equals(ruleName))) {
					propNames.add(AutoCodeManagerPropertyName.Item_codeRuleName);
					propValues.add(ruleName.trim());
					// savePropertyValue(AutoCodeManagerPropertyName.Item_codeRuleName, ruleName, newComp);
				}
				if ((codeStyle != null) && !("".equals(codeStyle))) {
					propNames.add(AutoCodeManagerPropertyName.Item_codeStyle);
					propValues.add(codeStyle.trim());
					// savePropertyValue(AutoCodeManagerPropertyName.Item_codeStyle, codeStyle, newComp);
				}
				saveComponentStringValues(newComp, propNames.toArray(), propValues.toArray());
			} catch (Exception e) {
				e.printStackTrace();
			}
			long time22 = System.nanoTime();
			System.out.println("±£´æÃèÊöÓÃÊ±(ms)£º " + (time22 - time21) / 1000000);
		}
	}

	private void saveComponentStringValues(TCComponent component, Object[] propNames, Object propValues[]) {
		try {
			if (userService == null) {
				userService = (TCUserService) session.getUserService();
			}
			Object[] params = { component, propNames, propValues };
			userService.call("saveComponentStringValues", params);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void saveCodeDescInfo(String ruleName, String idDesc) {
		try {
			List<String> propNames1 = new ArrayList<String>();
			List<String> propValues1 = new ArrayList<String>();
			String[] codeDesc1 = idDesc.split("\n");
			String codeValueProp = "cd9_codeValue";
			String codeValueDescProp = "cd9_codeValueDesc";
			for (int i = 0; i < codeDesc1.length; i++) {
				String codeDesc = codeDesc1[i];
				String[] codeDesc2 = codeDesc.split("::");
				if (codeDesc2.length == 2) {
					// savePropertyValue(codeValueProp + (i + 1), codeDesc2[0], newComp);
					// savePropertyValue(codeValueDescProp + (i + 1), codeDesc2[1], newComp);
					propNames1.add(codeValueProp + (i + 1));
					propValues1.add(codeDesc2[0].trim());
					propNames1.add(codeValueDescProp + (i + 1));
					propValues1.add(codeDesc2[1].trim());
				}
			}
			if (propNames1.size() > 0) {
				saveComponentStringValues(newComp, propNames1.toArray(), propValues1.toArray());
			}
			if (!(newComp instanceof TCComponentItem)) {
				return;
			}
			HashMap<String, String> values = TCPreferenceUitl.getPreferenceHashMap(session, TCPreferenceService.TC_preference_site, origin_codeNameMatchRevFormProperty);
			if ((values != null) && (values.size() > 0)) {
				TCComponentItem newItem = (TCComponentItem) newComp;
				TCComponentItemRevision newItemRev = newItem.getLatestItemRevision();
				if (newItemRev == null) {
					return;
				}
				if (newItemRev.getRelatedComponent("IMAN_master_form_rev") == null) {
					return;
				}
				TCComponentForm form = (TCComponentForm) newItemRev.getRelatedComponent("IMAN_master_form_rev");
				String type = newComp.getType().trim();
				String typeInfo = type + "&&" + ruleName;
				String propInfo = values.get(typeInfo.trim());

				if ((propInfo != null) && (propInfo.trim().length() > 0)) {
					// List<String> propNames2 = new ArrayList<String>();
					// List<String> propValues2 = new ArrayList<String>();

					String[] propInfos = propInfo.split(";");
					for (int i = 0; i < propInfos.length; i++) {
						if ((propInfos[i] != null) && (propInfos[i].trim().length() > 0)) {
							if (codeDesc1.length >= (i + 1)) {
								String codeDesc = codeDesc1[i];
								String[] codeDesc2 = codeDesc.split("::");
								if (codeDesc2.length == 2) {
									savePropertyValue(propInfos[i].trim(), codeDesc2[1].trim(), form);
									// propNames2.add(propInfos[i]);
									// propValues2.add(codeDesc2[1].trim());
									// System.out.println("propInfos==============" + propInfos[i]);
									// System.out.println("codeDesc2==============" + codeDesc2[1].trim());
								}
							}
						}
					}
					// if (propNames2.size() > 0) {
					// saveComponentStringValues(form, propNames2.toArray(), propValues2.toArray());
					// }
				}
			}
		} catch (TCException e) {
			e.printStackTrace();
		}
	}

	private void savePropertyValue(String propertyName, String propertyValue, TCComponent component) {
		try {
			TCProperty tcProperty = component.getTCProperty(propertyName.trim());
			if (tcProperty != null) {
				tcProperty.setStringValue(propertyValue);
			}
			component.save();
		} catch (TCException e) {
			e.printStackTrace();
		}
	}
}
