import java.awt.*;
import javax.swing.*;

/**
  *  This class contains information and methods related
  *  to uncrowned pieces.
  *  @author Casey Daughtery, Joshua Canter, Yvon Feaster 
  *  @version 4.0
 */
 
public class Pawn extends Piece
{
   /**
    *   This constructor initializs all the necessary
    *   information for the Pawn class
    *   public
    *   @param x   x coordinate 
    *   @param y   y coordinate
    *   @param w   width
    *   @param c   color of the Pawn
    *   @param owner  an instance of Player
    *   @param n   the number of the current Pawn
   */
   public Pawn (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.fullPath = this.imagePath + set + player.getPlayerColor() + "pawn.png";
      this.img = Toolkit.getDefaultToolkit().getImage(fullPath);
   }
   
   /**
     *   This is a contructor that takes no params.
    */
   public Pawn ()
   {
      this.color = new Color(0, 0, 0);
      this.pos = new Point(-1, -1);
      this.coord = new Point(-1, -1);
      this.hasBeenMoved = true;
      this.player = null;
      this.isNull = true;
   }
   /**
     *   This is a contructor.
     *   @param  checker   An instance of Piece
    */   
   
   public Pawn (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() + "pawn.png";
      this.img = Toolkit.getDefaultToolkit().getImage(fullPath);    
   }
 
   /**
    *   This method checks to see if a king move is valid.
    *   public
    *   @param isBlackSquare   true if the current square is occupiable
    *   @param isNotEmpty      true if the current square is occupied
    *   @param c               the x postion of the square that holds the pawn
    *   @param r               the y postion of the square that holds the pawn
    *   @param b               a reference to the board
    *	  @return                true if the move is valid
   */  
   public boolean isMoveValid (int c, int r, Board b)
   {
      boolean goodMove = false;

      if (this.isNull())
      {
         return false;
      }

      String tokens[];

      player.specialMove = "";

      if(!hasBeenMoved && r == this.getRow() + 2 * this.player.direction &&
         c == this.getCol() &&
         b.sq[c][r].hasPiece == false)
      {
         player.specialMove = "DoublePush";
         goodMove = tryMove(c, r, b, this.player);
      }
      
      if (!(b.moveLog.size() == 0))
      {
         tokens = b.moveLog.get(b.moveLog.size()-1).split(" ");
      
         if(tokens.length > 7)
         {
            if(tokens[7].intern() == "DoublePush")
            {
               int pawnCol = new Integer(tokens[4]);
               int pawnRow = new Integer(tokens[5]);

               if(pawnCol == c && 
                  pawnRow + this.player.direction == r &&
                  r == this.getRow() + this.player.direction &&
                  Math.abs(this.getCol() - pawnCol) == 1)
               {
                  player.specialMove = "EnPassant";
/*                  b.sq[pawnCol][pawnRow].contents.hasBeenTaken
                                               (b.sq[pawnCol][pawnRow]); */
                  goodMove = tryMove(c, r, b, this.player);
               }
            }
         }
      }
      
      if(r == this.getRow() + this.player.direction && 
         c == this.getCol() &&
         b.sq[c][r].hasPiece == false)
      {
         if (r == 0 || r == 7)
         {
            player.specialMove = "Promotion";
         }
         goodMove = tryMove(c, r, b, this.player);
      }
      
      if(r == this.getRow() + this.player.direction &&
         (c == this.getCol() + 1 || c == this.getCol() - 1) &&
         b.sq[c][r].hasPiece == true)
      {
         if (b.sq[c][r].contents.player == this.player.opponent)
         {
            if (this.player.opponent.myKing.getCol() == c &&
                this.player.opponent.myKing.getRow() == r)
            {
                goodMove = true;
            }
            else
            {
               if (r == 0 || r == 7)
               {
                  player.specialMove = "Promotion";
               }
               goodMove = tryMove(c, r, b, this.player);
            }
         }
      }
      
      if (!goodMove)
      {
         player.specialMove = "";
      }
      
      return goodMove;
   }
   
   public String pieceType()
   {
      return "Pawn";
   }
   
   public int pieceTypeID()
   {
      return 1;
   }
}
