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

import java.awt.*;
import java.io.*;
import javax.imageio.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;
import WheelOfFortune.game.player.*;

public class StartGamePanel extends JPanel
{
	private OptionFrame of;
	
	private MainMenu mm;
	private StartGamePanel sgp; 
	private final String PATH = ".\\WheelOfFortune\\game\\userOutput\\characters\\";
	private JComboBox rounds;
	private CharacterPanel[] cp;
	private Player[] p;
	public CharacterPanel getP(int i)
	{
		return cp[i];
	}
	public int getRounds()
	{
		return rounds.getSelectedIndex()+1;
	}
	public StartGamePanel(OptionModel om)
	{
		this.p = om.getP();
		cp = new CharacterPanel[3];
		JPanel top = new JPanel();
		top.setPreferredSize(new Dimension(600,30));
		top.add(new JLabel("Number of Rounds:"));
		rounds = new JComboBox();
		rounds.addItem("1");
		rounds.addItem("2");
		rounds.addItem("3");
		if(om.getNumRounds() > 0)
			rounds.setSelectedIndex(om.getNumRounds()-1);
		else
			rounds.setSelectedIndex(0);
		top.add(rounds);
		add(top);
		for(int i = 0; i < 3; i ++)
		{
			cp[i] = new CharacterPanel(p[i]);
			add(cp[i]);
		}
	}
	public class CharacterPanel extends JPanel
	{
		private JComboBox type, difficulty;
		private ImagePanel ip;
		private JTextField charName;
		private JSlider charSlider;
		private int[] playerDiff;
		private String[] chars;
		private int current;
		
		public CharacterPanel(Player p)
		{
			this.setPreferredSize(new Dimension(190,270));
			this.setLayout(new BorderLayout());
			
			type = new JComboBox();
			type.setEditable(false);
			type.addItem("None");
			type.addItem("Human Player");
			type.addItem("CPU Player");
			difficulty = new JComboBox();
			difficulty.setEditable(false);
			difficulty.addItem("Easy");
			difficulty.addItem("Normal");
			difficulty.addItem("Hard");
			difficulty.setVisible(false);
			ip = new ImagePanel();
			charName = new JTextField(10);
			charName.setHorizontalAlignment(JTextField.CENTER);
			charSlider = new JSlider(0,ip.getLength()-1,0);
			charSlider.setMajorTickSpacing(1);
			charSlider.setSnapToTicks(true);
			charSlider.setPaintTicks(true);
			charSlider.setEnabled(false);
			charSlider.addChangeListener(new CharSliderListener());
			

			JPanel top = new JPanel();
			top.add(type);
			top.add(difficulty);
			add(top,BorderLayout.NORTH);
			add(ip,BorderLayout.CENTER);
			JPanel bottom = new JPanel();
			bottom.setLayout(new BorderLayout());
			bottom.add(charName, BorderLayout.NORTH);
			bottom.add(charSlider, BorderLayout.SOUTH);
			add(bottom,BorderLayout.SOUTH);
			
			if(p != null && p.getDifficulty()!=0)
			{
				current = p.getCharacter();
				if(p.getDifficulty()>0)
				{
					type.setSelectedIndex(2);
					difficulty.setVisible(true);
					difficulty.setSelectedIndex(p.getDifficulty()-1);
					ip.setEnabled(true);			
					charSlider.setEnabled(true);
					charName.setEnabled(false);
					ip.showImage();
				}
				else
				{
					type.setSelectedIndex(1);
					difficulty.setVisible(false);
					charName.setEnabled(true);
					ip.setEnabled(true);
					charSlider.setEnabled(true);
					ip.showImage();
				}
				charSlider.setValue(p.getCharacter());
				repaint();
			}
			else
			{
				difficulty.setVisible(false);
				ip.setNull();
				ip.setEnabled(false);
				charName.setText("");
				charName.setEnabled(false);
				charSlider.setEnabled(false);
			}
			type.addActionListener(new ActionListener()
					{
						public void actionPerformed(ActionEvent e)
						{
							if(((String)(type.getSelectedItem())).equalsIgnoreCase("CPU Player"))
							{
								difficulty.setVisible(true);
								charName.setEnabled(false);
								ip.setEnabled(true);
								
								charSlider.setEnabled(true);
								ip.showImage();
							}
							else if(((String)(type.getSelectedItem())).equalsIgnoreCase("None"))
							{
								difficulty.setVisible(false);
								ip.setNull();
								ip.setEnabled(false);
								charName.setEnabled(false);
								charSlider.setEnabled(false);
								
							}
							else
							{
								difficulty.setVisible(false);
								charName.setEnabled(true);
								ip.setEnabled(true);
								
								charSlider.setEnabled(true);
								ip.showImage();
							}
							
						}
					});
		}
		//returns player difficulty
		public int getPlayerDiff()
		{
			int playerDiff;
			if(((String)(type.getSelectedItem())).equalsIgnoreCase("Human Player"))
				playerDiff = -1;
			else if(((String)(type.getSelectedItem())).equalsIgnoreCase("None"))
				playerDiff = 0;
			else if(((String)(difficulty.getSelectedItem())).equalsIgnoreCase("Easy"))
				playerDiff = 1;
			else if(((String)(difficulty.getSelectedItem())).equalsIgnoreCase("Normal"))
				playerDiff = 2;
			else
				playerDiff = 3;
			return playerDiff;
		}
		public String getName()
		{
			return charName.getText();
		}
		public int getPlayerChar()
		{
			if((((String)(type.getSelectedItem())).equalsIgnoreCase("None")))
					return -1;
			return charSlider.getValue();
		}
		public String getCharPath()
		{
			return chars[current];
		}
		
		private class CharSliderListener implements ChangeListener
		{
			public void stateChanged(ChangeEvent e)
			{
				ip.setCurrent(
						((JSlider)(e.getSource())).getValue());
				ip.showImage();
			}
		}
		private class ImagePanel extends JPanel
		{
			private Image charImage;	
			private boolean changed;
			
			public ImagePanel()
			{
				this.setPreferredSize(new Dimension(100,120));
				this.setBorder(BorderFactory.createLoweredBevelBorder());
				current = 0;
				changed = false;
				File f = new File(PATH);
				chars = f.list(new FilenameFilter()
						{
							public boolean accept(File f, String s)
							{
								if(s.endsWith(".gif"))
									return true;
								else
									return false;
							}
						});
				try{
					charImage = null;
					repaint();
				}
				catch(Exception e)
				{
					//mm.setVisible(true);
					//of.setVisible(false);
					e.printStackTrace();
				}
			}
			public void setNull()
			{
				charImage = null;
				repaint();
			}
			public int getCurrent()
			{
				return current;
			}
			public void setCurrent(int i)
			{
				current = i;
			}
			public void paintComponent(Graphics g)
			{
				super.paintComponent(g);
				if(charImage == null)
					return;
				else
				{
					int imageWidth = charImage.getWidth(this);
					int imageHeight = charImage.getHeight(this); 
					g.drawImage(charImage, (getWidth() - imageWidth) / 2, 
							(getHeight() - imageHeight) / 2, null);
				}
			}
			
			
			public void showImage()
			{
				try{
					charImage = ImageIO.read(new File
						(PATH+chars[current]));
					if(type.getSelectedIndex()!=1 || changed == false)
					{
						charName.setText(ip.getName(current));
						changed = true;
					}
					repaint();
				}
				catch(Exception e)
				{
					//mm.setVisible(true);
					//of.setVisible(false);
					e.printStackTrace();
				}
			}
			
			public String getName(int index)
			{
				String s = chars[index].substring(0,
						chars[index].length()-4);
				return s;
			}
			public int getLength()
			{
				return chars.length;
			}
		}
	}
}