/**
 * @author tyl
 * @Action  ini
 * 2011-5-6
 */
package cn.com.origin.autocodemanager.handlers;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.expressions.IEvaluationContext;
import org.eclipse.ui.ISources;
import cn.com.origin.autocodemanager.common.AbstractTreeViewPart;
import cn.com.origin.autocodemanager.common.AutoCodeManagerPropertyName;
import cn.com.origin.autocodemanager.common.AutoCodeManangerConstants;
import cn.com.origin.autocodemanager.common.operations.TreeAndListCodeOperation;
import cn.com.origin.autocodemanager.common.tree.AbstractTreeData;
import cn.com.origin.autocodemanager.common.tree.TreeViewOperation;
import cn.com.origin.autocodemanager.views.AutoCodeListCodeView;
import com.teamcenter.rac.kernel.TCComponent;

public class UpListNodeHandler extends AbstractHandler {
	public UpListNodeHandler() {
	}

	public Object execute(ExecutionEvent e) throws ExecutionException {
		Object obj = e.getApplicationContext();
		if (obj instanceof IEvaluationContext) {
//			AbstractAIFUIApplication abstractAIFUIApplication = AIFUtility.getCurrentApplication();
//			TCSession session = (TCSession) abstractAIFUIApplication.getSession();
			IEvaluationContext appContext = (IEvaluationContext) obj;
			Object view = appContext.getVariable(ISources.ACTIVE_PART_NAME);
			if (view instanceof AbstractTreeViewPart) {
				TreeViewOperation treeViewOperation = new TreeViewOperation();
				AbstractTreeViewPart viewPart = (AbstractTreeViewPart) view;
				TreeAndListCodeOperation treeAndListCodeOperation = new TreeAndListCodeOperation();

				AutoCodeListCodeView codeListView = (AutoCodeListCodeView) viewPart.getSite().getPage().findView(AutoCodeManangerConstants.AutoCodeManagerListCodeView_ID);
//				AutoCodeSegmentView codeSegmentView = (AutoCodeSegmentView) viewPart.getSite().getPage().findView(AutoCodeConstants.AutoCodeSegmentView_ID);
				AbstractTreeData selectedTreeData = treeViewOperation.getSelectedTreeNode(codeListView.createView.treeViewer);
//				AbstractTreeData segmentTreeData = treeViewOperation.getSelectedTreeNode(codeSegmentView.createView.treeViewer);
				if (selectedTreeData == null) {
					return null;
				}
				if (selectedTreeData.getPackCodeNodeInfo().getNode_type().equals(AutoCodeManagerPropertyName.ListPropertyNodeName)) {
					AbstractTreeData[] listPropertyTreeDatas = (AbstractTreeData[]) codeListView.createView.treeViewer.getInput();
					for (int i = 0; i < listPropertyTreeDatas.length; i++) {
						if (selectedTreeData.equals(listPropertyTreeDatas[i])) {
							AbstractTreeData tempTreeData = listPropertyTreeDatas[i];
							if (i > 0) {
								listPropertyTreeDatas[i] = listPropertyTreeDatas[i - 1];
								listPropertyTreeDatas[i -1] = tempTreeData;
								codeListView.createView.treeViewer.refresh();
								break;
							}
						}
					}
					String parentStr = "";
					parentStr = treeAndListCodeOperation.getParentStr(selectedTreeData.getParentTreeData(), parentStr);
					String childrenStr = "";
					for (int i = 0; i < listPropertyTreeDatas.length; i++) {
						TCComponent component = listPropertyTreeDatas[i].getPackCodeNodeInfo().getNodeComponent();
						if (component != null) {
							childrenStr = childrenStr + component.getUid() + ",";

						}
					}
					if (childrenStr.toString().endsWith(",")) {
						childrenStr = childrenStr.substring(0, childrenStr.length() - 1).toString();
					}
					treeAndListCodeOperation.updateChildrenCodeRelation(parentStr, selectedTreeData.getParentTreeData().getPackCodeNodeInfo().getNodeComponent().getUid(), childrenStr);
				} else {
					AbstractTreeData listPropertyTreeNode = selectedTreeData.getParentTreeData();
					AbstractTreeData[] listPropertyValueTreeDatas = listPropertyTreeNode.getChildrenTreeData();
					for (int i = 0; i < listPropertyValueTreeDatas.length; i++) {
						if (selectedTreeData.equals(listPropertyValueTreeDatas[i])) {
							AbstractTreeData tempTreeData = listPropertyValueTreeDatas[i];
							if (i > 0) {
								listPropertyValueTreeDatas[i] = listPropertyValueTreeDatas[i - 1];
								listPropertyValueTreeDatas[i - 1] = tempTreeData;
								listPropertyTreeNode.setTreeAutoCodeNodeChildren(listPropertyValueTreeDatas);
								codeListView.createView.treeViewer.refresh(listPropertyTreeNode);
								break;
							}
						}
					}
					String parentStr = "";
					parentStr = treeAndListCodeOperation.getParentStr(selectedTreeData.getParentTreeData(), parentStr);
					String childrenStr = "";
					for (int i = 0; i < listPropertyValueTreeDatas.length; i++) {
						TCComponent component = listPropertyValueTreeDatas[i].getPackCodeNodeInfo().getNodeComponent();
						if (component != null) {
							childrenStr = childrenStr + component.getUid() + ",";

						}
					}
					if (childrenStr.toString().endsWith(",")) {
						childrenStr = childrenStr.substring(0, childrenStr.length() - 1).toString();
					}
					treeAndListCodeOperation.updateChildrenCodeRelation(parentStr, selectedTreeData.getParentTreeData().getPackCodeNodeInfo().getNodeComponent().getUid(), childrenStr);
				}
			}
		}
		return null;
	}

}
