package jp.ac.kyutech.mns.ist.util;

import java.awt.event.ActionListener;
import java.awt.event.ItemListener;
import java.net.URL;
import java.util.ArrayList;
import java.util.Hashtable;
import java.awt.image.BufferedImage;
import java.awt.Color;
import java.awt.Graphics;

import javax.swing.AbstractButton;
import javax.swing.Action;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JToggleButton;
import javax.swing.JToolBar;

public class QuickMenuActionManager {
	protected KeepOrderHash<String, ArrayList<MyAction>> menurepos; // "File"=> ArrayList
	protected Hashtable<String, MyAction> name2act;
	protected Hashtable<String, AbstractButton> name2tbutton;

	public QuickMenuActionManager() {
		menurepos = new KeepOrderHash<String, ArrayList<MyAction>>();
		name2act = new Hashtable<String, MyAction>();
		name2tbutton = new Hashtable<String, AbstractButton>();
	}

	protected MyAction lastOne;

	static BufferedImage xxx;
	static {
		int w = 16;
		xxx = new BufferedImage(w, w, BufferedImage.TYPE_INT_RGB);
		Graphics g = xxx.getGraphics();
		g.setColor(Color.yellow);
		g.fillRect(1,1,w-2,w-2);
		g.setColor(Color.GREEN);
		g.drawLine(0, 0, w, w);
		g.drawLine(w, 0, 0, w);
	}

	public void addAction(String type, MyAction a) {
		ArrayList<MyAction> al = menurepos.get(type);
		if (al == null) {
			al = new ArrayList<MyAction>();
			al.add(a);
			menurepos.put(type, al);
		} else {
			al.add(a);
		}
		name2act.put((String) a.getValue(Action.NAME), a);
		System.out.println((String) a.getValue(Action.NAME));
		lastOne = a;
	}

	public void addHandler_Action(ActionListener al) {
		lastOne.addActionListenerToAry(al);
	}

	public void addHandler_Item(ItemListener il) {
		lastOne.addItemListenerToAry(il);
	}

	public MyAction getAction(String key) {
		return name2act.get(key);
	}

	public AbstractButton getButton(String key) {
		return name2tbutton.get(key);
	}

	public JMenuBar getMenuBar() {
		JMenuItem menuItem = null;
		JMenuBar menuBar;

		// Create the menu bar.
		menuBar = new JMenuBar();

		for (String type : menurepos.getIterator()) {
			JMenu mainMenu = new JMenu(type);
			for (Action a : menurepos.get(type)) {
				if (a.getValue("toggle") != null) {
					menuItem = new JCheckBoxMenuItem(a);
					a.putValue("togglemenu", menuItem);
				} else {
					menuItem = new JMenuItem(a);
				}
				// menuItem.setIcon(null); //arbitrarily chose not to use icon
				mainMenu.add(menuItem);
			}
			menuBar.add(mainMenu);
		}
		return menuBar;
	}

	public JToolBar getToolBar() {
		AbstractButton button = null;

		// Create the toolbar.
		JToolBar toolBar = new JToolBar();

		for (String type : menurepos.getIterator()) {
			for (Action a : menurepos.get(type)) {
				if (a.getValue("toggle") != null) {
					button = new JToggleButton(a);
					a.putValue("togglebutton", button);
				} else {
					button = new JButton(a);
				}
				if (button.getIcon() != null) {
					// button.setText(""); //an icon-only button
				}
				toolBar.add(button);
				name2tbutton.put(button.getText(), button);// あとでテキスト隠したりするときに参照が必要なので
				// System.out.println("BUTTON:: "+button.getText());
			}
			toolBar.addSeparator();
		}
		return toolBar;
	}

	/** Returns an ImageIcon, or null if the path was invalid. */
	public static ImageIcon createNavigationIcon(String imageName) {
		ClassLoader cl = QuickMenuActionManager.class.getClassLoader();

		if (cl == null) {
			System.err.println("Resource not found: " + imageName);
			return null;
		}
		String imgLocation = "toolbarButtonGraphics/"
				+ imageName
				+ ".gif";
		if (imageName.startsWith("_")) {
			imgLocation = "myicon/" + imageName.substring(1) + ".gif";
			ImageIcon ii;
			try {
				ii = new ImageIcon(cl.getResource(imgLocation));
			} catch (NullPointerException ex) {
				ii = new ImageIcon("resource/" + imgLocation);
			}
			return ii;
		} else {

			// java.net.URL imageURL = maincls.getResource(imgLocation);

			System.out.println(imgLocation);
			URL imgUrl = cl.getResource(imgLocation);
			if (imgUrl != null) return new ImageIcon(imgUrl);
			return new ImageIcon(xxx); // Cannot load image
		}
	}

	public void foldToolbarText(boolean f) {
		if (f) {
			for (AbstractButton ab : name2tbutton.values()) {
				if (ab.getIcon() != null) {
					ab.setText(null);
				}
			}
		} else {
			for (String key : name2tbutton.keySet()) {
				AbstractButton ab = name2tbutton.get(key);
				ab.setText(key);
				// ab.validate();
			}
		}
	}

}
