package com.connor.touliao;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

import javax.swing.JOptionPane;

import org.apache.commons.collections.map.HashedMap;
import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.Bullet;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.List;
import org.omg.CORBA.PRIVATE_MEMBER;

import com.sun.corba.se.impl.encoding.EncapsOutputStream;
import com.sun.org.apache.bcel.internal.generic.IF_ACMPEQ;
import com.teamcenter.rac.aif.AbstractAIFApplication;
import com.teamcenter.rac.aif.kernel.AIFComponentContext;
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.TCComponentForm;
import com.teamcenter.rac.kernel.TCComponentItemRevision;
import com.teamcenter.rac.kernel.TCComponentTask;
import com.teamcenter.rac.kernel.TCException;
import com.teamcenter.rac.kernel.TCSession;
import com.teamcenter.rac.kernel.TCTypeService;
import com.teamcenter.rac.kernel.TCComponentBOMLineType.BOMCompareListBomlinesObject;
import com.teamcenter.rac.util.MessageBox;

public class TouliaoHandler extends AbstractHandler {

	private Map<TCComponentItemRevision, Double> touliaoMap=new HashMap<TCComponentItemRevision, Double>();
	
	private Map<TCComponentItemRevision, TCComponentBOMLine> bomLineMap=new HashMap<TCComponentItemRevision, TCComponentBOMLine>();
	
	@Override
	public Object execute(ExecutionEvent arg0) throws ExecutionException {
		// TODO Auto-generated method stub
		
		AbstractAIFApplication app = AIFUtility.getCurrentApplication();
		TCSession session = (TCSession) app.getSession();
		InterfaceAIFComponent comp;
		comp=app.getTargetComponent();
		
		
//		List<>
		
		if(comp instanceof TCComponentBOMLine) {
			TCComponentBOMLine bomLine=(TCComponentBOMLine)comp;
			try {
				TCComponentItemRevision itemRevision = bomLine.getItemRevision();
				comp=itemRevision;
			} catch (TCException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		
		
		if( comp instanceof TCComponentItemRevision) {
			TCComponentItemRevision revision=(TCComponentItemRevision)comp;   //工艺对象
//			TCComponent[] whereUsed = null;
//			try {
//				whereUsed = revision.whereUsed((short) 0);
//			} catch (TCException e1) {
//				// TODO Auto-generated catch block
//				e1.printStackTrace();
//			}
//			System.out.println(whereUsed.length);
//			for(TCComponent component:whereUsed) {
//				try {
//					System.out.println(component.getStringProperty("object_string"));
//				} catch (TCException e) {
//					// TODO Auto-generated catch block
//					e.printStackTrace();
//				}
//			}
//			
//			TCComponent[] whereUsed2 = null;
//			try {
//				whereUsed2 = revision.whereUsed((short) 1);
//			} catch (TCException e1) {
//				// TODO Auto-generated catch block
//				e1.printStackTrace();
//			}
//			for(TCComponent component:whereUsed2) {
//				try {
//					System.out.println("1"+component.getStringProperty("object_string"));
//				} catch (TCException e) {
//					// TODO Auto-generated catch block
//					e.printStackTrace();
//				}
//			}
			if(revision.getType().equals("NHL8_AssemblyProRevision") 
					||revision.getType().equals("NHL8_PartsProRevision") 
					||revision.getType().equals("NHL8_DivergeProRevision") 
					){
				
				try {
					revision.refresh();
					System.out.println("刷新对象");
					TCComponent meTarget = revision.getRelatedComponent("IMAN_METarget");  //制造目标的bom
					String type = "";
					if(meTarget!=null) {
						TCComponentItemRevision meTargetRev =(TCComponentItemRevision) meTarget;
						
						type =	meTargetRev.getItem().getType();
						
						System.out.println(type);
					}
					
					if(type.equals("NHL8_Product") || type.equals("NHL8_Parts") 
							|| type.equals("NHL8_Fixture") || type.equals("NHL8_FixParts")) {
						
						TouliaoDialog dialog=new TouliaoDialog(session,revision,meTarget);
						session.queueOperation(dialog);
//						new Thread(dialog).start();
						
						
					}else {
						
						MessageBox.post("请选择正确的产品或零组件工艺","提示",1);
					}
				} catch (TCException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}else {
				MessageBox.post("请选择正确的产品或零组件工艺","提示",1);
			}
			
			
			
		}
		
		if(comp instanceof TCComponentTask) {
			TCComponentTask task = (TCComponentTask) comp;
			try {
				TCComponent[] targets = task.getRelatedComponents("root_target_attachments");
				for( int i = 0;i<targets.length;i++) {
					if(targets[i] instanceof TCComponentItemRevision) {
						TCComponentItemRevision revision = (TCComponentItemRevision) targets[i];
						if(revision.getType().equals("NHL8_AssemblyProRevision") 
								||revision.getType().equals("NHL8_PartsProRevision") 
								||revision.getType().equals("NHL8_DivergeProRevision") 
								){
							
							try {
								revision.refresh();
								System.out.println("刷新对象");
								TCComponent meTarget = revision.getRelatedComponent("IMAN_METarget");  //制造目标的bom
								String type = "";
								if(meTarget!=null) {
									TCComponentItemRevision meTargetRev =(TCComponentItemRevision) meTarget;
									
									type =	meTargetRev.getItem().getType();
									
									System.out.println(type);
								}
								
								if(type.equals("NHL8_Product") || type.equals("NHL8_Parts") 
										|| type.equals("NHL8_Fixture") || type.equals("NHL8_FixParts")) {
									
									TouliaoDialog dialog=new TouliaoDialog(session,revision,meTarget);
									session.queueOperation(dialog);
//									new Thread(dialog).start();
									
									
								}else {
									
									MessageBox.post("请选择正确的产品或零组件工艺","提示",1);
								}
							} catch (TCException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
							
						}else {
							MessageBox.post("请选择正确的产品或零组件工艺","提示",1);
						}
					}
				}
			} catch (TCException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		return null;
	}

//	private void CompareBomlines() {
//		// TODO Auto-generated method stub
//		for (Map.Entry<TCComponentItemRevision, TCComponentBOMLine> entry : bomLineMap.entrySet()) {
//			System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
//		}
//		
//		
//
//	}

	
	

}
