import java.awt.*;
import java.awt.GradientPaint;
import javax.swing.*;
import java.io.*;
import java.util.*;

/**
 *   This class contains information and methods related to 
 *   Players and their pieces.
 *   @author Casey Daughtery, Joshua Canter, Yvon Feaster
 *	 @version 4.0
*/
public class Player extends Thread
{
   protected int direction, playerNumber;
   protected boolean isMyTurn;
   protected Color color;
   protected Player opponent;
   protected Piece pieces[];
   protected String name;
   protected boolean hasWon;
   protected Board b;
   
   /**
     *  Constructor
     *  @param pNum       Number of player
     *  @param dir        Direction
     *  @param n	        String for name
     *  @param boardRef   Board  reference
     *
    */
   public Player (int pNum, int dir, String n, Board boardRef)
   {
      this.pieces = new Piece[12];
      this.color = new Color(128, 128, 128);
      this.playerNumber = pNum;
      this.direction = dir;
      this.name = n;
      this.b = boardRef;
      
      if (this.playerNumber == 0)
      {
         this.isMyTurn = true;
         this.color = Color.red;
      }
      if (this.playerNumber == 1)
      {
         this.isMyTurn = false;         
         this.color = Color.black;
      }
   }
   
   /**
     *  Constructor
     *  @param pNum           interger for player number
     *  @param dir            interger for direction 
     *  @param n	            String for name
     *  @param otherPlayer    Player for opponent
     *  @param boardRef       reference to the board
    */
   public Player (int pNum, int dir, String n, Player otherPlayer, Board boardRef)
   {
      this.pieces = new Piece[12];
      this.playerNumber = pNum;
      this.direction = dir;
      this.name = n;
      this.b = boardRef;

      if (this.playerNumber == 0)
      {
         this.isMyTurn = true;
         this.color = Color.red;
         this.opponent = otherPlayer;
      }
      if (this.playerNumber == 1)
      {
         this.isMyTurn = false;         
         this.color = Color.black;
         this.opponent = otherPlayer;
      }
   }

   /**
     *  This method sets the players opponent
     *  @param otherPlayer        Player
    */
   public void setOpponent(Player otherPlayer)
   {
      this.opponent = otherPlayer;
   }
   
   /**
     *  This method gets the number of the player
     *  @return    int that is the number of the player
    */
   public int getPlayerNumber()
   {
      return this.playerNumber;
   }

   /**
     *  This method ends the current players turn and updates the opponent
     *  that it is now their turn.
     *  @return      The player number of the player whose turn it now is.
    */   
   public int endTurn()
   {
      this.isMyTurn = false;
      return this.opponent.setTurn();
   }


   /**
     *  This method returns the interger representing the players number
     *  and it calls threads notify.
     *  @return    int 
    */
   public synchronized int setTurn()
   {
      this.isMyTurn = true;
      notify();
      return this.getPlayerNumber();
   }
   
   /**
     *  This method is overridden in the NetPlayer class
     *  @param  col1   column
     *  @param  row1   row
     *  @param  col2   column
     *  @param  row2   row
     *
    */
   public void sendMove(int col1, int row1, int col2, int row2)
   {
   }
   
   /**
     *  This method sets the checker array to the appropriate piece
     *  @param pieces     Piece class
    */
   public void setPieceArray(Piece pieces[])
   {
      this.pieces = pieces;
   }
   
   /**
     *  This methods determines whether a given piece must jump.  
     *  It is an overloaded method.
     *  @param b     A reference to the board for determining jump capabilities
     *  @param chNo  Lets the method which piece to check for jump capabilities
     *  @return      True if the given piece must jump
    */
/*   public boolean playerMustJump(Board b, int chNo)
   {
      if (pieces[chNo].getCol() < 6 && pieces[chNo].getRow() < 6)
      {
         if (pieces[chNo].isMoveValid(b.sq[pieces[chNo].getCol() + 2]
                                          [pieces[chNo].getRow() + 2].
                                          isOccupiable, 
                                          b.sq[pieces[chNo].getCol() + 2]
                                          [pieces[chNo].getRow() + 2].
                                          hasPiece, 
                                          pieces[chNo].getCol() + 2, 
                                          pieces[chNo].getRow() + 2, b))
         {
            return true;
         }
      }
      if (pieces[chNo].getCol() > 1 && pieces[chNo].getRow() > 1)
      {
         if (pieces[chNo].isMoveValid(b.sq[pieces[chNo].getCol() - 2]
                                          [pieces[chNo].getRow() - 2].
                                          isOccupiable, 
                                          b.sq[pieces[chNo].getCol() - 2]
                                          [pieces[chNo].getRow() - 2].
                                          hasPiece, 
                                          pieces[chNo].getCol() - 2, 
                                          pieces[chNo].getRow() - 2, b))
         {
            return true;
         }
      }
      if (pieces[chNo].getCol() > 1 && pieces[chNo].getRow() < 6)
      {         
         if (pieces[chNo].isMoveValid(b.sq[pieces[chNo].getCol() - 2]
                                          [pieces[chNo].getRow() + 2].
                                          isOccupiable, 
                                          b.sq[pieces[chNo].getCol() - 2]
                                          [pieces[chNo].getRow() + 2].hasPiece, 
                                          pieces[chNo].getCol() - 2, 
                                          pieces[chNo].getRow() + 2, b))
         {
            return true;
         }
      }
      if (pieces[chNo].getCol() < 6 && pieces[chNo].getRow() > 1)
      {
         if (pieces[chNo].isMoveValid(b.sq[pieces[chNo].getCol() + 2]
                                          [pieces[chNo].getRow() - 2].
                                          isOccupiable, 
                                          b.sq[pieces[chNo].getCol() + 2]
                                          [pieces[chNo].getRow() - 2].
                                          hasPiece, 
                                          pieces[chNo].getCol() + 2, 
                                          pieces[chNo].getRow() - 2, b))
         {
            return true;
         }
      }
      return false;      
   } */
   /**
     *  This methods determines whether a player has any pieces it must jump.  
     *  It is an overloaded method.
     *  @param b     A reference to the board for determining jump capabilities
     *  @return      True if any piece must jump
    */  
/*   public boolean playerMustJump(Board b)
   {
      for (int count = 0; count < 12; count++)
      {
         if (pieces[count].getCol() < 6 && pieces[count].getRow() < 6)
         {
            if (pieces[count].isMoveValid(b.sq[pieces[count].getCol() + 2]
                                              [pieces[count].getRow() + 2].
                                              isOccupiable, 
                                              b.sq[pieces[count].getCol() + 2]
                                              [pieces[count].getRow() + 2].
                                              hasPiece, 
                                              pieces[count].getCol() + 2, 
                                              pieces[count].getRow() + 2, b))
            {
               return true;
            }
         }
         if (pieces[count].getCol() > 1 && pieces[count].getRow() > 1)
         {
            if (pieces[count].isMoveValid(b.sq[pieces[count].getCol() - 2]
                                              [pieces[count].getRow() - 2].
                                              isOccupiable, 
                                              b.sq[pieces[count].getCol() - 2]
                                              [pieces[count].getRow() - 2].
                                              hasPiece, 
                                              pieces[count].getCol() - 2, 
                                              pieces[count].getRow() - 2, b))
            {
               return true;
            }
         }
         if (pieces[count].getCol() > 1 && pieces[count].getRow() < 6)
         {         
            if (pieces[count].isMoveValid(b.sq[pieces[count].getCol() - 2]
                                              [pieces[count].getRow() + 2].
                                              isOccupiable, 
                                              b.sq[pieces[count].getCol() - 2]
                                              [pieces[count].getRow() + 2].
                                              hasPiece, 
                                              pieces[count].getCol() - 2, 
                                              pieces[count].getRow() + 2, b))
            {
               return true;
            }
         }
         if (pieces[count].getCol() < 6 && pieces[count].getRow() > 1)
         {
            if (pieces[count].isMoveValid(b.sq[pieces[count].getCol() + 2]
                                              [pieces[count].getRow() - 2].
                                              isOccupiable, 
                                              b.sq[pieces[count].getCol() + 2]
                                              [pieces[count].getRow() - 2].
                                              hasPiece, 
                                              pieces[count].getCol() + 2, 
                                              pieces[count].getRow() - 2, b))
            {
               return true;
            }
         }
      }
      return false;      
   } */

   /**
     *  This methods determines whether a player has any moves.  
     *  @param b     A reference to the board for determining jump capabilities
     *  @return      True if any moves are possible
    */      
/*   public boolean mayMove(Board b)
   {
      for (int count = 0; count < 12; count++)
      {
         if (pieces[count].getCol() < 7 && pieces[count].getRow() < 7)
         {
            if (pieces[count].isMoveValid(b.sq[pieces[count].getCol() + 1]
                                              [pieces[count].getRow() + 1].
                                              isOccupiable, 
                                              b.sq[pieces[count].getCol() + 1]
                                              [pieces[count].getRow() + 1].
                                              hasPiece, 
                                              pieces[count].getCol() + 1, 
                                              pieces[count].getRow() + 1, b))
            {
               return true;
            }
         }
         if (pieces[count].getCol() > 0 && pieces[count].getRow() > 0)
         {
            if (pieces[count].isMoveValid(b.sq[pieces[count].getCol() - 1]
                                              [pieces[count].getRow() - 1].
                                              isOccupiable, 
                                              b.sq[pieces[count].getCol() - 1]
                                              [pieces[count].getRow() - 1].
                                              hasPiece, 
                                              pieces[count].getCol() - 1, 
                                              pieces[count].getRow() - 1, b))
            {
               return true;
            }
         }
         if (pieces[count].getCol() > 0 && pieces[count].getRow() < 7)
         {         
            if (pieces[count].isMoveValid(b.sq[pieces[count].getCol() - 1]
                                              [pieces[count].getRow() + 1].
                                              isOccupiable, 
                                              b.sq[pieces[count].getCol() - 1]
                                              [pieces[count].getRow() + 1].
                                              hasPiece, 
                                              pieces[count].getCol() - 1, 
                                              pieces[count].getRow() + 1, b))
            {
               return true;
            }
         }
         if (pieces[count].getCol() < 7 && pieces[count].getRow() > 0)
         {
            if (pieces[count].isMoveValid(b.sq[pieces[count].getCol() + 1]
                                              [pieces[count].getRow() - 1].
                                              isOccupiable, 
                                              b.sq[pieces[count].getCol() + 1]
                                              [pieces[count].getRow() - 1].
                                              hasPiece, 
                                              pieces[count].getCol() + 1, 
                                              pieces[count].getRow() - 1, b))
            {
               return true;
            }
         }
         if(this.playerMustJump(b, count))
         {
            return true;
         }
      }
      return false;         
   } */
   
   public int playerType()
   {
      return 0;
   }
   
   public void waitForTurn(Board b)
   {
      this.opponent.waitForTurn(b);
   }
   
   public synchronized void run()
   {
      while (!b.gameOver)
      {
         while (!isMyTurn)
         {
            try
            {
               wait();
            }
            catch (InterruptedException e)
            {
               return;
            }
         }
      }
   }
}