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

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JPanel;

import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.StructuredSelection;

import com.teamcenter.rac.aif.AIFDesktop;
import com.teamcenter.rac.aif.AbstractAIFDialog;
import com.teamcenter.rac.aif.AbstractAIFUIApplication;
import com.teamcenter.rac.aif.kernel.InterfaceAIFComponent;
import com.teamcenter.rac.classification.common.AbstractG4MApplication;
import com.teamcenter.rac.classification.common.G4MApplicationPanel;
import com.teamcenter.rac.classification.common.G4MUserAppContext;
import com.teamcenter.rac.classification.common.table.G4MTable;
import com.teamcenter.rac.classification.icm.ICMApplication;
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.TCComponentICO;
import com.teamcenter.rac.kernel.TCComponentItem;
import com.teamcenter.rac.kernel.TCComponentItemRevision;
import com.teamcenter.rac.kernel.TCException;
import com.teamcenter.rac.kernel.TCSession;
import com.teamcenter.rac.util.MessageBox;

import cn.net.connor.std.aifrcp.util.KUtil;

@SuppressWarnings("serial")
public class G4MDialog extends AbstractAIFDialog {

	private JButton b_ok;
	private JButton b_cel;
	private ICMApplication app;
	private MyG4MContext m_context;
	protected TCComponentICO ico;
	private String expandClass;
	private List<TCComponentItemRevision> revList;
	private TCComponentBOMLine targeTcComponentBOMLine;
	InterfaceAIFComponent iaf;
	TCComponentForm form;
	TCSession session;
	//	public G4MDialog(String expandClass, InterfaceAIFComponent iaf, QZjsbjdBean jsbjdBean, TCSession session, TCComponentForm form) {
	//		super(AIFDesktop.getActiveDesktop(), true);
	//		this.jsbjdBean=jsbjdBean;
	//		this.expandClass = expandClass;
	//		this.iaf = iaf;
	//		this.session=session;
	//		this.form=form;
	//		try {
	//			this.app = new ICMApplication();
	//			this.m_context = new MyG4MContext(app, "k0");
	//		} catch (Exception e) {
	//			e.printStackTrace();
	//		}
	//		initUI();
	//	}
	public G4MDialog(String expandClass, InterfaceAIFComponent iaf, TCSession session,TCComponentBOMLine targeTcComponentBOMLine) {
		super(AIFDesktop.getActiveDesktop(), true);
		this.expandClass = expandClass;
		this.iaf = iaf;
		this.session=session;
		this.targeTcComponentBOMLine = targeTcComponentBOMLine;
		try {
			this.app = new ICMApplication();
			this.m_context = new MyG4MContext(app, "k0");
		} catch (Exception e) {
			e.printStackTrace();
		}
		initUI();
	}

	@Override
	public void run() {
		showDialog();
	}

	private void initUI() {
		this.setTitle("材料选取");
		this.setLayout(new BorderLayout());
		this.setPreferredSize(new Dimension(1000, 600));
		this.setMinimumSize(new Dimension(640, 480));
		MyG4MPanel panel = new MyG4MPanel(null);
		try {
			m_context.openComponent(iaf);
			panel.initialize(m_context);
			this.add(BorderLayout.CENTER, panel);
			m_context.getTree().expandToClass(expandClass);
			//			new Thread(m_context.getClassificationDialog()).start();
		} catch (Exception e) {
			e.printStackTrace();
		}
		//  panel.open(iaf);
		this.add(BorderLayout.SOUTH, initBtnPanel());
		addListeners();
	}

	private void addListeners() {
		b_ok.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				G4MTable table = m_context.getTablePane().getG4MTable();
				List<?> list = null;
				if (table != null) {
					ISelection sel = table.getSelection();
					if (sel != null && sel instanceof StructuredSelection) {
						list = ((StructuredSelection) sel).toList();

					}
				}
				if (list == null || list.size() == 0) {
					MessageBox.post("请选择要使用的对象", "", MessageBox.INFORMATION);
					//     KUtil.info(G4MDialog.this, "请选择要使用的对象");
					return;
				}
				if (list.size() > 1) {
					MessageBox.post("请选择单行数据", "", MessageBox.INFORMATION);
					//     KUtil.info(G4MDialog.this, "请选择单行数据");
					return;
				}
				Object target = list.get(0);
				revList =  new ArrayList<TCComponentItemRevision>();
				if (target != null && target instanceof TCComponentICO) {
					ico = (TCComponentICO) target;
					System.out.println(" ico.getClassId()=====" + ico.getClassId());

					TCComponent classifiedObject = ico.getClassifiedObject();
					TCComponentItemRevision rev = null;
					if(classifiedObject instanceof TCComponentItemRevision) {
						rev = (TCComponentItemRevision) classifiedObject;
					}else if(classifiedObject instanceof TCComponentItem) {
						TCComponentItem item = (TCComponentItem) classifiedObject;
						try {
							rev = item.getLatestItemRevision();
						} catch (TCException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
					}
					
					
					System.out.println("ico.id==="+ico.getId());
				
					
					System.out.println("rev==="+rev);
					revList.add(rev);
					
					
					if(revList != null && revList.size() > 0) {
			
						try {
							TCComponentItemRevision revision = (TCComponentItemRevision)targeTcComponentBOMLine.getRelatedComponent("bl_line_object");
								KUtil.setByPassItk(session, true);
								TCComponentBOMWindowType bomWinType = (TCComponentBOMWindowType) session.getTypeComponent("BOMWindow");
								TCComponentBOMWindow window = bomWinType.create(null);
								TCComponentBOMLine newBom = window.setWindowTopLine(null, revision, null, null);
								for (int i = 0; i < revList.size(); i++) {
									TCComponentItemRevision tcComponentItemRevision = revList.get(i);
									newBom.add(tcComponentItemRevision.getItem(), tcComponentItemRevision, null, false);
								}
								window.save();
								window.close();
								KUtil.setByPassItk(session, false);
								

						} catch (TCException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
					}else {
						MessageBox.post("请选择分类对象!", "提示", MessageBox.INFORMATION);
						
					}
					
					
					
					MessageBox.post("引用成功！！！", "成功", MessageBox.INFORMATION);
				} else {
					MessageBox.post("选择的数据不是ICO对象", "", MessageBox.INFORMATION);
					//     KUtil.info(G4MDialog.this, "选择的数据不是ICO对象");
					return;
				}
			}
		});
		b_cel.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				disposeDialog();
			}
		});
	}



	private JPanel initBtnPanel() {
		JPanel panel = new JPanel(new FlowLayout(FlowLayout.CENTER, 25, 5));
		this.b_ok = new JButton("确定");
		this.b_cel = new JButton("取消");
		panel.add(b_ok);
		panel.add(b_cel);
		return panel;
	}

	public class MyG4MContext extends G4MUserAppContext {

		public MyG4MContext(AbstractAIFUIApplication paramAbstractAIFUIApplication, String paramString)
				throws Exception {
			super(paramAbstractAIFUIApplication, paramString);
		}

		@Override
		public void setBusy(String paramString) {
		}

		@Override
		public void setIdle(String paramString) {
		}

	}

	public class MyG4MPanel extends G4MApplicationPanel {

		public MyG4MPanel(AbstractG4MApplication paramAbstractG4MApplication) {
			super(paramAbstractG4MApplication);
		}

		@Override
		public void closeSignaled() {
			try {
				m_context.clear();
			} catch (Exception e) {
				e.printStackTrace();
			}
			//			m_context.openComponent(iaf);
		}


	}

}
