package com.connor.jingdiao.handlers;

import java.util.ArrayList;

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.TCComponentItemRevision;
import com.teamcenter.rac.kernel.TCSession;
import com.teamcenter.rac.util.MessageBox;

//创建常规文件夹结构
public class DesignToPartHandler extends AbstractHandler implements IHandler{
	
	public Object execute(ExecutionEvent arg0) throws ExecutionException {
		System.out.println("DesignToPartHandler");
		TCSession session = (TCSession) AIFUtility.getCurrentApplication().getSession();
		AIFDesktop desktop = AIFUtility.getActiveDesktop();
		try {
			String[] types = session.getPreferenceService().getStringValues("JD6_Part_Design_Type");
			if(types == null || types.length == 0) {
				MessageBox.post(desktop, "首选项JD6_Part_Design_Type配置不正确", "ERROR", MessageBox.ERROR);
				return null;
			}
			InterfaceAIFComponent[] targets = AIFUtility.getCurrentApplication().getTargetComponents();
			for(InterfaceAIFComponent t : targets) {
				if(t instanceof TCComponentItemRevision) {
					TCComponentItemRevision rev = (TCComponentItemRevision) t;
					if(rev.getProperty("j6_drawingno").isEmpty()) {
						MessageBox.post(desktop, "选中对象"+t+"未申请图号，无法创建物料！", "ERROR", MessageBox.ERROR);
						return null;
					}
					String type = rev.getItem().getType()+";";
					boolean no = true;
					for(String s : types) {
						if(s.startsWith(type)) {
							no = false;
							break;
						}
					}
					if(no) {
						MessageBox.post(desktop, "选中对象"+t+"不是设计版本类型，无法创建物料！", "ERROR", MessageBox.ERROR);
						return null;
					}
				}else {
					MessageBox.post(desktop, "选中对象"+t+"不是设计版本类型，无法创建物料！", "ERROR", MessageBox.ERROR);
					return null;
				}
			}
			ArrayList<String[]> list = new ArrayList<>();
			String[] ops = new String[] { "Jd_Code_Table_Design", "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 DesignToPartDialog(session, list, targets);
		}catch(Exception e) {
			e.printStackTrace();
			MessageBox.post(desktop, "错误："+e.getMessage(), "ERROR", MessageBox.ERROR);
			return null;
		}
		return null;
	}
}
