/*
 * Created on Nov 11, 2004
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package WheelOfFortune.game.userInput;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FilenameFilter;
import java.util.Vector;

import javax.swing.Action;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JCheckBox;
import javax.swing.JMenu;
import javax.swing.JRadioButton;

import WheelOfFortune.game.GameModel;
import WheelOfFortune.game.userOutput.MusicThread;
/**
 * @author caih
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class GameInput {

	public static final String SPIN = "Spin Wheel";
	public static final String BUY = "Buy Vowel";
	public static final String SOLVE = "Solve Puzzle";
	DecisionAction spinAction;
	DecisionAction buyAction;
	DecisionAction solveAction;
	DecisionAction playAction, stopAction, nextAction;
	ImageIcon play, stop,next;
	Vector alphabetAction = new Vector();
	GameModel gm;
	GameInput gi;
	String[] skinList;
	JCheckBox sound;
	JMenu decisionMenu;
	JMenu musicMenu;
	JMenu optionMenu;
	JMenu skinMenu;
	JRadioButton[] skins;
	ButtonGroup skinGroup;
	private final String PATH = ".\\WheelOfFortune\\game\\userOutput\\skin\\";
	private String skinPath = "default\\";
	MusicThread music;
	/**
	 * 
	 */
	public GameInput(GameModel gm, MusicThread music) {
		this.gm = gm;
		this.gi = this;
		this.music = music;
		gm.getOut().getWP().setGameInput(this);
		spinAction = new DecisionAction(SPIN,
					"Click on this to spin the wheel",
					gm, this);
		buyAction = new DecisionAction(BUY,
					"Click on this to buy a vowel",
					gm, this);
		solveAction = new DecisionAction(SOLVE,
					"Click on this to solve the puzzle",
					gm, this);
		play = 	new ImageIcon(PATH+skinPath+"play.gif");
		playAction = new DecisionAction("Play",
					play,
					"Click on this to play music",
					gm, this);
		stop = 	new ImageIcon(PATH+skinPath+"stop.gif");
		stopAction = new DecisionAction("Stop",
				stop,
				"Click on this to stop the music",
				gm, this);
		next = new ImageIcon(PATH+skinPath+"next.gif");
		nextAction = new DecisionAction("Next",
				next,
				"Click on this to go to the next song",
				gm, this);
		
		spinAction.putValue(Action.MNEMONIC_KEY, new Integer('S'));
		buyAction.putValue(Action.MNEMONIC_KEY, new Integer('B'));
		solveAction.putValue(Action.MNEMONIC_KEY, new Integer('P'));
		LetterAction la;	
		for(int i = 0; i < 15; i++)
		{
			la = new LetterAction(" "+(char)('A'+i)+" ", gm, this);
			alphabetAction.add(la);
			gm.getOut().getPP().getS1bar().add(la);
			gm.getOut().getPP().setupKeyMap(la, (char)('A'+i)+"");
		}
		for(int j = 15; j < 26; j++)
		{
			la = new LetterAction(" "+(char)('A'+j)+" ", gm, this);
			alphabetAction.add(la);
			gm.getOut().getPP().getS2bar().add(la);
			gm.getOut().getPP().setupKeyMap(la, (char)('A'+j)+"");
		}
		gm.getOut().getPP().getNbar().add(spinAction);
		gm.getOut().getPP().getNbar().add(buyAction);
		gm.getOut().getPP().getNbar().add(solveAction);
		gm.getOut().getPP().getNbar().add(playAction);
		gm.getOut().getPP().getNbar().add(stopAction);
		gm.getOut().getPP().getNbar().add(nextAction);
		
		gm.getOut().getPP().setupKeyMap(spinAction, "S");
		gm.getOut().getPP().setupKeyMap(buyAction, "B");
		gm.getOut().getPP().setupKeyMap(solveAction, "P");
		
		
		decisionMenu = new JMenu("Decision");
		decisionMenu.setMnemonic('D');
		decisionMenu.add(spinAction);
		decisionMenu.add(buyAction);
		decisionMenu.add(solveAction);
		
		musicMenu = new JMenu("Sound");
		musicMenu.setMnemonic('S');
		musicMenu.add(playAction);
		musicMenu.add(stopAction);
		musicMenu.add(nextAction);
		musicMenu.addSeparator();
			
		sound = new JCheckBox("Sound Effects");
		sound.setSelected(true);
		musicMenu.add(sound);
		sound.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent e)
				{
						gi.gm.setSound(sound.isSelected());
				}
		});
	
		optionMenu = new JMenu("Options");
		optionMenu.setMnemonic('O');
		skinMenu = new JMenu("Skins");
		skinMenu.setMnemonic('k');
		optionMenu.add(skinMenu);
		
		File f = new File(PATH);
		skinList = f.list((new FilenameFilter()
				{
			public boolean accept(File f, String s)
			{
				if(s.indexOf(".")>=0)
					return false;
				else
					return true;
			}
		}));
		skinGroup = new ButtonGroup();
		skins = new JRadioButton[skinList.length];
		RadioListener rl = new RadioListener();
		for(int i = 0; i < skins.length; i++)
		{
			if(skinList[i].equalsIgnoreCase("default"))
				skins[i] = new JRadioButton(skinList[i],true);
			else
				skins[i] = new JRadioButton(skinList[i],false);
			skinGroup.add(skins[i]);
			skinMenu.add(skins[i]);
			skins[i].addActionListener(rl);
		}
		
		
		gm.getOut().getMb().add(decisionMenu);
		gm.getOut().getMb().add(musicMenu);
		gm.getOut().getMb().add(optionMenu);
		
		
		disableActions();
	}
	private class RadioListener implements ActionListener
	{
		public void actionPerformed(ActionEvent e)
		{
			JRadioButton s = (JRadioButton)e.getSource();
			gm.getOut().changeSkin(s.getLabel());
			gm.updateOutput();
		}
	}
	public GameModel getGameModel()
	{
		return gm;
	}
	public void disableActions()
	{
		spinAction.setEnabled(false);
		buyAction.setEnabled(false);
		solveAction.setEnabled(false);
		LetterAction temp;
		for(int i = 0; i < alphabetAction.size(); i++)
		{
			temp = (LetterAction)(alphabetAction.get(i));
			temp.setEnabled(false);
			
		}
	}

	/**
	 * @return Returns the alphabetAction.
	 */
	public Vector getAlphabetAction() {
		return alphabetAction;
	}
	/**
	 * @return Returns the buyAction.
	 */
	public DecisionAction getBuyAction() {
		return buyAction;
	}
	
	public Vector getConsonantAction()
	{
		Vector v = new Vector();
		String s = "AEIOU";   			// dont count vowels... this is correct!!!
		LetterAction la;
		boolean add;
		for(int i = 0; i < alphabetAction.size(); i++)
		{
			add = true;
			la = ((LetterAction)(alphabetAction.get(i)));
			for(int j = 0; j < s.length(); j++)
			{
				if(la.getName().equalsIgnoreCase(s.charAt(j)+""))
					add = false;
			}
			if(add) v.add(la);
		}
		return v;
		
	}
	
	public Vector getVowelAction()
	{
		Vector v = new Vector();
		String s = "AEIOU";
		LetterAction la;
		boolean add;
		for(int i = 0; i < alphabetAction.size(); i++)
		{
			add = false;
			la = ((LetterAction)(alphabetAction.get(i)));
			for(int j = 0; j < s.length(); j++)
			{
				if(la.getName().equalsIgnoreCase(s.charAt(j)+""))
					add = true;
			}
			if(add) v.add(la);
		}
		return v;
		
	}
	
	/**
	 * @return Returns the solveAction.
	 */
	public DecisionAction getSolveAction() {
		return solveAction;
	}
	/**
	 * @return Returns the spinAction.
	 */
	public DecisionAction getSpinAction() {
		return spinAction;
	}
}
