import java.awt.*;
import javax.swing.*;
import java.io.File;
import java.awt.image.*;

/**
 *   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 abstract class Piece
{
   protected Point pos, coord;
   protected int width;
   protected Color color;
   protected Player player;
   protected int pieceNumber;
   protected boolean isNull;
   protected boolean hasBeenMoved;
   protected static String imagePath;
   protected Image img;
   protected String fullPath;

   static
   {
      File dir = new File(".");
      imagePath = "";
      
      try
      {
         imagePath += dir.getCanonicalPath() + "/images/"; 
      }
      catch (Exception e)
      {
         System.err.println(e);
      }
   }

   /**
    *   This method gets protected information from the Piece class.
    *   public
    *   @return     the x position of the piece in pixels
   */    
   public int getXpos()
   {
      return this.pos.x;
   }

    /**
    *   This method sets x position in pixels.
    *   public
    *   @param x    the x position of the piece in pixels
   */ 
   public void setXpos(int x)
   {
      this.pos.x = x;
   }

   /**
    *   This method gets protected information from the Piece class.
    *   public
    *   @return     the y position of the piece in pixels
   */  
   public int getYpos()
   {
      return this.pos.y;
   }

  /**
    *   This method sets y position in pixels.
    *   public
    *   @param y    the y position of the piece in pixels
   */ 
   public void setYpos(int y)
   {
      this.pos.y = y;
   }

   /**
    *   This method gets protected information from the checker class.
    *   public
    *   @return     color of the checker piece
   */   
   public Color getColor()
   {
      return this.color;
   }

   /**
    *   This method sets color.
    *   public
    *   @param c   sets the new color for the piece
   */   
   public void setColor(Color c)
   {
      this.color = c;
   }

   /**
    *   This method gets protected information from the Piece class.
    *   public
    *   @return    the x coordinate of the Piece
   */    
   public int getCol()
   {
      return this.coord.x;
   } 
 
    /**
    *   This method sets col.
    *   public
    *   @param c   the x coordinate of the Piece
   */  
   public void setCol(int c)
   {
      this.coord.x = c;
   }

   /**
    *   This method gets protected information from the Piece class.
    *   public
    *   @return    y coordinate of the Piece
   */    
   public int getRow()
   {
      return this.coord.y;
   }

   /**
    *   This method sets row.
    *   public
    *   @param r   sets the y coordinate of the piece
   */   
   public void setRow(int r)
   {
      this.coord.y = r;
   }

   /**
    *   This method gets protected information from the Piece class.
    *   public
    *   @return    the width of the piece
   */    
   public int getWidth()
   {
      return this.width;
   }
 
   /**
    *   This method sets width of the piece.
    *   public
    *   @param  w   the width of the piece
   */   
   public void setWidth(int w)
   {
      this.width = w;
   }

   /**
    *   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 abstract boolean isMoveValid (int c, int r, Board b);

   /**
    *   This method removes a jumped piece from the board.
    *   public
    *   @param containedBy     the square that contains this piece
   */
   public void hasBeenTaken(Square containedBy)
   {
      if (!containedBy.getHasPiece() || this.isNull)
      {
         return;
      }

//      System.out.println(this.player.name + "'s " + this.pieceType() +
//                         " at (" + this.getCol() + ", " + this.getRow() + ") was taken!");

      this.player.b.pc[this.player.getPlayerNumber()]
                      [this.pieceNumber] = new Pawn();
      this.player.setPieceArray(
         this.player.b.pc[this.player.getPlayerNumber()]);
      containedBy.contents = new Pawn();

      this.color = new Color(0, 0, 0);
      this.pos = new Point(0, 0);
      this.coord = new Point(0, 0);
      this.isNull = true;

      containedBy.setHasPiece(false);
   }
   
   /**
     *  This method gets the number of the current piece
     *  public
     *  @return    the piece number
    */
   public int getPieceNumber()
   {
      return this.pieceNumber;
   }
   
   /**
     *  This method sets the number of the current piece
     *  public
     *  @param  chNo    Sets the number of the current piece
    */
   public void setPieceNumber(int pcNo)
   {
      this.pieceNumber = pcNo;
   }
   /**
     *  This method checks to see if the current piece is "null".
     *  Rather than having truly null pieces, this method allows the
     *  program to verify whether or not it is dealing with a valid piece.  
     *  @return   True if the piece is not currently defined for play.
    */
   public boolean isNull()
   {
      return this.isNull;
   }

   /**
     *  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 abstract String pieceType();
   public abstract int pieceTypeID();
   
   /**
     *  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;
//      System.out.println("This piece moved from: " + this.getCol() + " " + this.getRow());
      b.sq[this.getCol()][this.getRow()].hasPiece = false;
      b.sq[this.getCol()][this.getRow()].contents = new Pawn ();

      if(p.specialMove.intern() == "EnPassant")
      {
         b.sq[col][row-p.direction].contents.hasBeenTaken(b.sq[col][row-p.direction]);
      }
      else 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 = this;
      b.pc[p.getPlayerNumber()]
          [this.getPieceNumber()] = this;
      b.players[p.getPlayerNumber()].
          setPieceArray(b.pc[p.getPlayerNumber()]);
   }
   
   public boolean tryMove(int col, int row, Board b, Player p)
   {
      int old_col, old_row;
      Piece takenPiece;
      int taken_col, taken_row, taken_pcNo = 0, taken_pieceTypeID;
      boolean taken_hasBeenMoved;
      boolean goodMove;
      
      old_col = this.getCol();
      old_row = this.getRow();
      
/*      if(p.specialMove.intern() == "Castling" && p.pieceType() == "King")
      {
         int dirCastle = (col - this.getCol()) / 2;
         if (
      } */

      if(p.specialMove.intern() == "EnPassant")
      {
         taken_col = col;
         taken_row = row + p.direction;
         taken_pcNo = b.sq[col][row-p.direction].contents.getPieceNumber();
         taken_pieceTypeID = b.sq[col][row-p.direction].contents.pieceTypeID();
         taken_hasBeenMoved = b.sq[col][row-p.direction].contents.hasBeenMoved;
      }
      else
      {
         taken_col = col;
         taken_row = row;
         taken_pcNo = b.sq[col][row].contents.getPieceNumber();
         taken_pieceTypeID = b.sq[col][row].contents.pieceTypeID();
         taken_hasBeenMoved = b.sq[col][row].contents.hasBeenMoved;
      }

      if (b.sq[col][row].contents.isNull)
      {
         taken_pieceTypeID = 0;
      }
      
      switch (taken_pieceTypeID)
      {
         case 1:
            takenPiece = new Pawn(taken_col * b.width,
                                  taken_row * b.width,
                                  b.width,
                                  p.opponent.color,
                                  p.opponent,
                                  taken_pcNo, b.set);
            break;
         case 2:
            takenPiece = new Knight(taken_col * b.width,
                                    taken_row * b.width,
                                    b.width,
                                    p.opponent.color,
                                    p.opponent,
                                    taken_pcNo, b.set);
            break;
         case 3:
            takenPiece = new Bishop(taken_col * b.width,
                                    taken_row * b.width,
                                    b.width,
                                    p.opponent.color,
                                    p.opponent,
                                    taken_pcNo, b.set);
            break;
         case 4:
            takenPiece = new Rook(taken_col * b.width,
                                  taken_row * b.width,
                                  b.width,
                                  p.opponent.color,
                                  p.opponent,
                                  taken_pcNo, b.set);
            break;
         case 5:
            takenPiece = new Queen(taken_col * b.width,
                                   taken_row * b.width,
                                   b.width,
                                   p.opponent.color,
                                   p.opponent,
                                   taken_pcNo, b.set);
            break;
         case 6:
            takenPiece = new King(taken_col * b.width,
                                  taken_row * b.width,
                                  b.width,
                                  p.opponent.color,
                                  p.opponent,
                                  taken_pcNo, b.set);
            p.opponent.myKing = (King) takenPiece;
            break;
         default:
            takenPiece = new Pawn();
      }

      takenPiece.hasBeenMoved = taken_hasBeenMoved;

      this.executeMove(col, row, b, p);
      
//      System.out.println("Checking to see if that move makes you vulnerable.");
      if (this.player.isInCheck())
      {
         goodMove = false;
      }
      else
      {
         goodMove = true;
      }
           
      String temp = p.specialMove;
      p.specialMove = "";
      this.executeMove(old_col, old_row, b, p);
      p.specialMove = temp;
      
      if(!takenPiece.isNull())
      {
         b.sq[taken_col][taken_row].contents = takenPiece;
         b.sq[taken_col][taken_row].setHasPiece(true);
         b.pc[p.opponent.getPlayerNumber()][taken_pcNo] = takenPiece;
         p.setPieceArray(b.pc[p.opponent.getPlayerNumber()]);
      }

      return goodMove;
   }
   
   /**
    *   This method is a forward declaration for the draw methods in King 
    *   and Pawn.
    *   public
    *   @param g      the passed Graphics object
    */   
   public void drawPiece(Graphics g, Board b)
   {
      if (this.isNull())
      {
         return;
      }

      Graphics2D g2 = (Graphics2D) g;

      //AntiAliasing
      RenderingHints renderHints =
      new RenderingHints(RenderingHints.KEY_ANTIALIASING,
      RenderingHints.VALUE_ANTIALIAS_ON);
      renderHints.put(RenderingHints.KEY_RENDERING,
      RenderingHints.VALUE_RENDER_QUALITY);
      g2.setRenderingHints(renderHints);

      g2.drawImage(img, this.getXpos(), this.getYpos(),
                   this.player.b.width, this.player.b.width, null);
 //     b.repaint();
   }
}
