package com.connor.processAttributeModification;

import java.util.LinkedHashMap;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.commands.IHandler;

import com.connor.jingdiao.handlers.StandardDialog;
import com.teamcenter.rac.aif.AIFDesktop;
import com.teamcenter.rac.aif.kernel.InterfaceAIFComponent;
import com.teamcenter.rac.aifrcp.AIFUtility;
import com.teamcenter.rac.kernel.TCComponent;
import com.teamcenter.rac.kernel.TCComponentTask;
import com.teamcenter.rac.kernel.TCComponentTaskTemplate;
import com.teamcenter.rac.kernel.TCHandlerType;
import com.teamcenter.rac.kernel.TCSession;
import com.teamcenter.rac.util.MessageBox;

public class ProcessAttriChangeHandler extends AbstractHandler implements IHandler {

	public Object execute(ExecutionEvent arg0) throws ExecutionException {
		System.out.println("StandardHandler");
		TCSession session = (TCSession) AIFUtility.getCurrentApplication().getSession();
		AIFDesktop desktop = AIFUtility.getActiveDesktop();
		try {
			InterfaceAIFComponent target = AIFUtility.getCurrentApplication().getTargetComponent();
			if(!(target instanceof TCComponentTask)) {
				MessageBox.post(desktop, "请选择流程任务进行节点属性编辑！", "ERROR", MessageBox.ERROR);
				return null;
			}
			TCComponentTask task = (TCComponentTask) target;//选中的流程任务节点
			TCComponent handler = task.getHandler(TCHandlerType.ACTION, TCComponentTaskTemplate.COMPLETE_ACTION, "j6_Standard");//Connor_check_process_member
			
			if(handler == null) {
				MessageBox.post(desktop, "节点handler配置不满足要求，无法执行！", "ERROR", MessageBox.ERROR);
				return null;
			}
			
			LinkedHashMap<String, String[]> ruleOpsMap = new LinkedHashMap<>();
			String[] options = session.getPreferenceService().getStringValues("Jd_Standard_UI_Process_Attribute");
			if(options == null || options.length == 0) {
				MessageBox.post(desktop, "首选项Jd_Standard_UI配置不正确", "ERROR", MessageBox.ERROR);
				return null;
			}
			int[] cols = new int[2];
			for(String s : options) {
				if(!s.contains("=")) {
					String[] sp = s.split(";");
					cols[0] = Integer.parseInt(sp[0]) - 1;
					cols[1] = Integer.parseInt(sp[1]) - 1;
					continue;
				}
				String[] sp = s.split("=");
				String[] pp = sp[0].split("\\.");
				ruleOpsMap.put(sp[1], new String[] { pp[0].toLowerCase(), pp[1] });
			}
			options = session.getPreferenceService().getStringValues("j6_Standard_puid_Process_Attribute");
			if(options == null || options.length < 2) {
				MessageBox.post(desktop, "首选项j6_Standard_puid配置不正确", "ERROR", MessageBox.ERROR);
				return null;
			}
			TCComponent[] datasets = session.stringToComponent(options);
			if(datasets.length<2 || datasets[0]==null || !datasets[0].getType().equals("XMLRenderingStylesheet")) {
				MessageBox.post(desktop, "首选项j6_Standard_puid配置不正确", "ERROR", MessageBox.ERROR);//QKIAAI4S5kSPsC
				return null;
			}
			if(datasets[1]==null || !datasets[1].getType().equals("MSExcelX")) {
				MessageBox.post(desktop, "首选项j6_Standard_puid配置不正确", "ERROR", MessageBox.ERROR);//QiAAAI4S5kSPsC
				return null;
			}
			new ProcessAttriChangeDialog(session, datasets, task, ruleOpsMap, cols);
		}catch(Exception e) {
			e.printStackTrace();
			MessageBox.post(desktop, "错误："+e.getMessage(), "ERROR", MessageBox.ERROR);
			return null;
		}
		return null;
	}


}
