/*
 * 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.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.io.File;
import java.io.IOException;
import java.util.StringTokenizer;

import javax.imageio.ImageIO;
import javax.swing.JLabel;
import javax.swing.JPanel;

import WheelOfFortune.game.GameModel;

/**
 * @author warren
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class BoardPanel extends JPanel{
	private GameModel gm;
	private Image background;
	private Image[][] letter;
	private int[] spacebefore, spaceafter, words, count;
	private JLabel category;
	private StringBuffer sb;
	
	private final int ROWMAX = 4;
	private final int COLMAX = 12;
	private final String PATH = ".\\WheelOfFortune\\game\\userOutput\\skin\\";
	private final String DEFAULT = "default\\";
	private String skinPath = "default\\";
	private String newSkinPath = "default\\";
	int i,j,location;
	public BoardPanel()
	{
		letter = new Image[ROWMAX][COLMAX];		
		setup();
		setLayout(null);
		category = new JLabel();
		category.setFont(new Font(null, Font.PLAIN, 20));
		category.setForeground(Color.DARK_GRAY);
		add(category);
		
		
		
	}
	public void setup()
	{
		try {
			
			background = ImageIO.read(new File(PATH + skinPath + "boardbg.jpg"));

		}
		catch (IOException e) {
			try{
				background = ImageIO.read(new File(PATH + DEFAULT + "boardbg.jpg"));
			}
			catch(Exception ex){}
		}
		for(int j = 0; j < COLMAX; j++)
			for(int i = 0; i<ROWMAX; i++)
			{
				try{
					letter[i][j] = ImageIO.read(new File(PATH + skinPath + "blank.jpg"));
				}
				catch(IOException e){
					try{
						letter[i][j] = ImageIO.read(new File(PATH + DEFAULT + "blank.jpg"));
					}
					catch(Exception ex){}
				}
			}

		skinPath = newSkinPath;
	}
	public void changeSkin(String s)
	{
		skinPath = s;
		newSkinPath = s;
		setup();
		location = 0;
		makeBoard();
	}
	public void paintComponent(Graphics g)
	{
		super.paintComponent(g);
		if(background == null)
			return;
		else
		{
			int backWidth = background.getWidth(this);
			int backHeight = background.getHeight(this);
			
			g.drawImage(background, (getWidth() - backWidth) / 2, 
					(getHeight() - backHeight) / 2, null);
			
			for(int i = 0; i<ROWMAX;i++)
				for(int j = 0; j < COLMAX; j++)
					g.drawImage(letter[i][j], j*30 + 10 + 1*j, 
							(getHeight() * 2/5) + i*45 + 10 + 1*i,null);
		}
	}
	
	public void setup(GameModel gm)
	{
		String puzzle = gm.getBoard().display();
		StringTokenizer st = new StringTokenizer(puzzle," ,");
		String temp;
		String cat = "Category: " + gm.getBoard().getCategory();
		category.setText(cat);
		int textLength = cat.length()*5;
		category.setHorizontalAlignment(JLabel.CENTER);
		category.setBounds(0,100,396,30);
		count = new int[ROWMAX];
		int currow = 0;
		words = new int[ROWMAX];
		int i, j, t1,t2;
		for(i = 0 ; i < ROWMAX; i++)
		{
			count[i]=words[i]=0;
		}
		if(st.hasMoreTokens())
		{
			temp = st.nextToken();
			count[currow]+= temp.length();
			words[currow]++;
		}
		
		while(st.hasMoreTokens())
		{
			temp = st.nextToken();
			while(st.hasMoreTokens() && count[currow] + temp.length() + 1<=12)
			{
				count[currow] += temp.length();
				count[currow]++;
				words[currow]++;
				temp = st.nextToken();
			}
			currow++;
			count[currow]+= temp.length();
			words[currow]++;
			
		}
		if(count[ROWMAX-1] == 0 && count[ROWMAX-2] == 0)
		{
			int min = ROWMAX-1;
			while(min>0)
			{
				count[min] = count[min-1];
				words[min] = words[min-1];
				min--;
			}
			count[0] = words[0] = 0;
		}
		sb = new StringBuffer();
		currow = 0;
		st = new StringTokenizer(puzzle," ,");
		spacebefore = new int[4];
		spaceafter = new int[4];
		for(currow = 0; currow < ROWMAX; currow++)
		{
			spacebefore[currow] = (COLMAX-(count[currow]))/2;
			for(i=0; i<spacebefore[currow]; i++)
				sb.append(" ");
			t1 = 0;
			if(words[currow] > 0)
			{	
				sb.append(st.nextToken());
				t1++;
			}
			for(j=t1; j<words[currow]; j++)
				sb.append(" " + st.nextToken());
			spaceafter[currow] = (COLMAX-(spacebefore[currow] + count[currow]));
			for(i=0; i< spaceafter[currow]; i++)
				sb.append(" ");
			
		}
		location = 0;
		makeBoard();
	}
	public void makeBoard()
	{
		for(i = 0; i<ROWMAX; i++)
			for(j = 0; j < COLMAX; j++)
			{
				try {

					if(sb.charAt(location)=='!')
						letter[i][j] = ImageIO.read(new File(PATH +skinPath+ "blank.jpg"));
					else if(sb.charAt(location) == ' ')
						letter[i][j] = ImageIO.read(new File(PATH +skinPath+ "empty.jpg"));
					else
						letter[i][j] = ImageIO.read(
								new File(PATH +skinPath+ sb.charAt(location) + ".jpg"));
					location++;
				}
				catch (IOException e) {
					try{
						if(sb.charAt(location)=='!')
							letter[i][j] = ImageIO.read(new File(PATH + DEFAULT + "blank.jpg"));
						else if(sb.charAt(location) == ' ')
							letter[i][j] = ImageIO.read(new File(PATH + DEFAULT + "empty.jpg"));
						else
							letter[i][j] = ImageIO.read(
									new File(PATH + DEFAULT + sb.charAt(location) + ".jpg"));
						location++;
					}
					catch(IOException ex)
					{
						
					}
				}
			}
	}
	public void updatePanel(GameModel gm)
	{
		int h, i, j,t1;
		StringTokenizer st = new StringTokenizer(gm.getBoard().display(), " ,");
		String temp;
		sb = new StringBuffer();
		for(h = 0; h < ROWMAX; h++)
		{
			for(i=0; i<spacebefore[h]; i++)
				sb.append(" ");
			t1 =0;
			if(words[h] > 0)
			{	
				sb.append(st.nextToken());
				t1++;
			}
			for(j=t1; j<words[h]; j++)
				sb.append(" " + st.nextToken());
			for(i=0; i< spaceafter[h]; i++)
				sb.append(" ");
		}
		location = 0;
		
		
		makeBoard();
		repaint();
		
	}
	
}
