package com.connor.erp.processresource.EquipmentAcquiredFromERP;

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.TCComponentItem;
import com.teamcenter.rac.kernel.TCSession;
import com.teamcenter.rac.kernel.ics.ICSView;
import com.teamcenter.rac.util.DateButton;
import com.teamcenter.rac.util.MessageBox;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import javax.swing.JComboBox;
import javax.swing.JTextField;

public class ORCreateICOObj {
  private TCSession session;
  
  private String rootICSID = "ICM";
  
  private String parentICSName = "";
  
  private String parentICSID = "";
  
  public ORCreateICOObj() {
    this.session = getSession();
  }
  
  public TCSession getSession() {
    if (this.session == null)
      this.session = (TCSession)AifrcpPlugin.getSessionService().getDefaultSession(); 
    return this.session;
  }
  
  private String getClassificationNode(AbstractTreeData treeData, String icsID) {
    AbstractTreeData parentNode = treeData.getParentTreeData();
    if (parentNode != null && parentNode.getPackCodeNodeInfo().getNodeComponent() != null) {
      icsID = getClassificationNode(parentNode, icsID);
      icsID = searchClassification(parentNode.getPackCodeNodeInfo().getNode_name(), icsID);
    } 
    return icsID;
  }
  
  private String getParentClassificationNode(AbstractTreeData treeData, String icsID) {
    String parentICSID = getClassificationNode(treeData, icsID);
    icsID = searchClassification(treeData.getPackCodeNodeInfo().getNode_name(), parentICSID);
    return icsID;
  }
  
  public void createTreeNodeICObject(AbstractTreeData parentTreeData, AbstractTreeData treeData, TCComponentItem item, Map<String, String> icoPropertyMap) {
    String parentICSID = getParentClassificationNode(parentTreeData, this.rootICSID);
    String icsID = searchClassification(treeData.getPackCodeNodeInfo().getNode_name(), parentICSID);
    if (icsID.trim().length() == 0) {
      MessageBox.post("物料创建成功，但没有找到特征码为：【"+ treeData.getPackCodeNodeInfo().getNode_name() + "】的分类", "提示", 2);
      return;
    } 
    createICObject(treeData.getPackCodeNodeInfo().getNode_name(), icsID, (TCComponent)item, icoPropertyMap);
  }
  
  public void createListNodeICObject(AbstractTreeData parentTreeData, String firstICOName, String currentICSName, TCComponentItem item, Map<String, String> icoPropertyMap) {
    String parentICSID = getParentClassificationNode(parentTreeData, this.rootICSID);
    String icsID = searchClassification(firstICOName.trim(), parentICSID);
    if (icsID.trim().length() == 0) {
      MessageBox.post("物料创建成功，但没有找到特征码为：【"+ currentICSName + "】的分类", "提示", 2);
      return;
    } 
    createICObject(firstICOName, icsID, (TCComponent)item, icoPropertyMap);
  }
  
  public boolean createICObject(ICSView icsView, PackClassificationInfos classificationInfos, TCComponent item) {
    if (classificationInfos == null)
      classificationInfos = new PackClassificationInfos(); 
    HashMap<String, Object> propHashMap = classificationInfos.getPropertyMaps();
    HashMap<String, Integer> nameToIdHashMap = classificationInfos.getNameToIdHashMap();
    HashMap<String, Object> propHashMap2 = new HashMap<String, Object>();
    HashMap<Integer, String> propHashMap3 = new HashMap<Integer, String>();
    String icsId = classificationInfos.getIcsID();
    if (propHashMap != null) {
      Set<String> propSet = propHashMap.keySet();
      Object[] keys = propSet.toArray();
      for (int i = 0; i < keys.length; i++) {
        if (keys[i] != null && keys[i].toString().trim().length() > 0) {
          Object propComponent = propHashMap.get(keys[i]);
          String value = "";
          if (propComponent instanceof JTextField) {
            value = ((JTextField)propComponent).getText();
          } else if (propComponent instanceof JComboBox) {
            value = ((JComboBox)propComponent).getSelectedItem().toString();
            if (value == null)
              continue; 
            String[] values = value.trim().split(" ");
            if (values.length == 3)
              value = values[0]; 
          } else if (propComponent instanceof DateButton) {
            value = ((DateButton)propComponent).getDate().toString();
          } 
          if (value != null) {
            propHashMap2.put((String)keys[i], value);
            propHashMap3.put(nameToIdHashMap.get(keys[i]), value);
          } 
        } 
        continue;
      } 
    } 
    classificationInfos.setPropertyMaps2(propHashMap3);
    try {
      boolean isSuccess = false;
      if (propHashMap2 == null || propHashMap2.size() == 0) {
        String[] names = new String[0];
        String[] values = new String[0];
        isSuccess = (new ORSendToIcsOperation()).sendItemToClassification((TCComponentItem)item, icsId, (Object[])names, values);
      } else {
        Set<String> set = propHashMap2.keySet();
        Object[] names = set.toArray();
        String[] values = new String[names.length];
        for (int i = 0; i < names.length; i++)
          values[i] = (String)propHashMap2.get(names[i]); 
        isSuccess = (new ORSendToIcsOperation()).sendItemToClassification((TCComponentItem)item, icsId, names, values);
      } 
      if (!isSuccess)
        return false; 
    } catch (Exception e1) {
      e1.printStackTrace();
      MessageBox.post("物料创建成功，但对象发送到分类失败！", "提示", 2);
      return false;
    } 
    return true;
  }
  
  public void createICObject(String currentICSName, String icsId, TCComponent item, Map<String, String> icoPropertyMap) {
    try {
      if (icoPropertyMap.size() == 0 || icoPropertyMap == null) {
        String[] names = new String[0];
        String[] values = new String[0];
        (new ORSendToIcsOperation()).sendItemToClassification((TCComponentItem)item, icsId, (Object[])names, values);
      } else {
        Set<String> set = icoPropertyMap.keySet();
        Object[] names = set.toArray();
        String[] values = new String[names.length];
        for (int i = 0; i < names.length; i++)
          values[i] = icoPropertyMap.get(names[i]); 
        (new ORSendToIcsOperation()).sendItemToClassification((TCComponentItem)item, icsId, names, values);
      } 
    } catch (Exception e1) {
      e1.printStackTrace();
      MessageBox.post("物料创建成功，但对象发送到分类失败！", "提示", 2);
    } 
  }
  
  public String searchClassification(String icsName, String rootId) {
    String icsId = (new ORSendToIcsOperation()).getClassificationByName(rootId, icsName);
    if (this.parentICSName.equals(icsName) && icsId.trim().length() == 0)
      icsId = this.parentICSID; 
    this.parentICSName = icsName;
    this.parentICSID = icsId;
    return icsId;
  }
}
