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;
   protected String specialMove;
   protected boolean inCheck;
   protected King myKing;
   
   /**
     *  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;
      this.specialMove = "";
      
      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;
      this.specialMove = "";

      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;
   }
   
   public String getPlayerColor()
   {
      return (this.playerNumber == 0) ? "white" : "black";
   }

   /**
     *  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,
                        String spMove)
   {
   }
   
   /**
     *  This method sets the checker array to the appropriate piece
     *  @param pieces     Piece class
    */
   public void setPieceArray(Piece pieces[])
   {
      this.pieces = pieces;
   }
   
   public int playerType()
   {
      return 0;
   }
   
   public void waitForTurn(Board b)
   {
      this.opponent.waitForTurn(b);
   }
   
   public synchronized void run()
   {
      for(;;)
      {
         try
         {
            wait();
         }
         catch (InterruptedException e)
         {
            return;
         }
      }
   }
   
   public boolean isInCheck()
   {
      inCheck = false;
      
      for (Piece p : this.opponent.pieces)
      {
         if (p.isMoveValid(this.myKing.getCol(), this.myKing.getRow(), b))
         {
            inCheck = true;
         }
      }
      return inCheck;
   }
   
   public void setPromotion(String pieceType){
      specialMove += pieceType;
   }
   
   public boolean hasAnyMoves()
   {
      for (Piece p : this.pieces)
      {
         for (int x = 0; x < 8; x++)
         {
            for (int y = 0; y < 8; y++)
            {
               if (p.isMoveValid(x, y, b))
               {
                  return true;
               }
            }
         }
      }
      return false;
   }
}