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.TCComponent;
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 CodeApplyHandler extends AbstractHandler implements IHandler{
	
	public Object execute(ExecutionEvent arg0) throws ExecutionException {
		System.out.println("CodeApplyHandler");
		TCSession session = (TCSession) AIFUtility.getCurrentApplication().getSession();
		AIFDesktop desktop = AIFUtility.getActiveDesktop();
		try {
			InterfaceAIFComponent[] targets = AIFUtility.getCurrentApplication().getTargetComponents();
			ArrayList<TCComponent> target = new ArrayList<>();
			ArrayList<TCComponent> boms = new ArrayList<>();
			StringBuilder msg = new StringBuilder("");
			for(InterfaceAIFComponent t : targets) {
				TCComponentItemRevision rev;
				TCComponentBOMLine bom = null;
				if(t instanceof TCComponentItemRevision) {
					rev = (TCComponentItemRevision) t;
				}else if(t instanceof TCComponentBOMLine) {
					bom = (TCComponentBOMLine) t;
					rev = bom.getItemRevision();
				}else {
					msg.append("选中对象 ").append(t).append(" 不是BOM或版本对象\n");
					continue;
//					MessageBox.post(desktop, "选中对象 "+t+" 不是BOM或版本对象，请重新选择！", "ERROR", MessageBox.ERROR);
//					return null;
				}
				if(rev.getTCProperty("j6_wlbm")==null) {
					msg.append("选中对象 ").append(t).append(" 没有物料编码属性\n");
					continue;
//					MessageBox.post(desktop, "选中对象 "+rev+" 没有物料编码属性，请正确选择物料对象！", "ERROR", MessageBox.ERROR);
//					return null;
				}
				if(!rev.getProperty("j6_wlbm").isEmpty()) {
					msg.append("选中对象 ").append(t).append(" 已有物料编码，无法再次申请\n");
					continue;
//					MessageBox.post(desktop, "选中对象"+t+"已有物料编码，无法再次申请！", "ERROR", MessageBox.ERROR);
//					return null;
				}
				if(bom != null)
					boms.add(bom);
				else
					target.add(rev);
			}
			ArrayList<String[]> list = new ArrayList<>();
			String[] ops = new String[] { "Jd_Code_Apply", "Jd_TC_Info_Connect", "Jd_TICODE_Info_Connect", "Jd_Code_Product_Class" };
			if(boms.size()>0)
				ops[0] = "Jd_BOM_Code_Apply";
			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);
			}
			if(boms.size()==0 && target.size()==0) {
				MessageBox.post(desktop, "以下对象申请物料编码遇到问题：\n"+msg, "提示", MessageBox.ERROR);
				return null;
			}else if(boms.size()>0)
				new CodeApplyOperation(session, boms, list, msg).executeOperation();
			else
				new CodeApplyOperation(session, target, list, msg).executeOperation();
		}catch(Exception e) {
			e.printStackTrace();
			MessageBox.post(desktop, "错误："+e.getMessage(), "ERROR", MessageBox.ERROR);
			return null;
		}
		return null;
	}
}
