package cn.net.connor.std.processedit.ldy;

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

//创建常规文件夹结构
public class ProcessDivisionHandler extends AbstractHandler implements IHandler{

	
	public Object execute(ExecutionEvent arg0) throws ExecutionException {
		System.out.println("ProcessDivisionHandler");
		AIFDesktop desktop = AIFUtility.getActiveDesktop();
		try {
			AbstractAIFUIApplication app = AIFUtility.getCurrentApplication();
			TCSession session = (TCSession) app.getSession();
			InterfaceAIFComponent[] targetComponents = app.getTargetComponents();
			
			List<TCComponentBOMLine> tcList = new ArrayList<>();
			for (InterfaceAIFComponent comp : targetComponents) {
			    if (comp instanceof TCComponentBOMLine) {
			        tcList.add((TCComponentBOMLine) comp);
			    }else if(comp instanceof TCComponentItemRevision){
			    	TCComponentItemRevision revision = (TCComponentItemRevision)comp;
					TCComponentBOMWindowType bomWinType = (TCComponentBOMWindowType) session.getTypeComponent("BOMWindow");
					TCComponentBOMWindow window = bomWinType.create(null);
					TCComponentBOMLine newBom = window.setWindowTopLine(null, revision, null, null);
					tcList.add(newBom);
			    }
			    // 可选：记录非 TCComponent 对象用于调试
			    // else { System.out.println("跳过非TCComponent对象: " + comp); }
			}

			// 3. 转换为数组
			TCComponentBOMLine[] target = tcList.toArray(new TCComponentBOMLine[0]);
			
			if(target.length <= 0) {
				MessageBox.post(desktop, "请选择工艺路线对象进行工艺分工", "ERROR", MessageBox.ERROR);
				return null;
			}
			/*
			 * String type = ((TCComponentBOMLine)target).getItemRevision().getType();
			 * if(!type.equals("LD6_MEProcessCrRevision")) { MessageBox.post(desktop,
			 * "请选择工艺路线BOM进行工艺分工", "ERROR", MessageBox.ERROR); return null; }
			 */
			
			String[] options = session.getPreferenceService().getStringValues("Connor_GYFG");
			if(options==null || options.length==0) {
				MessageBox.post(desktop, "首选项Connor_GYFG配置不正确", "ERROR", MessageBox.ERROR);
				return null;
			}
//			String optionC[] = session.getPreferenceService().getStringValues("Connor_AVIC_Apply_Create");
//			if(optionC==null || optionC.length==0) {
//				MessageBox.post(AIFUtility.getActiveDesktop(), "首选项Connor_AVIC_Apply_Create配置不正确", "ERROR", MessageBox.ERROR);
//				return null;
//			}
//			String options[] = new String[] { "名称", "状态" };
			new ProcessDivisionDialog(session, target, options);
		}catch(Exception e) {
			e.printStackTrace();
			MessageBox.post(desktop, "错误："+e.getMessage(), "ERROR", MessageBox.ERROR);
			return null;
		}
		return null;
	}
	
}
