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

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.swing.BorderFactory;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import com.teamcenter.rac.util.MessageBox;
import com.teamcenter.rac.util.Registry;
import com.teamcenter.rac.util.iTextField;

import cn.net.connor.std.aifrcp.KDialog;
import cn.net.connor.std.aifrcp.KDialogController;
import cn.net.connor.std.aifrcp.util.KUtil;
import cn.net.connor.std.processedit.newmeop.NewMeopDialogController.MEOPType;
import cn.net.connor.std.processedit.util.PrefComponent;

@SuppressWarnings("serial")
public class NewMeopDialog extends KDialog {

	private JButton b_ok;
	private JButton b_cel;
	protected DefaultListModel<MEOPType> lm_type;
	protected JList<MEOPType> l_type;
	private NewMeopDialogController controller;
	private JButton b_assignId;
	private PrefComponent idComponent;
	private Map<PrefComponent,JButton> chooseComponents = new HashMap<PrefComponent, JButton>();
	private PrefComponent sequenceComponent;
	private JButton b_assignMeopNo;
	private JButton b_insertMeopNo;
	private JButton b_WorkCenter;
	protected iTextField tf_WorkCenter;
	private static final Registry REG = Registry.getRegistry(NewMeopDialog.class);

	public NewMeopDialog(KDialogController controller) {
		super(controller);
		this.controller = (NewMeopDialogController)controller;
	}
	
	private void setCompEnabled(boolean b) {
		b_ok.setEnabled(b);
		b_cel.setEnabled(b);
		for (JButton btn : chooseComponents.values()) {
			btn.setEnabled(b);
		}
	}
	
	public void setAssignId(String newId) {
		if(idComponent!=null&&b_assignId!=null) {
			if(KUtil.isEmpty(newId)) {
				idComponent.setValue(null);
				b_assignId.setEnabled(true);
			}else {
				idComponent.setValue(newId);
				b_assignId.setEnabled(false);
			}
		}
	}
	
	public void setMeopNo(String newId) {
		if(sequenceComponent!=null&&b_assignMeopNo!=null&&b_insertMeopNo!=null) {
			if(KUtil.isEmpty(newId)) {
				sequenceComponent.setValue(null);
//				b_assignMeopNo.setEnabled(true);
//				b_insertMeopNo.setEnabled(true);
			}else {
				sequenceComponent.setValue(newId);
//				b_assignMeopNo.setEnabled(false);
//				b_insertMeopNo.setEnabled(false);
			}
		}
	}

	@Override
	protected void addListeners() {
		if(b_WorkCenter != null)
			b_WorkCenter.addActionListener(new ActionListener() {//TODO b_WorkCenter
				@Override
				public void actionPerformed(ActionEvent e) {
					try {
						controller.chooseWorkCenter();
					} catch (Exception e1) {
						e1.printStackTrace();
						MessageBox.post(NewMeopDialog.this,e1);
					}
				}
			});
		b_ok.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				new Thread(new Runnable() {
					@Override
					public void run() {
						setCompEnabled(false);
						try {
							controller.doCreate();
						} catch (Exception e) {
							e.printStackTrace();
							MessageBox.post(NewMeopDialog.this,e);
						}
						setCompEnabled(true);
					}
				}).start();
			}
		});
		b_cel.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				disposeDialog();
			}
		});
		if(b_assignId!=null&&idComponent!=null) {
			b_assignId.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					try {
						controller.assignId();
					} catch (Exception e1) {
						e1.printStackTrace();
						MessageBox.post(NewMeopDialog.this,e1);
					}
				}
			});
		}
		if(this.sequenceComponent!=null&&this.b_assignMeopNo!=null&&this.b_insertMeopNo!=null) {
			b_assignMeopNo.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					try {
						controller.assignMeopNo();
					} catch (Exception e1) {
						e1.printStackTrace();
						MessageBox.post(NewMeopDialog.this,e1);
					}
				}
			});
			b_insertMeopNo.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					try {
						controller.insertMeopNo();

					} catch (Exception e1) {
						e1.printStackTrace();
						MessageBox.post(NewMeopDialog.this,e1);
					}
				}
			});
		}
		for (PrefComponent bean : chooseComponents.keySet()) {
			JButton btn = chooseComponents.get(bean);
			btn.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					MEOPType typeComp = l_type.getSelectedValue();
					if (typeComp == null) {
						MessageBox.post(NewMeopDialog.this, REG.getString("selectMeopType.INFO"), "", MessageBox.WARNING);
						return;
					}
				
					//bean.performChoose(NewMeopDialog.this, typeComp.type, controller.getApp(), controller.prefComponents);
					G4MDialog classDialog = new G4MDialog("¾«µñ·ÖÀà", null, controller.session,controller.prefComponents);
					new Thread(classDialog).start();
				}
			});
			
		}
		this.l_type.addListSelectionListener(new ListSelectionListener() {
			@Override
			public void valueChanged(ListSelectionEvent arg0) {
				if(arg0.getValueIsAdjusting()) {
					return;
				}
				System.out.println("Select Type Change > "+l_type.getSelectedValue());
				setAssignId(null);
				setMeopNo(null);
				if(controller.shortDescPrefComp!=null&&l_type.getSelectedValue()!=null) {
					controller.shortDescPrefComp.setValue(l_type.getSelectedValue().additional);
				}
				controller.clearWorkCenter();
			}
		});

	}

	@Override
	protected void initUI() {
		this.setTitle(REG.getString("newmeop.TITLE"));
		this.setPreferredSize(new Dimension(850, 780));
		this.setMinimumSize(new Dimension(850, 780));
		this.setLayout(new BorderLayout());
		this.add(BorderLayout.WEST, initTypePanel());
		this.add(BorderLayout.CENTER, initPropPanel());
		this.add(BorderLayout.SOUTH, initBtnPanel());
	}

	private Component initBtnPanel() {
		JPanel panel = new JPanel(new FlowLayout(FlowLayout.CENTER, 20, 5));
		this.b_ok = new JButton(REG.getString("ok"));
		this.b_cel = new JButton(REG.getString("cancel"));
		panel.add(b_ok);
		panel.add(b_cel);
		return panel;
	}

	private Component initPropPanel() {
		JPanel panel = new JPanel(new BorderLayout());
		panel.add(BorderLayout.CENTER, initMEPropPanel());
		panel.add(BorderLayout.SOUTH, initDESCPropPanel());
		return panel;
	}

	private Component initDESCPropPanel() {
		JPanel panel = new JPanel(new BorderLayout());
		if(controller.shortDescPrefComp!=null) {
			//panel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEmptyBorder(30, 5, 5, 5), ProcessLocale.getString("medesc.TITLE")));
			panel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEmptyBorder(30, 5, 5, 5), controller.shortDescPrefComp.getLabel().getText()));
			JScrollPane scroll = new JScrollPane(controller.shortDescPrefComp.getValueComp());
			panel.add(BorderLayout.CENTER, scroll);
			controller.prefComponents.add(controller.shortDescPrefComp);
		}
		return panel;
	}

	private Component initMEPropPanel() {
		JPanel wrapper = new JPanel(new BorderLayout());
		wrapper.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEmptyBorder(30, 5, 5, 5), REG.getString("meinfo.TITLE")));
		JPanel panel = new JPanel(new GridBagLayout());
		panel.setBorder(BorderFactory.createEmptyBorder(20,30,20,30));
		GridBagConstraints s = getGridBagConstraints(0, 0, new Insets(6, 6, 6, 6));
		List<PrefComponent> comps = controller.prefComponents;
		int colCnt = 1;
		int size = comps.size();
		for(int i=0;i<size;i++) {
			PrefComponent prefComponent = comps.get(i);
			if(i%colCnt==0) {
				KUtil.newRow(panel, prefComponent.label, s, 1);
			}else {
				KUtil.addCol(panel, prefComponent.label, s, 1, 0);
			}
			KUtil.addCol(panel, prefComponent.getValueComp(), s, 1, 1);
			if("item.item_id".equals(prefComponent.propConfig.toLowerCase())) {
				KUtil.addCol(panel, this.b_assignId = new JButton(REG.getString("assign")), s, 1, 0);
				this.idComponent = prefComponent;
			}
			//else if(KUtil.getLen(prefComponent.getExcelConfig())>0) {
			else if("item.object_name".equals(prefComponent.propConfig.toLowerCase())) {
				JButton b_choose = new JButton(REG.getString("select"));
				KUtil.addCol(panel, b_choose, s, 1, 0);
				chooseComponents.put(prefComponent, b_choose);
//			}else if("revmaster.ac5_OperationNumber".equalsIgnoreCase(prefComponent.propConfig)) {
			}else if("bomline.bl_sequence_no".equalsIgnoreCase(prefComponent.propConfig)) {
				KUtil.addCol(panel,this.b_assignMeopNo = new JButton(REG.getString("shenqing")), s, 1, 0);
				KUtil.addCol(panel,this.b_insertMeopNo = new JButton(REG.getString("insert")), s, 1, 0);
				this.b_insertMeopNo.setVisible(false);
				this.sequenceComponent = prefComponent;
			}
		}
		/*KUtil.newRow(panel, new JLabel(REG.getString("workcenter")), s, 1);
		this.tf_WorkCenter = new iTextField(18);
//		this.tf_WorkCenter.setRequired(true);
//		this.tf_WorkCenter.setEditable(false);//TODO tf_WorkCenter
		KUtil.addCol(panel, tf_WorkCenter, s, 1, 1);
		KUtil.addCol(panel, this.b_WorkCenter = new JButton(REG.getString("select")), s, 1, 0);*/
		s.weighty = 1;
		KUtil.newRow(panel, new JLabel(""), s, 1);
		JScrollPane scroll = new JScrollPane(panel);
		//scroll.setBorder(null);
		wrapper.add(BorderLayout.CENTER, scroll);
		return wrapper;
	}

	private Component initTypePanel() {
		JPanel panel = new JPanel(new BorderLayout());
		panel.setPreferredSize(new Dimension(200,0));
		panel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEmptyBorder(30, 5, 5, 0), REG.getString("meoptype.TITLE")));
		this.lm_type = new DefaultListModel<MEOPType>();
		this.l_type = new JList<MEOPType>(lm_type);
		for (MEOPType t : controller.meopTypes) {
			this.lm_type.addElement(t);
		}
		if(controller.meopTypes.size() == 1) {
			l_type.setSelectedIndex(0);
		}
		this.l_type.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		JScrollPane scroll = new JScrollPane(this.l_type);
		panel.add(BorderLayout.CENTER, scroll);
		return panel;
	}

}
