package com.connor.jingdiao.handlers;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

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.teamcenter.rac.aif.AIFDesktop;
import com.teamcenter.rac.aif.kernel.InterfaceAIFComponent;
import com.teamcenter.rac.aifrcp.AIFUtility;
import com.teamcenter.rac.kernel.TCComponentBOMLine;
import com.teamcenter.rac.kernel.TCComponentItemRevision;
import com.teamcenter.rac.kernel.TCSession;
import com.teamcenter.rac.util.MessageBox;

//创建常规文件夹结构
public class DrawingNoApplyHandler extends AbstractHandler implements IHandler{
	
	public Object execute(ExecutionEvent arg0) throws ExecutionException {
		System.out.println("DrawingNoApplyHandler");
		TCSession session = (TCSession) AIFUtility.getCurrentApplication().getSession();
		AIFDesktop desktop = AIFUtility.getActiveDesktop();
		try {
			Map<String, String[]> ruleOpsMap = new HashMap<String, String[]>();
			String ruleString = "Jd_Code_Rule_";
			InterfaceAIFComponent[] targets = AIFUtility.getCurrentApplication().getTargetComponents();
			for(InterfaceAIFComponent t : targets) {
				TCComponentItemRevision revision;
				if(t instanceof TCComponentItemRevision) {
					revision = (TCComponentItemRevision) t;
				}else if(t instanceof TCComponentBOMLine) {
					revision = ((TCComponentBOMLine) t).getItemRevision();
				}else {
					MessageBox.post(desktop, "选中对象"+t+"不是设计版本类型，无法申请图号！", "ERROR", MessageBox.ERROR);
					return null;
				}
				String type = revision.getItem().getType();
				if(!ruleOpsMap.containsKey(type)) {
					String[] options = session.getPreferenceService().getStringValues(ruleString+type);
					if(options==null || options.length==0) {
						MessageBox.post(desktop, "选中对象"+t+"不是设计版本类型，无法申请图号！", "ERROR", MessageBox.ERROR);
						return null;
					}else {
						ruleOpsMap.put(type, options);
					}
				}
//				if(!(t instanceof TCComponentItemRevision) || !t.getType().contains("Design")) {// Revision
//					MessageBox.post(desktop, "选中对象"+t+"不是设计版本类型，无法申请图号！", "ERROR", MessageBox.ERROR);
//					return null;
//				}else {
//					TCComponentItemRevision revision = (TCComponentItemRevision)t;
//					String type = revision.getItem().getType();
//					if(!ruleOpsMap.containsKey(type)) {
//						String typeString = ruleString + type;
//						String[] options = session.getPreferenceService().getStringValues(typeString);
//						ruleOpsMap.put(type, options);
//					}
//					
//				}
			}
			String ruleType = "Jd_Code_Rule_Design";
			
			
			
			ArrayList<String[]> list = new ArrayList<>();
			String[] ops = new String[] { "Jd_Code_Table_Design", ruleType, "Jd_TC_Info_Connect", 
					"JD6_Part_Design_Type", "JD6_Part_Design_Copy_Properties" };
			for(String name : ops) {
				String[] options = session.getPreferenceService().getStringValues(name);
				if(options == null || options.length == 0) {
					MessageBox.post(desktop, "首选项"+name+"配置不正确", "ERROR", MessageBox.ERROR);
					return null;
				}
				list.add(options);
			}
			new DrawingNoApplyDialog(session, list, targets,ruleOpsMap);
		}catch(Exception e) {
			e.printStackTrace();
			MessageBox.post(desktop, "错误："+e.getMessage(), "ERROR", MessageBox.ERROR);
			return null;
		}
		return null;
	}
}
