package com.connor.jingdiao.handlers;

import java.util.ArrayList;
import java.util.List;

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 WlbmApplyHandler 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 {
//			TCComponent
			String typeName = "";
			List<String> typeList = new ArrayList<String>();
			typeList.add("Part");
			typeList.add("J6_PRODUCT");
			typeList.add("J6_FUNCTIONASS");
			typeList.add("J6_DQPart");
			typeList.add("J6_WGPart");
			typeList.add("J6_VMPART");
			typeList.add("J6_GZPart");
			InterfaceAIFComponent[] targets = AIFUtility.getCurrentApplication().getTargetComponents();
			for(InterfaceAIFComponent t : targets) {
				
				if(t instanceof TCComponentItemRevision) {
					TCComponentItemRevision revision = (TCComponentItemRevision) t;
					String type = revision.getItem().getType();
					System.out.println("type==>"+type);
					if(!typeList.contains(type)) {
						MessageBox.post(desktop, "选中对象"+t+"不是物料版本类型，无法申请物料编码！", "ERROR", MessageBox.ERROR);
						return null;
					}
					typeName = type;
				}else if(t.getType().contains("J6_FUNCTIONASS")) {
					typeName = "J6_FUNCTIONASS";
				}
				else {
					MessageBox.post(desktop, "选中对象"+t+"不是物料版本类型，无法申请物料编码！", "ERROR", MessageBox.ERROR);
					return null;
				}
//				if(!t.getType().equals("Design Revision")) {
//					//判断是否为
//					return null;
//				}
			}
			String ruleNameString = "Jd_Code_Rule_"+typeName;
			
			String tables[] = session.getPreferenceService().getStringValues("Jd_Code_Table_Design"); //表头配置
			if(tables==null || tables.length==0) {
				MessageBox.post(desktop, "首选项配置Jd_Code_Table_Design不正确", "ERROR", MessageBox.ERROR);
				return null;
			}
			String options[] = session.getPreferenceService().getStringValues(ruleNameString); //编码规则配置
			if(options == null || options.length == 0) {
				MessageBox.post(desktop, "首选项"+ruleNameString+"配置不正确", "ERROR", MessageBox.ERROR);
				return null;
			}
			String sqlOptions[] = session.getPreferenceService().getStringValues("Jd_TC_Info_Connect"); //tc数据库链接
			if(sqlOptions==null || sqlOptions.length<4) {
				MessageBox.post(desktop, "首选项Jd_TC_Info_Connect配置不正确", "ERROR", MessageBox.ERROR);
				return null;
			}
			String sqlOptionsTI[] = session.getPreferenceService().getStringValues("Jd_TICODE_Info_Connect"); //TI数据库链接
			if(sqlOptionsTI==null || sqlOptionsTI.length<4) {
				MessageBox.post(desktop, "首选项Jd_TICODE_Info_Connect配置不正确", "ERROR", MessageBox.ERROR);
				return null;
			}
			
			String productClass[] = session.getPreferenceService().getStringValues("Jd_Code_Product_Class"); //TI数据库链接
			if(productClass==null || productClass.length==0) {
				MessageBox.post(desktop, "首选项Jd_Code_Product_Class配置不正确", "ERROR", MessageBox.ERROR);
				return null;
			}
			
			new WlbmApplyDialog(session, tables, options, sqlOptions, targets,sqlOptionsTI,productClass);
		}catch(Exception e) {
			e.printStackTrace();
			MessageBox.post(desktop, "错误："+e.getMessage(), "ERROR", MessageBox.ERROR);
			return null;
		}
		return null;
	}

}
