/**
 * @author tyl
 * @Action  ini
 * 2011-3-23
 */
package cn.com.origin.autocodemanager.views.codeview;

import java.util.HashMap;
import java.util.List;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Tree;
import cn.com.origin.autocodemanager.common.AutoCodeManagerImageKey;
import cn.com.origin.autocodemanager.common.AutoCodeManagerPropertyName;
import cn.com.origin.autocodemanager.common.AutoCodeManangerConstants;
import cn.com.origin.autocodemanager.common.CacheImage;
import cn.com.origin.autocodemanager.common.CreateCompositeUtil;
import cn.com.origin.autocodemanager.common.dialog.AbstractCodeSegmentDialog;
import cn.com.origin.autocodemanager.common.operations.PackCodeNodeInfo;
import cn.com.origin.autocodemanager.common.operations.TreeAndListCodeOperation;
import cn.com.origin.autocodemanager.common.tree.AbstractCreateTreeView;
import cn.com.origin.autocodemanager.common.tree.AbstractTreeData;
import com.teamcenter.rac.kernel.TCComponent;
import com.teamcenter.rac.kernel.TCException;
import com.teamcenter.rac.util.MessageBox;
import com.teamcenter.schemas.soa._2006_03.exceptions.ServiceException;

public class CodeSegmentDialog extends AbstractCodeSegmentDialog {

	public CodeSegmentDialog(Shell parentShell, AbstractCreateTreeView createView, String title, String action) {
		super(parentShell, createView, title, action);
	}

	// 创建对话框的内容
	@Override
	protected Control createDialogArea(Composite parent) {
		Composite container = (Composite) super.createDialogArea(parent);
		Group nodeGroup = CreateCompositeUtil.createCompositeGroup(container, "编码规则");
		createTreeComposite(nodeGroup);
		return container;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.com.origin.autocodemanager.common.tree.AbstractCreateTreeView#addRightKeyAction()
	 */
	public void addRightKeyAction() {
		final Tree tree = treeViewer.getTree();
		tree.addMouseListener(new MouseAdapter() {
			public void mouseDoubleClick(MouseEvent mouseevent) {
				addChildrenDataToParent();
			}
		});
	}

	private void addChildrenDataToParent() {
		AbstractTreeData parentTreeData = null;
		AbstractTreeData childrenTreeData = treeViewOperation.getSelectedTreeNode(treeViewer);

		AbstractTreeData segmentTreeData = treeViewOperation.getSelectedTreeNode(codeSegmentView.createView.treeViewer);

		if ((childrenTreeData == null) || (childrenTreeData.getPackCodeNodeInfo().getNodeComponent() == null)) {
			MessageBox.post("请选择要要添加节点!", "INFO", MessageBox.WARNING);
			return;
		}
		if ((segmentTreeData == null) || (segmentTreeData.getPackCodeNodeInfo().getNodeComponent() == null)) {
			MessageBox.post("请选择修改的规则!", "INFO", MessageBox.WARNING);
			return;
		}

		parentTreeData = treeViewOperation.getSelectedTreeNode(createView.treeViewer);
		if (parentTreeData == null) {
			parentTreeData = treeViewOperation.getSelectedTreeNode(codeSegmentView.createView.treeViewer);
		}
		if (action.equals(AutoCodeManagerPropertyName.ListPropertyNodeName)) {
			if (!childrenTreeData.getPackCodeNodeInfo().getNode_type().equals(AutoCodeManagerPropertyName.ListPropertyNodeName)) {
				MessageBox.post("请选择要要添加的属性节点!", "INFO", MessageBox.WARNING);
				return;
			}
			parentTreeData = treeViewOperation.getSelectedTreeNode(codeSegmentView.createView.treeViewer);
			AbstractTreeData targetAddNode = addNewChildrenNode(codeSegmentView.createView.treeViewer, parentTreeData, childrenTreeData.getPackCodeNodeInfo().getNodeComponent(), segmentTreeData.getPackCodeNodeInfo().getNodeComponent());
			String parentStr = "";
			addNewChildrenNodes(createView.treeViewer, treeAndListCodeOperation.getParentStr(childrenTreeData, parentStr), childrenTreeData.getPackCodeNodeInfo().getNodeComponent().getUid(), targetAddNode, segmentTreeData.getPackCodeNodeInfo().getNodeComponent());
			treeViewOperation.addExistCodeNode(createView.treeViewer, parentTreeData, targetAddNode);
			createView.treeViewer.refresh();
		} else if (action.equals(AutoCodeManagerPropertyName.TreeNodeName)) {
			if ((!childrenTreeData.getPackCodeNodeInfo().getNode_type().equals(AutoCodeManagerPropertyName.TreeNodeName))) {
				MessageBox.post("请选择要要添加的树节点!", "INFO", MessageBox.WARNING);
				return;
			}
			AbstractTreeData targetAddNode = addNewChildrenNode(treeViewer, parentTreeData, childrenTreeData.getPackCodeNodeInfo().getNodeComponent(), segmentTreeData.getPackCodeNodeInfo().getNodeComponent());
			String parentStr = "";
			addNewChildrenNodes(createView.treeViewer, treeAndListCodeOperation.getParentStr(childrenTreeData, parentStr), childrenTreeData.getPackCodeNodeInfo().getNodeComponent().getUid(), targetAddNode, segmentTreeData.getPackCodeNodeInfo().getNodeComponent());
			treeViewOperation.appendChildrenNode(createView.treeViewer, parentTreeData, targetAddNode);
			createView.treeViewer.refresh();
		}
	}

	private AbstractTreeData addNewChildrenNode(TreeViewer treeView, AbstractTreeData parentNode, TCComponent component, TCComponent segmentComponent) {
		AbstractTreeData treeNode = null;
		try {
			if (component == null) {
				return null;
			}
			TreeAndListCodeOperation treeAndListCodeOperation = new TreeAndListCodeOperation();
			PackCodeNodeInfo packCodeNodeInfo = treeAndListCodeOperation.getTreeAndListStringValue(component, AutoCodeManagerPropertyName.searchTreeAndListCodeNodePropNames);
			treeNode = new AbstractTreeData(parentNode, packCodeNodeInfo);
			
			treeViewOperation.addChildrenRelations(parentNode, treeNode);
			treeAndListCodeOperation.addNodeReferenceComponents(component, segmentComponent);
			treeView.refresh(parentNode);
		} catch (TCException e) {
			e.printStackTrace();
		} catch (ServiceException e) {
			e.printStackTrace();
		}
		return treeNode;
	}

	private void addNewChildrenNodes(TreeViewer treeView, String parentStr, String currentStr, AbstractTreeData parentNode, TCComponent segmentComponent) {
		try {
			TCComponent[] tccomponents = null;
			HashMap<Object, Object> hashMap = new HashMap<Object, Object>();
			hashMap.put(AutoCodeManagerPropertyName.CodeRelation_parent_node, parentStr);
			hashMap.put(AutoCodeManagerPropertyName.CodeRelation_current_node, currentStr);
			tccomponents = treeAndListCodeOperation.searchTreeORListNodes(hashMap);
			for (int i = 0; i < tccomponents.length; i++) {
				AbstractTreeData copyNode = addNewChildrenNode(treeView, parentNode, tccomponents[i], segmentComponent);
				String parentStr2 = parentStr + "," + currentStr;
				if (parentStr2.trim().startsWith(",")) {
					parentStr2 = parentStr2.substring(1);
				}
				addNewChildrenNodes(treeView, parentStr2, tccomponents[i].getUid().trim(), copyNode, segmentComponent);
			}
		} catch (ServiceException e) {
			e.printStackTrace();
		} catch (TCException e) {
			e.printStackTrace();
		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.com.origin.autocodemanager.common.tree.AbstractCreateTreeView#createOrgAutoCodeNodeChildren(cn.com.origin.autocodemanager.common.tree.AbstractTreeData[])
	 */

	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;
				}
				TCComponent autoCodeDataComponent = autoCodeData.getPackCodeNodeInfo().getNodeComponent();
				TreeAndListCodeOperation treeAndListCodeOperation = new TreeAndListCodeOperation();
				HashMap<Object, Object> hashMap = new HashMap<Object, Object>();
				String parentStr = "";
				parentStr = treeAndListCodeOperation.getParentStr(autoCodeData, parentStr);

				if (autoCodeDataComponent != null) {
					hashMap.put(AutoCodeManagerPropertyName.CodeRelation_current_node, autoCodeDataComponent.getUid());
					hashMap.put(AutoCodeManagerPropertyName.CodeRelation_parent_node, parentStr);
				} else {
					hashMap.put(AutoCodeManagerPropertyName.CodeRelation_parent_node, "");
					hashMap.put(AutoCodeManagerPropertyName.CodeRelation_current_node, "");
				}
				String parentStr2 = "";
				if (autoCodeDataComponent != null) {
					if (parentStr.trim().length() == 0) {
						parentStr2 = autoCodeDataComponent.getUid();
					} else {
						parentStr2 = parentStr + "," + autoCodeDataComponent.getUid();
					}
				} else {
					parentStr2 = parentStr;
				}

				List<PackCodeNodeInfo> packCodeNodeInfos = treeAndListCodeOperation.searchTreeORListNodeInfos(hashMap, parentStr2, AutoCodeManagerPropertyName.searchTreeAndListCodeNodePropNames);
				if (packCodeNodeInfos != null && packCodeNodeInfos.size() > 0) {
					AbstractTreeData autoCodeDataTCComs[] = new AbstractTreeData[packCodeNodeInfos.size()];
					for (int k = 0; k < packCodeNodeInfos.size(); k++) {
						if (packCodeNodeInfos.get(k).getNodeComponent() == null) {
							continue;
						}
						PackCodeNodeInfo packCodeNodeInfo = packCodeNodeInfos.get(k);
						autoCodeDataTCComs[k] = new AbstractTreeData(autoCodeData, packCodeNodeInfo);
						if (packCodeNodeInfo.isContainsChildren()) {
							autoCodeDataTCComs[k].setTreeAutoCodeNodeChildren(new AbstractTreeData[] { new AbstractTreeData() });
						}
					}
					autoCodeData.setTreeAutoCodeNodeChildren(autoCodeDataTCComs);
					autoCodeData.setChildrenLoader(true);
					treeViewer.refresh(autoCodeData);
				}
			}
		}
	}

	// 设置对话框初始大小
	@Override
	protected Point getInitialSize() {
		return new Point(370, 500);
	}

	// 创建对话框按钮
	@Override
	protected void createButtonsForButtonBar(final Composite parent) {
		createButton(parent, IDialogConstants.OK_ID, "添加", true);
		createButton(parent, IDialogConstants.CANCEL_ID, "关闭", false);
		getButton(IDialogConstants.OK_ID).setImage(CacheImage.getINSTANCE().getImage(AutoCodeManangerConstants.AutoCodeMananger_APPLICATION_ID, AutoCodeManagerImageKey.OK_IMAGE));
		getButton(IDialogConstants.CANCEL_ID).setImage(CacheImage.getINSTANCE().getImage(AutoCodeManangerConstants.AutoCodeMananger_APPLICATION_ID, AutoCodeManagerImageKey.CLOSE_IMAGE));
	}

	// 检查节点和组是否为空
	@Override
	protected void okPressed() {
		addChildrenDataToParent();
		this.close();
	}
}
