package com.connor.erp.processresource.EquipmentAcquiredFromERP;

import cn.com.origin.autocode.classification.ClassificationPropertyNames;
import cn.com.origin.autocode.classification.panels.ClassificationTreeData;
import com.teamcenter.rac.aifrcp.AifrcpPlugin;
import com.teamcenter.rac.kernel.TCClassificationService;
import com.teamcenter.rac.kernel.TCException;
import com.teamcenter.rac.kernel.TCSession;
import com.teamcenter.rac.kernel.ics.ICSApplicationObject;
import com.teamcenter.rac.kernel.ics.ICSHierarchyNodeDescriptor;
import java.awt.Component;
import java.net.URL;
import javax.swing.ImageIcon;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.event.TreeExpansionEvent;
import javax.swing.event.TreeExpansionListener;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;

public class ORCreateClassificationPanel {
  JScrollPane classificationPropertyPanel;
  
  CreateClassificationPropertyPanel createClassificationPropertyPanel;
  
  TCClassificationService classificationService;
  
  ICSApplicationObject icsapplicationobject;
  
  ORNewEquipmentDialog dialog;
  
  ClassificationTreeData treeData;
  
  public TCSession getSession() {
    TCSession session = (TCSession)AifrcpPlugin.getSessionService().getDefaultSession();
    return session;
  }
  
  public ORCreateClassificationPanel(ORNewEquipmentDialog dialog) {
    this.classificationService = getSession().getClassificationService();
    this.dialog = dialog;
    try {
      this.icsapplicationobject = this.classificationService.newICSApplicationObject("ICM");
    } catch (TCException e) {
      e.printStackTrace();
    } 
  }
  
  public JScrollPane createPropertyInfoPanel() {
    this.createClassificationPropertyPanel = new CreateClassificationPropertyPanel();
    this.classificationPropertyPanel = this.createClassificationPropertyPanel.getPropertyPanel();
    return this.classificationPropertyPanel;
  }
  
  public JScrollPane CreateClassificationInfoPanel(String rootID, String rootName, String type) {
    JScrollPane scrollPane = new JScrollPane();
    try {
      ClassificationTreeData rootTreeData = new ClassificationTreeData(rootID, rootName, type, null, null);
      DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode(rootTreeData);
      setChildrenNode(1, rootID, rootNode, rootTreeData);
      DefaultTreeModel treeModel = new DefaultTreeModel(rootNode);
      JTree tree = new JTree(treeModel);
      tree.addTreeSelectionListener(new treeSelectedListener());
      tree.setCellRenderer(new MyRenderer());
      tree.addTreeExpansionListener(new TreeExpandedListener());
      scrollPane.setVerticalScrollBarPolicy(22);
      scrollPane.setHorizontalScrollBarPolicy(32);
      scrollPane.getViewport().setView(tree);
    } catch (Exception e) {
      e.printStackTrace();
    } 
    return scrollPane;
  }
  
  private class MyRenderer extends DefaultTreeCellRenderer {
    private static final long serialVersionUID = 1L;
    
    ImageIcon imageIcon;
    
    public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus) {
      super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);
      DefaultMutableTreeNode treeNode = (DefaultMutableTreeNode)value;
      ClassificationTreeData treeData = (ClassificationTreeData)treeNode.getUserObject();
      setText(treeData.getName());
      String type = treeData.getType();
      URL url = null;
      if (type.equals(ClassificationPropertyNames.icsGroupType)) {
        url = getClass().getResource("images/inclassgroup_16.png");
      } else if (type.equals(ClassificationPropertyNames.icsAbstractType)) {
        url = getClass().getResource("images/inclassabstractclass_16.png");
      } else if (type.equals(ClassificationPropertyNames.icsInstanceType)) {
        url = getClass().getResource("images/inclassstorageclass_16.png");
      } 
      if (url != null) {
        this.imageIcon = new ImageIcon(url);
        setIcon(this.imageIcon);
      } 
      return this;
    }
  }
  
  private void setChildrenNode(int expandNum, String parentIcsID, DefaultMutableTreeNode parentNode, ClassificationTreeData parentClassificationTreeData) {
    ICSHierarchyNodeDescriptor[] icsnode = this.classificationService.getChildren(parentIcsID, false, true, 0, 0);
    if (icsnode != null) {
      expandNum++;
      for (int i = 0; i < icsnode.length; i++) {
        String node_id = icsnode[i].getId();
        String node_name = icsnode[i].getName();
        String type = icsnode[i].getType();
        if (icsnode[i].isStorageClass())
          type = ClassificationPropertyNames.icsInstanceType; 
        ClassificationTreeData classificationTreeData = new ClassificationTreeData(node_id, node_name, type, parentClassificationTreeData, null);
        parentClassificationTreeData.addChildrenTreeNodes(classificationTreeData);
        DefaultMutableTreeNode node = new DefaultMutableTreeNode(classificationTreeData);
        parentNode.add(node);
        setChildrenNode(expandNum, node_id, node, classificationTreeData);
      } 
    } 
  }
  
  private class TreeExpandedListener implements TreeExpansionListener {
    private TreeExpandedListener() {}
    
    public void treeCollapsed(TreeExpansionEvent treeexpansionevent) {}
    
    public void treeExpanded(TreeExpansionEvent treeexpansionevent) {}
  }
  
  private class treeSelectedListener implements TreeSelectionListener {
    private treeSelectedListener() {}
    
    public void valueChanged(TreeSelectionEvent tse) {
      TreePath selectedPath = tse.getNewLeadSelectionPath();
      if (selectedPath == null)
        return; 
      DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode)selectedPath.getLastPathComponent();
      if (selectedNode == null)
        return; 
      ORCreateClassificationPanel.this.treeData = (ClassificationTreeData)selectedNode.getUserObject();
      ORCreateClassificationPanel.this.createClassificationPropertyPanel.createPropertyPanel(ORCreateClassificationPanel.this.treeData.getName(), ORCreateClassificationPanel.this.treeData.getId());
      PackClassificationInfos classificationInfos = new PackClassificationInfos();
      classificationInfos.setIcsID(ORCreateClassificationPanel.this.treeData.getId());
      classificationInfos.setPropertyMaps(ORCreateClassificationPanel.this.createClassificationPropertyPanel.getPropertyInfos());
      classificationInfos.setNameToIdHashMap(ORCreateClassificationPanel.this.createClassificationPropertyPanel.getNameToIdPropertyInfos());
      ORCreateClassificationPanel.this.dialog.setFolderName(ORCreateClassificationPanel.this.treeData.getName());
      ORCreateClassificationPanel.this.dialog.setClassificationInfos(classificationInfos);
      ORCreateClassificationPanel.this.dialog.setClassificationService(ORCreateClassificationPanel.this.createClassificationPropertyPanel.getClassificationService());
      ORCreateClassificationPanel.this.dialog.setIcsView(ORCreateClassificationPanel.this.createClassificationPropertyPanel.getICSView());
      ORCreateClassificationPanel.this.dialog.setIcsapplicationobject(ORCreateClassificationPanel.this.createClassificationPropertyPanel.getICSApplicationObject());
    }
  }
}
