package cn.dongjak.table2bean.ui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.AbstractListModel;
import javax.swing.JCheckBox;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListCellRenderer;
import javax.swing.ListSelectionModel;

import org.apache.commons.lang3.ArrayUtils;
import org.dom4j.Element;

import cn.dongjak.table2bean.Consts;
import cn.dongjak.table2bean.utils.ConfigurationAccessor;

public class PluginConfigurationPanel extends JPanel {

	private static final long serialVersionUID = 2289030491566292707L;
	private ConfigurationAccessor configurationAccessor = new ConfigurationAccessor(
			Consts.table2beanXMLFile);
	private JList<Plugin> list;
	private Plugin[] plugins = null;

	public Plugin[] getPlugins() {
		return plugins;
	}

	public void setPlugins(Plugin[] plugins) {
		this.plugins = plugins;
	}

	public PluginConfigurationPanel() {
		setLayout(new BorderLayout(0, 0));

		JScrollPane scrollPane = new JScrollPane();
		add(scrollPane);

		list = new JList<Plugin>();
		scrollPane.setViewportView(list);
		// final CheckListManager checkListManager = new CheckListManager(list);
		list.setCellRenderer(new PluginListRenderer());
		list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		list.setSelectionBackground(new Color(177, 232, 58));// 186,212,239,177,232,58
		list.setSelectionForeground(Color.black);
		list.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent event) {
				JList list = (JList) event.getSource();
				// Get index of item clicked
				// »ñµÃÓÃ»§µã»÷ÏîµÄË÷Òý
				int index = list.locationToIndex(event.getPoint());
				Plugin plugin = (Plugin) list.getModel().getElementAt(index);
				// ÉèÖÃÁÐ±íÖÐÏîµÄÑ¡Ôñ×´Ì¬
				plugin.enable = !plugin.enable;
				// ÖØÐÂ»æÖÆÁÐ±íÖÐÏî
				list.repaint(list.getCellBounds(index, index));
			}

			// public void mousePressed(MouseEvent e) {
			// // TODO Auto-generated method stub
			// first = list.locationToIndex(e.getPoint());
			// }
			//
			// public void mouseReleased(MouseEvent e) {
			// // TODO Auto-generated method stub
			// sec = list.locationToIndex(e.getPoint());
			// if (sec != -1) {
			// if (first != sec) {
			// String s1 = model.getElementAt(first).toString();
			// String s2 = model.getElementAt(sec).toString();
			// model.setElementAt(cli[first], sec);
			// model.setElementAt(cli[sec], first);
			// model.copyInto(cli);
			// list.setModel(model);
			// }
			// }
			// }
		});
		addComponentListener(new ComponentAdapter() {
			@Override
			public void componentShown(ComponentEvent e) {
				for (Element element : configurationAccessor.getPluginManager()
						.getPlugins())
					plugins = ArrayUtils
							.add(plugins,
									new Plugin(element.elementText("name"),
											new Boolean(element
													.elementText("enable"))));
				list.setModel(new PluginListModel(plugins));
			}
		});

	}

}

class Plugin {
	String name;
	boolean enable;

	public Plugin(String name, boolean enable) {
		super();
		this.name = name;
		this.enable = enable;
	}

	@Override
	public String toString() {
		return name;
	}

}

class PluginListRenderer extends JCheckBox implements ListCellRenderer<Plugin> {
	private static final long serialVersionUID = -881457682805555741L;

	@Override
	public Component getListCellRendererComponent(JList<? extends Plugin> list,
			Plugin value, int index, boolean isSelected, boolean cellHasFocus) {
		setEnabled(list.isEnabled());
		setSelected(((Plugin) value).enable);
		setFont(list.getFont());
		if (isSelected) {
			this.setBackground(list.getSelectionBackground());
			this.setForeground(list.getSelectionForeground());
		} else {
			this.setBackground(list.getBackground());
			this.setForeground(list.getForeground());
		}
		setText(value.toString());
		return this;
	}

}

class PluginListModel extends AbstractListModel<Plugin> {

	private static final long serialVersionUID = 2198696432870337061L;
	private Plugin[] plugins;

	public PluginListModel(Plugin[] plugins) {
		super();
		this.plugins = plugins;
	}

	@Override
	public int getSize() {
		return this.plugins.length;
	}

	@Override
	public Plugin getElementAt(int index) {
		return this.plugins[index];
	}

}
