package m127.smd2e.frontend;

import java.awt.event.ActionListener;

import javax.swing.JMenu;
import javax.swing.JMenuItem;

import m127.smd2e.Main;
import m127.smd2e.backend.Block;
import m127.smd2e.backend.util.BlockGroup;

public class BlockChooserMenuConstructor {
	
	public static JMenu construct(BlockGroup g, ActionListener actionListener, boolean hasEmpty) {
		JMenu ret=new JMenu(g.name==null?"Blocks":g.name);
		if(hasEmpty) {
			JMenuItem j=new JMenuItem("No block");
			j.addActionListener(actionListener);
			j.setActionCommand("0");
			ret.add(j);
		}
		ret.addSeparator();
		for(BlockGroup gr:g.children) {
			ret.add(construct(gr,actionListener,false));
		}
		ret.addSeparator();
		JMenuItem j;
		Main m=Main.getInstance();
		for(Block b:g.blocks) {
			j=new JMenuItem("["+b.id+"] "+b.name,m.iregister.getImage(b.iid));
			j.setActionCommand(Integer.toString(b.id));
			j.addActionListener(actionListener);
			ret.add(j);
		}
		return ret;
	}
}
