import java.awt.*;
import javax.swing.*;

/**
 *   This class contains information and methods associated with the piece 
 *   for a checker board
 *   public
 *   @author Casey Daughtery, Joshua Canter, Yvon Feaster
 *	 @version 4.0
*/
public class King extends Piece
{
   public King (int x, int y, int w, Color c, Player owner, int n, String set)
   {  
      this.color = new Color(255, 255, 255);
      this.color = c;
      this.pos = new Point(x, y);
      this.width = w;
      this.coord = new Point (x / width, y / width);
      this.player = owner;
      this.pieceNumber = n;
      this.isNull = false;
      this.hasBeenMoved = false;
      this.player.myKing = this;
      this.fullPath = this.imagePath + set + player.getPlayerColor() + "king.png";
      this.img = Toolkit.getDefaultToolkit().getImage(fullPath);
   }
   
   public King (Piece p)
   {
      this.color = p.color;
      this.pos = new Point(p.pos.x, p.pos.y);
      this.width = p.width;
      this.coord = new Point (p.coord.x, p.coord.y);
      this.player = p.player;
      this.isNull = p.isNull;
      this.hasBeenMoved = p.hasBeenMoved;
      this.fullPath = this.imagePath + p.player.b.set + player.getPlayerColor() + "king.png";
      this.img = Toolkit.getDefaultToolkit().getImage(fullPath);
   }

   /**
    *   This method check to see if a checker move is valid.
    *   public
    *   @param isBlackSquare   true if it is occupiable square
    *   @param isNotEmpty      true if the square has a piece
    *   @param c               destination of x coordinate
    *   @param r							 destination of y coordinate
    *   @param b               a reference to Board
    *	  @return                true if the move is valid
   */  
   public boolean isMoveValid (int c, int r, Board b)
   {
      boolean goodMove = false;
      int boardOrientation = (this.player.getPlayerNumber() == 1)?1:-1;
      Piece tempKing;
      Point heading = new Point(0, 0);
      Point currentSpotCheck = new Point (0, 0);
      player.specialMove = "";

      heading.x = c - this.getCol();
      heading.y = r - this.getRow();
      
      //for castling
      if (Math.abs(heading.x) == 2 && Math.abs(heading.y) == 0)
      {
         if (!this.hasBeenMoved && 
             heading.x == -2 * boardOrientation && 
                b.sq[c-1*boardOrientation][r].contents.pieceType() == "Rook" &&
               !b.sq[c-1*boardOrientation][r].contents.hasBeenMoved &&
               !player.isInCheck())
         {
            if (!b.sq[c][r].getHasPiece() && 
                !b.sq[c+1*boardOrientation][r].getHasPiece())
            {
               for (Piece p : player.opponent.pieces)
               {
                  if (p.isMoveValid(c, r, b))
                  {
                     return false;
                  }
                  if (p.isMoveValid(c+1*boardOrientation, r, b))
                  {
                     return false;
                  }
               }
               if (this.player.isInCheck())
               {
                  return false;
               }
            
               player.specialMove = "Castling";
               tempKing = b.draggedPiece;
               b.draggedPiece = b.sq[c-1*boardOrientation][r].contents;
               b.sq[c-1*boardOrientation][r].contents.
                  executeMove(c + 1 * boardOrientation, r, b, this.player);
               this.player.opponent.sendMove(c - 1 * boardOrientation, r, 
                                             c + 1 * boardOrientation, r,
                                             player.specialMove);
               b.draggedPiece = tempKing;
               goodMove = tryMove(c, r, b, this.player);
            }
         }
         if (!this.hasBeenMoved && 
             heading.x == -2 && 
                b.sq[c+2*boardOrientation][r].contents.pieceType() == "Rook" &&
               !b.sq[c+2*boardOrientation][r].contents.hasBeenMoved)
         {
            if (!b.sq[c][r].getHasPiece() && 
                !b.sq[c+1][r].getHasPiece() &&
                !b.sq[c-1][r].getHasPiece())
            {
               for (Piece p : player.opponent.pieces)
               {
                  if (p.isMoveValid(c, r, b))
                  {
                     return false;
                  }
                  if (p.isMoveValid(c-1*boardOrientation, r, b))
                  {
                     return false;
                  }
               }
               if (this.player.isInCheck())
               {
                  return false;
               }
               player.specialMove = "Castling";
               tempKing = b.draggedPiece;
               b.draggedPiece = b.sq[c+2*boardOrientation][r].contents;
               b.sq[c+2*boardOrientation][r].contents.
                  executeMove(c - 1 * boardOrientation, r, b, this.player);
               this.player.opponent.sendMove(c + 2 * boardOrientation, r, 
                                             c - 1 * boardOrientation, r,
                                             player.specialMove);
               b.draggedPiece = tempKing;
               goodMove = tryMove(c, r, b, this.player);
            }            
         }   
      }
      
      if (heading.x == 0 && heading.y == 0)
      {
         return false;
      }
      
      if (Math.abs(heading.x) == 1 && Math.abs(heading.y) == 0 ||
          Math.abs(heading.x) == 0 && Math.abs(heading.y) == 1 ||
          Math.abs(heading.x) == 1 && Math.abs(heading.y) == 1)
      {
         if (b.sq[c][r].hasPiece)
         {
            if (b.sq[c][r].contents.player == this.player)
            {
               return false;
            }
         }
         
         goodMove = tryMove(c, r, b, this.player);
      }
      
      if (!goodMove)
      {
         this.player.specialMove = "";
      }
      
      return goodMove;
   }
   
   /**
    *   This method is a forward declaration for the draw methods in King 
    *   and Pawn.
    *   public
    *   @param g      the passed Graphics object
    */   
//   public abstract void drawPiece(Graphics g);
   
   /**
     *  This method is a forward declaration for a functor that 
     *  returns whether or not the piece is a King.
     *  public
     *  @return     True if the piece is a member of the King
    */
   public String pieceType()
   {
      return "King";
   }
   
   public int pieceTypeID()
   {
      return 6;
   }
   
   /**
     *  Since the move has been verified as valid this method
     *  only concerns itself with moving the pieces.
     *  @param  col  x coordinate of its destination
     *  @param  row  y coordinate of its destination
     *  @param  b    a reference to the board in order to refer to other pieces
    */
   public void executeMove(int col, int row, Board b, Player p)
   {
      this.hasBeenMoved = true;
      b.sq[this.getCol()][this.getRow()].hasPiece = false;
      b.sq[this.getCol()][this.getRow()].contents = new Pawn ();

      if(b.sq[col][row].hasPiece)
      {
         b.sq[col][row].contents.hasBeenTaken(b.sq[col][row]);
      }

      //Sets checkers new col row xpos ypos and new squares hasChecker                
      this.setCol(col);
      this.setRow(row);
      this.setXpos(col*b.width);
      this.setYpos(row*b.width);
      b.sq[this.getCol()][this.getRow()].hasPiece = true;
      b.sq[this.getCol()][this.getRow()].contents = b.draggedPiece;
      b.pc[p.getPlayerNumber()]
          [this.getPieceNumber()] = b.draggedPiece;
      b.players[p.getPlayerNumber()].
          setPieceArray(b.pc[p.getPlayerNumber()]);
      
//      System.out.println(this + " " + this.player + " "/* + this.player.myKing*/);
      p.myKing = this;
   }
}