package cn.net.connor.std.processedit.processview.safeicon;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Enumeration;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.table.DefaultTableModel;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;

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

import cn.net.connor.std.aifrcp.KDialog;
import cn.net.connor.std.processedit.util.CheckBoxTreeCellRenderer;
import cn.net.connor.std.processedit.util.CheckBoxTreeNode;
import cn.net.connor.std.processedit.util.CheckBoxTreeNodeSelectionListener;

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

	private static Registry REG = Registry.getRegistry(SafeIconDialog.class);
	private JButton b_OK;
	private JButton b_Cancel;
	private SafeIconBean rootNode;
	private JTree tree;
	private DefaultTableModel tm_SafeIcon;
	private CheckBoxTreeNode topNode;
	
	public SafeIconDialog(SafeIconBean rootNode, DefaultTableModel tm_SafeIcon) {
		super();
		this.rootNode = rootNode;
		this.tm_SafeIcon = tm_SafeIcon;
	}
	
	@Override
	protected void addListeners() {
		b_Cancel.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				disposeDialog();
			}
		});
		b_OK.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				try {
					addSelectedIcons();
					disposeDialog();
				} catch (Exception e1) {
					e1.printStackTrace();
					MessageBox.post(SafeIconDialog.this, e1);
				}
			}
		});
	}

	protected void addSelectedIcons() throws Exception{
		addIcon(topNode);
	}

	private void addIcon(CheckBoxTreeNode pNode) throws Exception{
		if(pNode==null) {
			return;
		}
		if(pNode.isSelected()) {
			SafeIconBean bean = (SafeIconBean) pNode.getUserObject();
			if(bean.getIconDataset()!=null) {
				System.out.println(">> Add icon: "+bean);
				boolean added = false;
				int rowCnt = tm_SafeIcon.getRowCount();
				for(int i=0; i<rowCnt; i++) {
					SafeIconBean rowBean = (SafeIconBean) tm_SafeIcon.getValueAt(i, 1);
					if(rowBean.getIconDataset().equals(bean.getIconDataset())) {
						added = true;
						break;
					}
				}
				if(added) {
					System.out.println(">> skip");
				}else {
					tm_SafeIcon.addRow(bean.getRowData(rowCnt+1));
				}
			}
		}
		int cnt = pNode.getChildCount();
		for(int i=0; i<cnt; i++) {
			addIcon((CheckBoxTreeNode) pNode.getChildAt(i));
		}
	}

	@Override
	protected void initUI() {
		this.setTitle(REG.getString("dialog.TITLE"));
		this.setPreferredSize(new Dimension(350, 400));
		this.add(BorderLayout.CENTER, initTreePanel());
		this.add(BorderLayout.SOUTH, initBtnPanel());
	}

	private Component initBtnPanel() {
		JPanel panel = new JPanel(new FlowLayout(FlowLayout.CENTER, 25, 7));
		panel.add(this.b_OK = new JButton(REG.getString("ok.BUTTON")));
		panel.add(this.b_Cancel = new JButton(REG.getString("cancel.BUTTON")));
		return panel;
	}

	private JPanel initTreePanel() {
//		DefaultTreeCellRenderer treeCellRenderer = new DefaultTreeCellRenderer() {
//			@Override
//			public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus) {
//				super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);
//				if(value!=null&&(value instanceof SafeIconTreeNode)) {
//					this.setIcon(((SafeIconTreeNode)value).getIcon());
//				}
//				return this;
//			}
//		};

		this.topNode = new CheckBoxTreeNode(rootNode);
		tree = new JTree(topNode);
		// expandAll(tree, new TreePath(topNode), true);
		tree.setEditable(false);
		tree.setCellRenderer(new CheckBoxTreeCellRenderer());
		tree.putClientProperty("JTree.lineStyle", "Horizontal");
		tree.setRowHeight(30);
		tree.setRootVisible(false);
		tree.addMouseListener(new CheckBoxTreeNodeSelectionListener());
		cycleNode(topNode, rootNode);
		expandTree(tree, new TreePath(topNode));
		JPanel panel = new JPanel(new BorderLayout());
		panel.setBorder(BorderFactory.createEmptyBorder(7, 7, 0, 7));
		JScrollPane scroll = new JScrollPane(tree);
		panel.add(BorderLayout.CENTER, scroll);
		return panel;
	}

	private void cycleNode(CheckBoxTreeNode parentTreeNode, SafeIconBean parentIcon){
		List<SafeIconBean> children = parentIcon.getChildren();
		if(children==null || children.size()==0) {
			return;
		}
		for(int i=0; i<children.size(); i++) {
			SafeIconBean childIcon = children.get(i);
			CheckBoxTreeNode childTreeNode = new CheckBoxTreeNode(childIcon);
			parentTreeNode.add(childTreeNode);
			cycleNode(childTreeNode, childIcon);
		}
	}
	
	public static void expandTree(JTree tree, TreePath parent) {
		if (parent == null) {
			return;
		}
		TreeNode node = (TreeNode) parent.getLastPathComponent();
		if (node.getChildCount() >= 0) {
			for (Enumeration<?> e = node.children(); e.hasMoreElements();) {
				TreeNode n = (TreeNode) e.nextElement();
				TreePath path = parent.pathByAddingChild(n);
				expandTree(tree, path);
			}
		}
		tree.expandPath(parent);
	}

}
