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

import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.util.Vector;

import javax.imageio.ImageIO;
import javax.swing.*;


import WheelOfFortune.game.player.*;
import WheelOfFortune.game.GameModel;
import WheelOfFortune.game.userInput.*;
/**
 * @author warren
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class PlayerPanel extends JPanel{
	private GameModel gm;
	private Image background,chars[];
	private JToolBar nbar, s1bar, s2bar;
	private JTextField[] tf;
	private JTextField[] name;
	private Player[] p;
	
	private final String PATH = ".\\WheelOfFortune\\game\\userOutput\\skin\\";
	private final String DEFAULT = "default\\";
	private String skinPath = "default\\";
	private String newSkinPath = "default\\";
	private final String CHARPATH = ".\\WheelOfFortune\\game\\userOutput\\characters\\";
	public PlayerPanel(Player[] p)
	{		
		this.setBackground(Color.lightGray);
		this.p = p;
		setup();
		try{
			chars = new Image[3];
			for(int i =0; i < 3; i++)
			{
				if(p[i].getDifficulty()!= 0)
					chars[i] = ImageIO.read(new File(CHARPATH+
							p[i].getImagePath()));
			}
		}
		catch (IOException e){
			
		}
		
		
		nbar = new JToolBar();
		nbar.setFloatable(false);
		this.setLayout(new BorderLayout());
		add(nbar, BorderLayout.NORTH);
		JPanel south = new JPanel();
		s1bar = new JToolBar();
		s1bar.setFloatable(false);
		s2bar = new JToolBar();
		s2bar.setFloatable(false);

		
		tf = new JTextField[3];
		name = new JTextField[3]; 
		Box score = Box.createHorizontalBox();
		Box nameBox = Box.createHorizontalBox();
		for(int j = 0; j < 3; j++)
		{
			name[j] = new JTextField(p[j].getName());
			name[j].setPreferredSize(new Dimension(this.getWidth()/3, 20));
			name[j].setHorizontalAlignment(JTextField.CENTER);
			name[j].setBackground(Color.white);
			name[j].setForeground(Color.black);
			name[j].setEditable(false);
			nameBox.add(name[j]);
			nameBox.add(Box.createGlue());
			tf[j] = new JTextField("0");
			tf[j].setPreferredSize(new Dimension(this.getWidth()/3, 20));
			tf[j].setBackground(Color.white);
			tf[j].setForeground(Color.black);
			tf[j].setEditable(false);
			tf[j].setHorizontalAlignment(JTextField.CENTER);
			score.add(tf[j]);
			score.add(Box.createGlue());
		}
		JPanel temp = new JPanel();
		temp.setLayout(new BorderLayout());
		temp.add(nameBox, BorderLayout.NORTH);
		temp.add(score, BorderLayout.CENTER);
		
		south.setLayout(new BorderLayout());
		south.add(temp, BorderLayout.NORTH);
		south.add(s1bar, BorderLayout.CENTER);
		south.add(s2bar, BorderLayout.SOUTH);
		add(south,BorderLayout.SOUTH);
		
	}
	
	public void setup()
	{
		try {
			background = ImageIO.read(new File(PATH + skinPath+ "playerbg.jpg"));

				
		}
		catch (IOException e) {
			try{
			skinPath = DEFAULT;
			background = ImageIO.read(new File(PATH + skinPath+ "playerbg.jpg"));
			skinPath = newSkinPath;
			}
			catch(Exception ex){}
		}		
	}
	public void changeSkin(String s)
	{
		skinPath = s;
		newSkinPath = s;
		setup();
	}

	public void paintComponent(Graphics g)
	{
		super.paintComponent(g);
		if(background == null)
			return;
		else
		{
			int imageWidth = background.getWidth(this);
			int imageHeight = background.getHeight(this); 
			g.drawImage(background, (getWidth() - imageWidth) / 2, 
					(getHeight() - imageHeight) / 2, null);
			for(int i = 0; i < 3; i++)
			{
				if(p[i].getDifficulty() != 0)
					g.drawImage(chars[i], (i * (getWidth() - 30)/3 + 10*i+10),
							(getHeight()-100 - chars[i].getHeight(this)), null);
			}
		}
	}
	
	public void updatePanel(GameModel gm, GameInput gi)
	{
		for(int i = 0; i < 3; i++)
		{
			if(gm.getPlayer(i) != null)
				tf[i].setText(""+gm.getPlayer(i).getRoundMoney());
		}
		
		for(int j = 0; j < 3; j++)
			tf[j].setBorder(BorderFactory.createEtchedBorder());
		tf[gm.getActivePlayerIndex()].setBorder(
				BorderFactory.createLineBorder(Color.red));
		if(!gm.activePlayer().isCPU())
		{
			if(gm.getState()==GameModel.WAITING_FOR_DECISION)
			{
				if(gm.getBoard().getConsonantsLeft().length()!=0)
					gi.getSpinAction().setEnabled(true);
				if(gm.activePlayer().getRoundMoney()
						>= GameModel.VOWEL_PRICE &&
						gm.getBoard().getVowelsLeft().length()!=0)
					gi.getBuyAction().setEnabled(true);
				gi.getSolveAction().setEnabled(true);
			}
			else if(gm.getState()==GameModel.WAITING_FOR_CONSONANT)
			{
				Vector v = gi.getConsonantAction();
				if(!gm.getHideUsedLetters())
				{			
					for(int i = 0; i < v.size(); i ++)
						((LetterAction)v.get(i)).setEnabled(true);
				}
				else
				{
					for(int i = 0; i < v.size(); i++)
					{
						
						if(gm.getBoard().getConsonantsLeft().indexOf(
								((LetterAction)v.get(i)).getName().trim()) > -1)
							((LetterAction)v.get(i)).setEnabled(true);
					}
				}
			}
			
			else if (gm.getState()==GameModel.WAITING_FOR_VOWEL)
			{
				Vector v = gi.getVowelAction();
				if(!gm.getHideUsedLetters())
				{
					for(int i = 0; i < v.size(); i ++)
						((LetterAction)v.get(i)).setEnabled(true);
				}
				else
				{
					for(int i = 0; i < v.size(); i++)
					{
						if(gm.getBoard().getVowelsLeft().indexOf(
								((LetterAction)v.get(i)).getName().trim()) > -1)
							((LetterAction)v.get(i)).setEnabled(true);
					}
				}
			}
			else
			{
				for(int j = 0; j < 3; j++)
				{
					if(gm.getPlayer(j)!=null)
						tf[j].setText(""+gm.getPlayer(j).getTotalMoney());
				}
			}
		}
		repaint();
		
	}
	/**
	 * @return Returns the nbar.
	 */
	public JToolBar getNbar() {
		return nbar;
	}
	/**
	 * @return Returns the sbar.
	 */
	public JToolBar getS1bar() {
		return s1bar;
	}
	
	public JToolBar getS2bar()
	{
		return s2bar;
	}
	
	public void setupKeyMap(AbstractAction a, String key)
	{
		InputMap imap = this.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
		ActionMap amap = this.getActionMap();
		if(a instanceof DecisionAction)
		{
			imap.put(KeyStroke.getKeyStroke("ctrl "+key), "panel."+key);
			amap.put("panel."+key, (DecisionAction)a);
		}
		else
		{
			imap.put(KeyStroke.getKeyStroke(key), "panel."+key+"let");
			amap.put("panel."+key+"let", (LetterAction)a);
		}
	}
}
