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

import java.awt.Color;
import java.util.ArrayList;
import java.util.Hashtable;

import edu.umd.cs.piccolo.PCamera;
import edu.umd.cs.piccolo.event.PBasicInputEventHandler;
import edu.umd.cs.piccolo.event.PInputEvent;

/**
 * Cascading Menu
 * @author miuramo
 */
public class PMenuItem_Cascade extends PMenuItem_Abstract implements PMenuParent {
	private static final long serialVersionUID = 5516411970722622274L;
	ArrayList<PMenuItem_Abstract> items = new ArrayList<PMenuItem_Abstract>();
	Hashtable<String, PMenuItem_Abstract> itemsHash = new Hashtable<String, PMenuItem_Abstract>();
	PCamera camera;
	boolean isHorizontal;
	
	boolean showingSubmenu = false;
	PMenuItem_Abstract selected = null; // 選択中のメニュー
	
	
	public PMenuItem_Cascade(String arg, boolean m, Color c, PCamera pc, boolean hori) {
		super(arg, m, c);
		camera = pc;
		isHorizontal = hori;

		addInputEventListener(new PBasicInputEventHandler() {
			public void mouseEntered(PInputEvent e) {
				setPaint(defaultcolor);
			}

			public void mouseExited(PInputEvent e) {
				setSelected(isSelected);
			}

			public void mousePressed(PInputEvent e) {
				setPaint(Color.red);
			}

			public void mouseReleased(PInputEvent e) {
				setSelected(isSelected);
			}

			public void mouseClicked(PInputEvent e) {
//				doClick(e);
			}
		});

	}
	public void addPMI(PMenuItem_Abstract pma, String title){
		items.add(pma);
		itemsHash.put(title, pma);
		pma.parent = this;
	}
	public PMenuItem_Abstract getPMI(String title){
		return itemsHash.get(title);
	}
	public void addStringMenu(String title, Color c){
		PMenuItem_String mi = new PMenuItem_String(title, false, c);
		addPMI(mi, title);
//		mi.addPActionListener(new PActionListener() {
//			public void actionPerformed(PInputEvent e) {
//				System.out.println("PRESS!! "+title);
//			}
//		});		
	}
	
	public void addStringMenu(String title, Color c, boolean b){
		PMenuItem_String mi = new PMenuItem_String(title, b, c);
		addPMI(mi, title);
//		mi.addPActionListener(new PActionListener() {
//			public void actionPerformed(PInputEvent e) {
//				System.out.println("PRESS!! "+title);
//			}
//		});		
	}

	public void doClick(PInputEvent e) {
//		System.out.println(text + " is clicked.");
		if (showingSubmenu) {
			hideMenu();
		} else {
			showMenu();
		}
//		if (isToggle) {
//			isSelected = !isSelected;
//			parent.setSelected(PMenuItem_Cascade.this);
//		}
	}
	public void showMenu(){
		double x,y;
//		x = getOffset().getX()+parent.getOffset().getX();
		x = 0;
		y = getHeight();
		for(PMenuItem_Abstract pm: items){
			addChild(pm);
			pm.setOffset(x,y);
			if (isHorizontal){
				x += pm.getWidth();
			} else {
				y += pm.getHeight();
			}
		}
		showingSubmenu = true;
	}
	public void hideMenu(){
		for(PMenuItem_Abstract pm: items){
			pm.removeFromParent();
		}
		showingSubmenu = false;
	}
	
	public void setSelected(PMenuItem_Abstract childMenu) {
		for (PMenuItem_Abstract pm : items) {
			if (pm.isToggle) {
				if (pm == childMenu) {
					pm.setSelected(true);
				} else {
					pm.setSelected(false);
				}
			}
		}
		selected = childMenu;
	}
		
}
