/**
 * @author tyl
 * @Action  ini
 * 2011-3-16
 */
package cn.com.origin.autocode.views.itemtypeview;

import java.awt.image.BufferedImage;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Set;

import javax.swing.ImageIcon;

import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.ui.part.ViewPart;

import cn.com.origin.autocode.common.operations.ClassificationOperation;
import cn.com.origin.autocodemanager.common.AutoCodeConstants;
import cn.com.origin.autocodemanager.common.AutoCodeManagerPropertyName;
import cn.com.origin.autocodemanager.common.CacheImage;
import cn.com.origin.autocodemanager.common.operations.PackCodeNodeInfo;
import cn.com.origin.autocodemanager.common.progress.ExpandProgressBar;
import cn.com.origin.autocodemanager.common.tree.AbstractCreateTreeView;
import cn.com.origin.autocodemanager.common.tree.AbstractTreeData;
import cn.com.origin.autocodemanager.common.tree.ORTreeContentProvider;
import cn.com.origin.autocodemanager.common.tree.ORTreeLabelProvider;

import com.teamcenter.rac.common.create.DisplayTypeInfo;
import com.teamcenter.rac.kernel.TCComponent;
import com.teamcenter.rac.kernel.TCComponentUser;
import com.teamcenter.rac.kernel.TCException;
import com.teamcenter.schemas.soa._2006_03.exceptions.ServiceException;

public class CreateClassificationView extends AbstractCreateTreeView {

	int selected = 0;

	public CreateClassificationView(Composite composite, String rootName,
			ViewPart viewPart) {
		super(composite, rootName, viewPart);
	}

	public CreateClassificationView(Composite composite, String rootName) {
		super(composite, rootName);
	}

	public void initTree(String rootName) {
		treeViewer.setLabelProvider(new ORTreeLabelProvider());
		treeViewer.setContentProvider(new ORTreeContentProvider());
		AbstractTreeData treeData = new AbstractTreeData(null, null, rootName,
				AutoCodeManagerPropertyName.ComponentName, false);
		treeViewer.setInput((new Object[] { treeData }));
	}

	public void addRightKeyAction() {
		final Tree tree = treeViewer.getTree();
		tree.addMouseListener(new MouseAdapter() {
			public void mouseDown(MouseEvent e) {
				if (e.button == 3) {
					ClassificationViewRightKeyAction ruleActionGroup = new ClassificationViewRightKeyAction(
							CreateClassificationView.this);
					ruleActionGroup.fillContextMenu();
				}
			}
		});
	}

	public void expandNode(AbstractTreeData codeData) {
		if (codeData == null) {
			return;
		}
		long time1 = System.nanoTime();
		AbstractTreeData[] codeDatas = new AbstractTreeData[] { codeData };
		if (codeData.isChildrenLoader()) {
			treeViewer.refresh(codeData);
			treeViewer.expandToLevel(codeData, 1);
			codeDatas = codeData.getChildrenDatas();
		}
		ExpandProgressBar expandProgressBar = new ExpandProgressBar(Display
				.getDefault().getActiveShell(), "正在打开", this, codeDatas);
		try {
			expandProgressBar.expand();
		} catch (Exception e) {
			e.printStackTrace();
		}
		long time2 = System.nanoTime();
		System.out.println("加载【"
				+ codeData.getPackCodeNodeInfo().getNode_name() + "】节点用时(ms)： "
				+ (time2 - time1) / 1000000);
	}

	/**
	 * @param codeDatas
	 * @throws Exception
	 */
	public void createOrgAutoCodeNodeChildren(AbstractTreeData[] codeDatas)
			throws Exception {
		if (codeDatas != null && codeDatas.length > 0) {
			for (int i = 0; i < codeDatas.length; i++) {
				AbstractTreeData autoCodeData = (AbstractTreeData) codeDatas[i];
				if (autoCodeData == null) {
					continue;
				}
				String nodeType = autoCodeData.getPackCodeNodeInfo()
						.getNode_type();

				if (nodeType.equals(AutoCodeManagerPropertyName.ComponentName)) {

					String[] strs = Util.getCustomType(this.session);
					if (strs == null) {
						strs = AutoCodeConstants.as1;
					}
					HashMap<?, ?> hashMap = (HashMap<?, ?>) businessType
							.loadTypes(strs, AutoCodeConstants.as2);
					getItemTypeNodes(autoCodeData, hashMap);
				} else {
					ClassificationOperation classificationOperation = new ClassificationOperation();
					TCComponent[] tccomponents = null;
					if (nodeType.equals(AutoCodeManagerPropertyName.ItemName)) {
						HashMap<Object, Object> hashMap = new HashMap<Object, Object>();
						hashMap.put(
								AutoCodeManagerPropertyName.ClassificationCodeNode_node_name,
								autoCodeData.getPackCodeNodeInfo()
										.getNode_name());
						tccomponents = classificationOperation
								.searchClassificationSegments(hashMap);
						int num = 0;
						int segmentNum = 0;
						if (tccomponents != null) {
							num = tccomponents.length;
							segmentNum = num;
						}
						hashMap.put(
								AutoCodeManagerPropertyName.ClassificationCodeNode_segment,
								"");
						TCComponent[] userComponents = classificationOperation
								.searchClassificationUsers(hashMap);
						if (userComponents != null) {
							num = num + userComponents.length;
						}
						AbstractTreeData autoCodeDataTCComs[] = new AbstractTreeData[num];
						if (tccomponents != null) {
							for (int k = 0; k < tccomponents.length; k++) {
								if (tccomponents[k] == null) {
									continue;
								}
								PackCodeNodeInfo packCodeNodeInfo = treeAndListCodeOperation
										.getTreeAndListStringValue(
												tccomponents[k],
												AutoCodeManagerPropertyName.searchTreeAndListCodeNodePropNames);
								autoCodeDataTCComs[k] = new AbstractTreeData(
										autoCodeData, packCodeNodeInfo);
								setSegmentUser(autoCodeDataTCComs[k],
										autoCodeData.getPackCodeNodeInfo()
												.getNode_name());
							}
						}
						if (userComponents != null) {
							for (int k = 0; k < userComponents.length; k++) {
								if (userComponents[k] == null) {
									continue;
								}
								userComponents[k].refresh();
								String object_name = userComponents[k].toDisplayString();
										//.getProperty("object_name");
								if (userComponents[k] instanceof TCComponentUser) {
									TCComponentUser user = (TCComponentUser) userComponents[k];
									object_name = user.toString();
								}
								autoCodeDataTCComs[k + segmentNum] = new AbstractTreeData(
										autoCodeData, userComponents[k],
										object_name,
										userComponents[k].getType(), false);
							}
						}
						autoCodeData.setChildrenLoader(true);
						autoCodeData
								.setTreeAutoCodeNodeChildren(autoCodeDataTCComs);
						treeViewer.refresh(autoCodeData);
					}
				}
			}
		}
	}

	private void setSegmentUser(AbstractTreeData parentData,
			String classificationName) {
		try {
			ClassificationOperation classificationOperation = new ClassificationOperation();
			TCComponent[] tccomponents = null;
			HashMap<Object, Object> hashMap = new HashMap<Object, Object>();
			hashMap.put(
					AutoCodeManagerPropertyName.ClassificationCodeNode_node_name,
					classificationName);
			hashMap.put(
					AutoCodeManagerPropertyName.ClassificationCodeNode_segment,
					parentData.getPackCodeNodeInfo().getNodeComponent()
							.getUid());
			tccomponents = classificationOperation
					.searchClassificationNodes(hashMap);
			if ((tccomponents != null) && (tccomponents.length > 0)) {
				for (int m = 0; m < tccomponents.length; m++) {
					tccomponents[m].refresh();
					TCComponent[] groups = (TCComponent[]) tccomponents[m]
							.getReferenceListProperty(AutoCodeManagerPropertyName.ClassificationCodeNode_grant_groups);
					if ((groups != null) && (groups.length > 0)) {
						for (int k = 0; k < groups.length; k++) {
							String object_name = groups[k].toDisplayString();
									//.getProperty("object_name");
							AbstractTreeData autoCodeDataTCCom = new AbstractTreeData(
									parentData, groups[k], object_name,
									groups[k].getType(), false);
							treeViewOperation.appendChildrenData(treeViewer,
									parentData, autoCodeDataTCCom);
						}
					}
					TCComponent[] roles = (TCComponent[]) tccomponents[m]
							.getReferenceListProperty(AutoCodeManagerPropertyName.ClassificationCodeNode_grant_roles);
					if ((roles != null) && (roles.length > 0)) {
						for (int k = 0; k < roles.length; k++) {
							String object_name = roles[k].toDisplayString();
									//.getProperty("object_name");
							AbstractTreeData autoCodeDataTCCom = new AbstractTreeData(
									parentData, roles[k], object_name,
									roles[k].getType(), false);
							treeViewOperation.appendChildrenData(treeViewer,
									parentData, autoCodeDataTCCom);
						}
					}
					TCComponent[] users = (TCComponent[]) tccomponents[m]
							.getReferenceListProperty(AutoCodeManagerPropertyName.ClassificationCodeNode_grant_users);
					if ((users != null) && (users.length > 0)) {
						for (int k = 0; k < users.length; k++) {
							String object_name = "";
							if (users[k] instanceof TCComponentUser) {
								TCComponentUser user = (TCComponentUser) users[k];
								object_name = user.toString();
							}
							AbstractTreeData autoCodeDataTCCom = new AbstractTreeData(
									parentData, users[k], object_name,
									users[k].getType(), false);
							treeViewOperation.appendChildrenData(treeViewer,
									parentData, autoCodeDataTCCom);
						}
					}
				}
			}
		} catch (ServiceException e) {
			e.printStackTrace();
		} catch (TCException e) {
			e.printStackTrace();
		}
	}

	public void getItemTypeNodes(AbstractTreeData autoCodeData,
			HashMap<?, ?> hashMap) {
		AbstractTreeData[] treeDatas = null;
		if (hashMap != null && hashMap.size() > 0) {
			treeDatas = new AbstractTreeData[hashMap.size()];
			Set<?> set = hashMap.keySet();
			Object obj[] = set.toArray();
			Arrays.sort(obj);
			for (int m = 0; m < obj.length; m++) {
				DisplayTypeInfo displaytypeinfo = (DisplayTypeInfo) obj[m];
				String childrenNodeName = displaytypeinfo.getDisplayTypeName();
				// ConvertibaleImageIcon s = (ConvertibaleImageIcon)
				// hashMap.get(displaytypeinfo);
				// ImageDescriptor imagedes = s.getImageDescriptor();
				// Image image = imagedes.createImage();
				System.out.println("getTypeName============="
						+ displaytypeinfo.getTypeName());
				System.out.println("getDisplayTypeName============="
						+ displaytypeinfo.getDisplayTypeName());
				System.out.println("displaytypeinfo============="
						+ displaytypeinfo);

				ImageIcon icon = (ImageIcon) hashMap.get(displaytypeinfo);
				BufferedImage bufferedImage = CacheImage.getBufferedImage(icon);
				ImageData imageData = CacheImage.getImageData(bufferedImage);
				Image image = new Image(null, imageData);
				treeDatas[m] = new AbstractTreeData(autoCodeData, null, image,
						childrenNodeName, displaytypeinfo.getTypeName(),
						AutoCodeManagerPropertyName.ItemName, false);
			}
			autoCodeData.setChildrenLoader(true);
			autoCodeData.setTreeAutoCodeNodeChildren(treeDatas);
			treeViewer.refresh(autoCodeData);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @seecn.com.origin.autocodemanager.common.tree.AbstractCreateTreeView#
	 * addMouseAction()
	 */
	@Override
	public void addMouseAction() {

	}
}
