package cn.com.origin.autocode.classification.panels;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.ScrollPaneConstants;

import com.teamcenter.rac.aifrcp.AifrcpPlugin;
import com.teamcenter.rac.commands.newitem.NewItemDialog;
import com.teamcenter.rac.kernel.TCClassificationService;
import com.teamcenter.rac.kernel.TCException;
import com.teamcenter.rac.kernel.TCSession;
import com.teamcenter.rac.kernel.ics.ICSApplicationObject;
import com.teamcenter.rac.kernel.ics.ICSFormat;
import com.teamcenter.rac.kernel.ics.ICSPropertyDescription;
import com.teamcenter.rac.kernel.ics.ICSView;
import com.teamcenter.rac.util.Painter;
import com.teamcenter.rac.util.PropertyLayout;
import com.teamcenter.rac.util.VerticalLayout;

public class CreateClassificationPropertyPanel {
	JPanel mainPanel;
	JScrollPane scrollPane;
	ICSApplicationObject icsapplicationobject;
	ICSView icsView;
	List<Object> notNullClassificationPropTextField;
	HashMap<String, JComponent> propHashMap = new HashMap<String, JComponent>();
	TCClassificationService classificationService;
	HashMap<String,ICSPropertyDescription> iCSPropertyDescriptions= new HashMap<String, ICSPropertyDescription>();
	List icsPropList = new ArrayList();
	
	public TCSession getSession() {
		TCSession session = (TCSession) AifrcpPlugin.getSessionService().getDefaultSession();
		return session;
	}

	public CreateClassificationPropertyPanel() {
		notNullClassificationPropTextField = new ArrayList<Object>();
		mainPanel = new JPanel(new BorderLayout());
		// scrollPane = new JScrollPane(mainPanel);
		scrollPane = new JScrollPane();
		// scrollPane.getViewport().add(mainPanel);
		scrollPane.getViewport().setView(mainPanel);

		scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
		scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		classificationService = getSession().getClassificationService();
		try {
			icsapplicationobject = classificationService.newICSApplicationObject("ICM");
		} catch (TCException e) {
			e.printStackTrace();
		}
	}

	public JScrollPane getPropertyPanel() {
		return scrollPane;
	}

	public ICSApplicationObject getICSApplicationObject() {
		return icsapplicationobject;
	}

	public ICSView getICSView() {
		return icsView;
	}

	public TCClassificationService getClassificationService() {
		return classificationService;
	}

	public List<Object> getNotNullClassificationPropTextField() {
		return notNullClassificationPropTextField;
	}

	public void setNotNullClassificationPropTextField(List<Object> notNullClassificationPropTextField) {
		this.notNullClassificationPropTextField = notNullClassificationPropTextField;
	}

	public JScrollPane createPropertyPanel(String icsName, String icsID, NewItemDialog newItemDialog) {
		try {
			mainPanel.removeAll();
			icsapplicationobject.setView("Base", icsID);
			icsView = icsapplicationobject.getView();
			if (icsView == null) {
				return scrollPane;
			}

			ICSPropertyDescription[] aicspropertydescription = icsapplicationobject.getView().getPropertyDescriptions();
			JLabel jlabel = new JLabel(icsName);
			JPanel formPanel = new JPanel();
			if (aicspropertydescription != null && aicspropertydescription.length > 0) {
				formPanel.setLayout(new VerticalLayout(aicspropertydescription.length));
				for (int i = 0; i < aicspropertydescription.length; i++) {
					String dispalayName = aicspropertydescription[i].getDisplayName();
					boolean isMandatory = aicspropertydescription[i].isMandatory();
					ICSFormat isformat = aicspropertydescription[i].getFormat();
					JPanel panel = new JPanel(new GridLayout(1, 2, 2, 2));
					panel.setPreferredSize(new Dimension(0, 25));
					JLabel labelDisplayName = new JLabel(dispalayName);
					JLabel labelUnit = new JLabel(aicspropertydescription[i].getUnit());
					int lenth = isformat.getSize() > 20 ? 20 : isformat.getSize();
					JComponent jtextField = null;
					if (isformat.isList()) {
						String keyLovs[] = isformat.getKeyLov().getKeyValues();
						if (isMandatory) {
							jtextField = new JComboBox(keyLovs) {
								private static final long serialVersionUID = 1L;

								public void paint(Graphics g) {
									super.paint(g);
									Painter.paintIsRequired(this, g);
								}
							};
						} else {
							jtextField = new JComboBox(keyLovs);
						}
						((JComboBox)jtextField).setSelectedIndex(-1);
					} else {
						if (isMandatory) {
							jtextField = new JTextField(lenth) {
								private static final long serialVersionUID = 1L;

								public void paint(Graphics g) {
									super.paint(g);
									Painter.paintIsRequired(this, g);
								}
							};
						} else {
							jtextField = new JTextField(lenth);
						}
					}

					panel.add(labelDisplayName);

					ListValuesButton ListValuesButton = new ListValuesButton(icsView, aicspropertydescription[i], jtextField, labelDisplayName);
					ListValuesButton.setPreferredSize(new Dimension(28, 25));
					// panel.add(ListValuesButton);

					JPanel valuePanel = new JPanel(new PropertyLayout());
					valuePanel.add("1.1.left.center", new JLabel("   "));
					valuePanel.add("1.2.center.center", ListValuesButton);
					valuePanel.add("1.3.right.center", new JLabel("   "));
					panel.add(valuePanel);

					if (isMandatory) {
						notNullClassificationPropTextField.add(jtextField);
					}
					if (jtextField instanceof JTextField) {
						panel.add((JTextField) jtextField);
					} else if (jtextField instanceof JComboBox) {
						panel.add((JComboBox) jtextField);
					}
					panel.add(labelUnit);

					if (icsapplicationobject.getView().isAbstract()) {
						Component[] components = panel.getComponents();
						for (int m = 0; m < components.length; m++) {
							components[m].setEnabled(false);
						}
					}
					formPanel.add("top.bind.left.top", panel);
					// int propertyId = aicspropertydescription[i].getId();
					// propHashMap.put(String.valueOf(propertyId), jtextField);
					String propName = aicspropertydescription[i].getName();
					propHashMap.put(propName, jtextField);
					iCSPropertyDescriptions.put(propName, aicspropertydescription[i]);
					icsPropList.add(propName);
				}
			}
			mainPanel.add(jlabel, BorderLayout.NORTH);
			mainPanel.add(formPanel, BorderLayout.CENTER);
			mainPanel.repaint();
			mainPanel.validate();

			scrollPane.repaint();
			scrollPane.validate();
		} catch (TCException e) {
			e.printStackTrace();
		}
		return scrollPane;
	}

	public HashMap<String, JComponent> getPropertyInfos() {
		return propHashMap;
	}
	public HashMap<String, ICSPropertyDescription> getICSPropertyDescriptions() {
		return iCSPropertyDescriptions;
	}
	
	public List getIcsPropList() {
		return icsPropList;
	}
}
