/*
 * 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.Graphics2D;
import java.awt.Image;
import java.io.File;
import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;

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

import WheelOfFortune.game.GameModel;
import WheelOfFortune.game.userInput.GameInput;
import WheelOfFortune.game.wheel.Space;

/**
 * @author warren
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class WheelPanel extends JPanel{
	
	private GameInput gi;
	private Image background;
	private final String PATH = ".\\WheelOfFortune\\game\\userOutput\\skin\\";
	private final String DEFAULT = "default\\";
	private String skinPath = "default\\";
	private String newSkinPath = "default\\";
	double theta=0.0;
	int spinTarget;
	Color colorSet[];
	String values[];
	int width = 340;
	int height = 340;
	int startAngle=-15;
	int arcAngle=30;
	
	boolean spinning;
	/**
	 * 
	 */
	public WheelPanel(){
		this.setBackground(Color.lightGray);
		
		setup();
		colorSet = new Color[12];
		colorSet[0] = Color.BLUE; colorSet[1] = Color.CYAN; colorSet[2] = Color.GRAY;
		colorSet[3] = Color.GREEN; colorSet[4] = Color.MAGENTA; colorSet[5] = Color.ORANGE;
		colorSet[6] = Color.CYAN; colorSet[7] = Color.PINK; colorSet[8] = Color.YELLOW;
		colorSet[9] = Color.GREEN;	colorSet[10] = Color.RED; colorSet[11] = Color.LIGHT_GRAY;
		
		int i = 0;
		
		values = new String [12];
		spinning = false;
	}
	public void setup()
	{
		try {
			background = ImageIO.read(new File(PATH + skinPath + "wheelbg.jpg"));
			
		}
		catch (IOException e) {
			try{
				background = ImageIO.read(new File(PATH + DEFAULT + "wheelbg.jpg"));
			}
			catch(Exception ex){}
		}
	}
	public void setGameInput(GameInput gi)
	{
		this.gi = gi;
		for(int i = 0; i < 12; i++)
		{
			Space s = gi.getGameModel().getWheel().getSpaceAt(i);
			values[11-i] = s.getName();
			if(s.getCode() == Space.LOSE_TURN)
				colorSet[11-i] = Color.WHITE;
			else if(s.getCode() == Space.BANKRUPT)
				colorSet[11-i] = Color.BLACK;
		}
	}
	
	public void changeSkin(String s)
	{
		skinPath = s;
		newSkinPath = s;
		setup();
	}
	interface TimerListener
	{
		void timeElapsed(Timer t);
	}
	
	/*	class Timer extends Thread
	 {
	 public Timer(double i,TimerListener t)
	 {
	 target = t;
	 interval = i;
	 setDaemon(true);
	 
	 }
	 
	 public void run()
	 {
	 try{
	 while(spinTarget>0)
	 {
	 if(spinTarget > 30)
	 interval = interval + .05;
	 else
	 interval = interval + .1;
	 sleep((int)interval);
	 target.timeElapsed(this);
	 spinTarget--;
	 }
	 
	 }catch(InterruptedException e){}
	 gi.getGameModel().updateOutput();
	 }
	 
	 private TimerListener target;
	 private double interval;
	 
	 }
	 */
	int delay = 2;
	boolean shown;
	public void spinWheel(int targ)
	{
		targ = targ*6;
		// make it targ *= (72/numSpaces) in the future
		spinTarget = targ;
		delay = 2;
		spinning = true;
		spinWheelHelp();
		while(spinning == true) {
			try{Thread.sleep(40);}catch(InterruptedException ie) {}
		}
		
		
	}
	
	public void spinWheelHelp()
	{
		if(spinTarget>72)
		{
			Timer t= new Timer();
			t.schedule(new Test(), delay);
		}
		else if(spinTarget>24)
		{
			Timer t= new Timer();
			t.schedule(new Test(), delay=(int)(delay*1.02)+1);
		}
		else if(spinTarget>1)
		{
			Timer t= new Timer();
			t.schedule(new Test(), delay=(int)(delay*1.05)+1);
			shown = false;
		}
		else if(spinTarget>0)
		{
			Timer t= new Timer();
			t.schedule(new Test(), delay=(int)(delay*1.05)+1);
			
			
			/*if(shown == false)
			 {
			 gi.getGameModel().getOut().print("The wheel lands on: ");	
			 gi.getGameModel().getOut().println(
			 gi.getGameModel().getWheel().getCurrentSpace().getName());
			 gi.getGameModel().updateOutput();
			 shown = true;
			 }*/
		}	
	}
	public class Test extends TimerTask
	{
		public void run()
		{
			repaint();
			spinWheelHelp();
		}
	}
	
	public void paintComponent(Graphics g1)
	{
		super.paintComponent(g1);
		
		Graphics2D g = (Graphics2D) g1;
		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);
		}
		
		
		if(spinTarget >0)
		{			
			theta+=(Math.PI/36);
			--spinTarget;
		}
		if(spinTarget == 0) spinning = false;
		int x = 200,y=250;
		g.translate(x,y);
		g.rotate(theta);
		for(int i = 0; i < 12; i++)
		{
			g.setColor(colorSet[i]);
			
			g.fillArc(-width/2,-height/2,width,height,startAngle,arcAngle);
			g.setColor(Color.black);
			
			if(colorSet[i].equals(Color.BLACK))
				g.setColor(Color.WHITE);
			else
				g.setColor(Color.BLACK);
			g.setFont(new Font("San Serif",Font.PLAIN,20));
			
			if(values[i].length() <= 5)
				g.drawString(values[i],100,10);
			else
				g.drawString(values[i],50,10);
			g.rotate(Math.PI/6);
			
		}
		
		
	}
	public void update(GameModel gm)
	{
		
	}
}
