import java.util.*;

import edu.neu.ccs.util.JPTUtilities;
import edu.neu.ccs.util.ProbStatTools;
import edu.neu.ccs.*;
import edu.neu.ccs.gui.*;
import edu.neu.ccs.codec.*;
import edu.neu.ccs.console.*;
import edu.neu.ccs.filter.*;
import edu.neu.ccs.jpf.*;
import edu.neu.ccs.parser.*;
import edu.neu.ccs.pedagogy.*;
import edu.neu.ccs.quick.*;
import edu.neu.ccs.util.*;

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.awt.font.*;
import java.awt.image.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.*;
import java.io.*;
import java.util.*;
import java.math.*;
import java.beans.*;
import java.lang.reflect.*;
import java.lang.Object.*;
import java.net.URL;
import java.util.regex.*;
import java.text.ParseException;

public class Player extends DisplayPanel{
	
	//Is this player a dealer?
	boolean dealer;
	//Where on table;
	int pos;
	int x;
	int y;
	//the player's hand
	ArrayList<Card> hand;
	//the total value of the player's cards
	int total;
	//has the player gone
	boolean stood;
	//how much money player has
	public int balance;
	//how much player is betting
	int bet;
	//has busted?
	boolean busted;
	//players chip
	Paintable chip;
	Paintable chipAmt;
	Paintable balanceView;
	
	public TextPaintable win = new TextPaintable("You Win!", new Font("Arial",3,38), Colors.darkgreen);
	public TextPaintable winBack = new TextPaintable("You Win!", new Font("Arial",3,40), Colors.limegreen);
	
	public TextPaintable loose = new TextPaintable("You Loose!", new Font("Arial",3,38), Colors.red);
	public TextPaintable looseBack = new TextPaintable("You Loose!", new Font("Arial",3,40), Colors.darkred);

	public Player(boolean isDealer, int pos, int balance) {
		this.dealer = isDealer;
		
		if (pos==0) {this.x=339; this.y=25;}
		if (pos==1) {this.x=180; this.y=450;}
		if (pos==2) {this.x=550; this.y=450;}
		if (pos==3) {this.x=100; this.y=300;}
		if (pos==4) {this.x=600; this.y=300;}
		
		this.hand = new ArrayList<Card>();
		this.total = 0;
		this.stood = false;
		this.balance = balance;
		this.busted = false;
		this.bet=0;
		this.balanceView = new TextPaintable(Integer.toString(this.balance));
	}
	
	public int getBalance() {
		return this.balance;
	}
	
	public void changeBalance(int amt) {
		this.balance = this.balance+amt;
	}
	
	private void createChip() {
		XCircle circle = new XCircle(20);

	    Color fill = Colors.gold;
	    Color draw = Colors.red;
	    BasicStroke stroke = new BasicStroke(4);
	        
	    chip = new ShapePaintable
	       (circle, PaintMode.FILL_DRAW, fill, draw, stroke);
	}
	
	public void bet(PaintableSequence sequence, int amt) {
		createChip();
		
		sequence.addPaintable(chip);
		chip.moveCenterTo(223,389);
		chipAmt = new TextPaintable(Integer.toString(amt),
				new Font("Arial",3,20));
		chipAmt.moveCenterTo(222,395);
		
		sequence.addPaintable(chipAmt);
		this.balance = this.balance-this.bet;
	}
	
	public void updateBalance(int balance) {
		this.balance = balance;
		this.balanceView = new TextPaintable("You have $" + Integer.toString(this.balance),
				new Font("Arial",3,18), Colors.gold);
	}
	
	//Chooses a hit method
	public void hit(Card c) {
		hand.add(c);
	}
	
    public void animate(PaintableSequence sequence, Card card) {
    	card.getPaint(card.id).moveCornerTo(-100,this.y);
		sequence.addPaintable(card.getPaint(card.id));
		
		int x=-100;
		int delta = 10;
		int X = this.x + (15 * this.hand.size());
		
		while (card.getPaint(card.id).getCorner().x < X) {
			x += delta;
			card.getPaint(card.id).moveCornerTo(x, this.y);
			JPTUtilities.pauseThread(17);
		}
		card.getPaint(card.id).moveCornerTo(X, this.y);
	}

    
    public void stand() {
    	this.stood = true;
    }
    
	public void animateWin() {
		win.moveCenterTo(377,280);
		winBack.moveCenterTo(377,280);
	}
	
	public void animateLoose() {
		loose.moveCenterTo(367,280);
		looseBack.moveCenterTo(367,280);
	}

	public void win(PaintableSequence sequence) {
		Toolkit.getDefaultToolkit().beep();
		System.out.println("You win!");
		sequence.addPaintable(winBack);
		sequence.addPaintable(win);
		animateWin();
	}
	
	public void loose(PaintableSequence sequence, Paintable chip) {
		//Toolkit.getDefaultToolkit().beep();
		System.out.println("You loose");
		animateLooseBet(sequence, chip);
		sequence.addPaintable(looseBack);
		sequence.addPaintable(loose);
		animateLoose();

	}
	
	public void animateLooseBet(PaintableSequence sequence, Paintable chip) {
		
		double y = chip.getCenter().y;
		int delta = 10;
		
		while (chip.getCenter().y > -10) {
			y -= delta;
			chip.moveCenterTo(chip.getCenter().x, y);
			JPTUtilities.pauseThread(17);
		}
	}
}

