/**
 * @author tyl
 * @Action  ini
 * 2011-3-29
 */
package cn.com.origin.autocodemanager.common.tree;

import java.util.ArrayList;
import java.util.List;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.swt.widgets.TreeItem;
import cn.com.origin.autocodemanager.common.operations.TreeAndListCodeOperation;
import cn.com.origin.autocodemanager.views.AutoCodeListCodeView;
import cn.com.origin.autocodemanager.views.AutoCodeSegmentView;
import cn.com.origin.autocodemanager.views.AutoCodeTreeCodeView;
import com.teamcenter.rac.util.MessageBox;

public class TreeViewOperation {
	public TreeAndListCodeOperation treeAndListCodeOperation;
	public AutoCodeListCodeView codeListView;
	public AutoCodeSegmentView codeSegmentView;
	public AutoCodeTreeCodeView codeTreeView;

	public TreeViewOperation() {
		this.treeAndListCodeOperation = new TreeAndListCodeOperation();
	}

	public void addChildrenTreeNodeRelations(TreeViewer treeViewer, AbstractTreeData parentNode, AbstractTreeData[] childrenNodes) {
		for (int i = 0; i < childrenNodes.length; i++) {
			AbstractTreeData childrenNode = childrenNodes[i];
			if (childrenNode.getPackCodeNodeInfo().getNodeComponent() != null) {
				treeAndListCodeOperation.createNewCodeRelation(parentNode, childrenNode.getPackCodeNodeInfo().getNodeComponent());
				addChildrenTreeNodeRelations(treeViewer, childrenNode, childrenNode.getChildrenDatas());
			}
		}
	}

	public boolean isRuleRootNode(AbstractTreeData treeData) {
		if (treeData == null) {
			return false;
		}
		if(treeData.getPackCodeNodeInfo() ==null){
			return false;
		}
		if (treeData.getPackCodeNodeInfo().getNodeComponent() == null) {
			return false;
		}
		if (treeData.getPackCodeNodeInfo().is_root) {
			return true;
		}
		return false;
	}

	public void addExistCodeNode(TreeViewer treeViewer, AbstractTreeData parentData, AbstractTreeData childrenData) {
		try {
			if (isRuleRootNode(parentData)) {
				addNewTreeOrListInput(treeViewer, parentData, childrenData);
			} else {
				addChildrenData(treeViewer, parentData, childrenData);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void appendChildrenNode(TreeViewer treeViewer, AbstractTreeData parentData, AbstractTreeData childrenData) {
		AbstractTreeData[] childrenDatas = parentData.getChildrenDatas();
		int childrenLength = 1;

		if (childrenDatas != null) {
			childrenLength = childrenLength + childrenDatas.length;
		}
		AbstractTreeData[] treeDatas = new AbstractTreeData[childrenLength];
		boolean isContain = true;
		for (int i = 0; i < childrenLength - 1; i++) {
			if ((childrenData.getPackCodeNodeInfo().getNodeComponent()).equals(childrenDatas[i].getPackCodeNodeInfo().getNodeComponent())) {
				isContain = false;
				break;
			}
			treeDatas[i] = childrenDatas[i];
		}
		if (!isContain) {
			return;
		} else {
			treeDatas[childrenLength - 1] = childrenData;
			parentData.setTreeAutoCodeNodeChildren(treeDatas);
			childrenData.setParentTreeData(parentData);
			parentData.setChildrenLoader(true);
			treeViewer.refresh(parentData);
		}
	}

	public void addChildrenRelations(AbstractTreeData parentData, AbstractTreeData childrenData) {
		treeAndListCodeOperation.createNewCodeRelation(parentData, childrenData.getPackCodeNodeInfo().getNodeComponent());
		AbstractTreeData[] childrenDatas = parentData.getChildrenDatas();
		int childrenLength = 1;

		if (childrenDatas != null) {
			childrenLength = childrenLength + childrenDatas.length;
		}
		AbstractTreeData[] treeDatas = new AbstractTreeData[childrenLength];
		for (int i = 0; i < childrenLength - 1; i++) {
			treeDatas[i] = childrenDatas[i];
		}
		treeDatas[childrenLength - 1] = childrenData;

		if (isRuleRootNode(parentData)) {
			parentData.setChildrenDatas(treeDatas);
		} else {
			parentData.setTreeAutoCodeNodeChildren(treeDatas);
		}
		childrenData.setParentTreeData(parentData);
		parentData.setChildrenLoader(true);
	}

	public void addTreeNodes(TreeViewer treeViewer, AbstractTreeData parentData, AbstractTreeData childrenData) {
		treeAndListCodeOperation.createNewCodeRelation(parentData, childrenData.getPackCodeNodeInfo().getNodeComponent());
		AbstractTreeData[] childrenDatas = parentData.getChildrenDatas();
		int childrenLength = 1;

		if (childrenDatas != null) {
			childrenLength = childrenLength + childrenDatas.length;
		}
		AbstractTreeData[] treeDatas = new AbstractTreeData[childrenLength];
		for (int i = 0; i < childrenLength - 1; i++) {
			treeDatas[i] = childrenDatas[i];
		}
		treeDatas[childrenLength - 1] = childrenData;

		parentData.setTreeAutoCodeNodeChildren(treeDatas);
		childrenData.setParentTreeData(parentData);
		parentData.setChildrenLoader(true);
		treeViewer.refresh(parentData);
	}

	public void addChildrenData(TreeViewer treeViewer, AbstractTreeData parentData, AbstractTreeData childrenData) {
		if (parentData == null) {
			return;
		}
		if (childrenData == null) {
			return;
		}
		treeAndListCodeOperation.createNewCodeRelation(parentData, childrenData.getPackCodeNodeInfo().getNodeComponent());
		AbstractTreeData[] childrenDatas = parentData.getChildrenDatas();
		int childrenLength = 1;

		if (childrenDatas != null) {
			childrenLength = childrenLength + childrenDatas.length;
		}
		AbstractTreeData[] treeDatas = new AbstractTreeData[childrenLength];
		for (int i = 0; i < childrenLength - 1; i++) {
			treeDatas[i] = childrenDatas[i];
		}
		treeDatas[childrenLength - 1] = childrenData;

		if (isRuleRootNode(parentData)) {
			parentData.setChildrenDatas(treeDatas);
		} else {
			parentData.setTreeAutoCodeNodeChildren(treeDatas);
		}
		childrenData.setParentTreeData(parentData);
		parentData.setChildrenLoader(true);
		treeViewer.refresh(parentData);
	}

	public boolean appendChildrenData(TreeViewer treeViewer, AbstractTreeData parentData, AbstractTreeData childrenData) {
		AbstractTreeData[] childrenDatas = parentData.getChildrenDatas();
		int childrenLength = 1;

		if (childrenDatas != null) {
			childrenLength = childrenLength + childrenDatas.length;
		}
		AbstractTreeData[] treeDatas = new AbstractTreeData[childrenLength];
		boolean isContain = false;
		for (int i = 0; i < childrenLength - 1; i++) {
			if ((childrenData.getPackCodeNodeInfo().getNodeComponent()).equals(childrenDatas[i].getPackCodeNodeInfo().getNodeComponent())) {
				isContain = true;
				break;
			}
			treeDatas[i] = childrenDatas[i];
		}
		if (isContain) {
			MessageBox.post("زرجي¼س،¾" + childrenData.getPackCodeNodeInfo().getNode_name() + "،؟!", "INFO", MessageBox.WARNING);
		} else {
			treeDatas[childrenLength - 1] = childrenData;
			parentData.setTreeAutoCodeNodeChildren(treeDatas);
			childrenData.setParentTreeData(parentData);
			if(childrenData.packCodeNodeInfo.getNodeComponent()!=null){
				String cNodeNameString = childrenData.packCodeNodeInfo.getNodeComponent().toDisplayString();
				if(cNodeNameString !=null && cNodeNameString.replace(" ", "").length() >0){
					childrenData.packCodeNodeInfo.setNode_name(cNodeNameString);
				}
			}
			parentData.setChildrenLoader(true);
			treeViewer.refresh(parentData);
		}
		return isContain;
	}

	public void addNewTreeOrListInput(TreeViewer treeViewer, AbstractTreeData parentData, AbstractTreeData childrenData) {
		// treeAndListCodeOperation.createNewCodeRelation(parentData, childrenData.getPackCodeNodeInfo().getNodeComponent());
		int length = 1;
		if (treeViewer.getInput() != null) {
			Object[] existDatas = (Object[]) treeViewer.getInput();
			if (existDatas != null) {
				length = length + existDatas.length;
			}
			AbstractTreeData[] childrenDatas = new AbstractTreeData[length];
			if (existDatas != null) {
				for (int i = 0; i < existDatas.length; i++) {
					childrenDatas[i] = (AbstractTreeData) existDatas[i];
				}
			}
			childrenData.setParentTreeData(parentData);
			childrenDatas[length - 1] = childrenData;
			treeViewer.setInput(childrenDatas);
			parentData.setChildrenDatas(childrenDatas);
		} else {
			treeViewer.setInput(new AbstractTreeData[] { childrenData });
			parentData.setChildrenDatas(new AbstractTreeData[] { childrenData });
			childrenData.setParentTreeData(parentData);
		}
		parentData.setChildrenLoader(true);
		treeViewer.refresh(parentData);
		treeAndListCodeOperation.createNewCodeRelation(parentData, childrenData.getPackCodeNodeInfo().getNodeComponent());
	}

	public AbstractTreeData getSelectedTreeNode(TreeViewer treeViewer) {
		TreeItem[] treeItems = treeViewer.getTree().getSelection();
		if ((treeItems != null) && (treeItems.length > 0)) {
			return (AbstractTreeData) treeItems[0].getData();
		} else {
			return null;
		}
	}

	public void removeTreeNode(TreeViewer treeViewer, AbstractTreeData treeData) {
		try {
			AbstractTreeData parentTreeData = treeData.getParentTreeData();
			if (parentTreeData != null) {
				if (isRuleRootNode(parentTreeData)) {
					updateTreeOrListInput(treeViewer, parentTreeData, treeData);
				} else {
					List<AbstractTreeData> treeDatas = new ArrayList<AbstractTreeData>();
					AbstractTreeData[] parentChildrenData = parentTreeData.getChildrenDatas();
					if (parentChildrenData != null) {
						for (int i = 0; i < parentChildrenData.length; i++) {
							if (!treeData.equals(parentChildrenData[i])) {
								treeDatas.add(parentChildrenData[i]);
							}
						}
						if (treeDatas.size() > 0) {
							AbstractTreeData[] treeDatas2 = new AbstractTreeData[treeDatas.size()];
							for (int i = 0; i < treeDatas.size(); i++) {
								treeDatas2[i] = treeDatas.get(i);
							}
							parentTreeData.setTreeAutoCodeNodeChildren(treeDatas2);
						} else {
							parentTreeData.setTreeAutoCodeNodeChildren(null);
						}
					}
				}
			} else {
				treeViewer.setInput(null);
			}
			treeViewer.remove(treeData);
		} catch (Exception e) {
			e.printStackTrace();
		}
		// treeViewer.refresh(parentTreeData);
	}

	public void removeNodeFromParent(AbstractTreeData parentTreeData, AbstractTreeData childrenData) {
		if ((parentTreeData == null) || (childrenData == null)) {
			return;
		}
		List<AbstractTreeData> treeDatas = new ArrayList<AbstractTreeData>();
		AbstractTreeData[] parentChildrenData = parentTreeData.getChildrenDatas();
		if (parentChildrenData != null) {
			for (int i = 0; i < parentChildrenData.length; i++) {
				if (!childrenData.equals(parentChildrenData[i])) {
					treeDatas.add(parentChildrenData[i]);
				}
			}
			if (treeDatas.size() > 0) {
				AbstractTreeData[] treeDatas2 = new AbstractTreeData[treeDatas.size()];
				for (int i = 0; i < treeDatas.size(); i++) {
					treeDatas2[i] = treeDatas.get(i);
				}
				parentTreeData.setTreeAutoCodeNodeChildren(treeDatas2);
			} else {
				parentTreeData.setTreeAutoCodeNodeChildren(null);
			}
		}

	}

	public void updateTreeOrListInput(TreeViewer treeViewer, AbstractTreeData parentData, AbstractTreeData childrenData) {
		int length = -1;
		if (treeViewer.getInput() != null) {
			AbstractTreeData[] existDatas = (AbstractTreeData[]) treeViewer.getInput();
			if (existDatas != null) {
				length = length + existDatas.length;
			}
			if (length >= 1) {
				List<AbstractTreeData> treeDatas = new ArrayList<AbstractTreeData>();
				for (int i = 0; i < existDatas.length; i++) {
					if (!childrenData.equals(existDatas[i])) {
						treeDatas.add(existDatas[i]);
					}
				}
				AbstractTreeData[] childrenDatas = new AbstractTreeData[treeDatas.size()];
				for (int i = 0; i < treeDatas.size(); i++) {
					childrenDatas[i] = treeDatas.get(i);
				}
				treeViewer.setInput(childrenDatas);
				parentData.setTreeAutoCodeNodeChildren(childrenDatas);
			} else {
				treeViewer.setInput(null);
				parentData.setTreeAutoCodeNodeChildren(null);
			}
		} else {
			treeViewer.setInput(null);
			parentData.setTreeAutoCodeNodeChildren(null);
		}
		treeViewer.refresh(parentData);
	}

	public void addExistSegmentRelation(TreeViewer treeViewer, AbstractTreeData parentTreeData, AbstractTreeData[] childrenTreeDatas) {
		for (int i = 0; i < childrenTreeDatas.length; i++) {
			AbstractTreeData childrenData = childrenTreeDatas[i];
			if (childrenData.getPackCodeNodeInfo().getNodeComponent() != null) {
				treeAndListCodeOperation.createNewCodeRelation(parentTreeData, childrenData.getPackCodeNodeInfo().getNodeComponent());
				addExistSegmentRelation(treeViewer, childrenData, childrenData.getChildrenDatas());
			}
		}
	}

}
