import java.awt.*;
import javax.swing.*;
import java.awt.geom.*;
/**
  *  This class contains information and methods related
  *  to crowned pieces.
  *  @author Casey Daughtery, Joshua Canter, Yvon Feaster 
  *  @version 2.0
 */
 
public class King extends Piece
{
   /**
    *   This constructor initializs all the necessary
    *   information for the King class
    *   public
    *   @param x   x coordinate 
    *   @param y   y coordinate
    *   @param w   width
    *   @param c   color of the King
    *   @param n   the number of the current King
   */
   public King (int x, int y, int w, Color c, int n)
   {  
      this.color = new Color(255, 255, 255);
      this.pos = new Point(x, y);
      this.width = w;
      this.coord = new Point (x / width, y / width);
      this.color = c;
      this.player = null;
      this.checkerNumber = n;
      this.isNull = false;      
   }
   
   public King (Piece checker)
   {
      this.color = checker.color;
      this.pos = new Point(checker.pos.x, checker.pos.y);
      this.width = checker.width;
      this.coord = new Point (checker.coord.x, checker.coord.y);
      this.player = checker.player;
      this.checkerNumber = checker.checkerNumber;
      this.isNull = checker.isNull;
   }

   /**
     *   This is a contructor that takes no params.
    */
   public King ()
   {
      this.color = new Color(0, 0, 0);
      this.pos = new Point(0, 0);
      this.coord = new Point(0, 0);      
      this.player = null;
      this.isNull = true;
   }
   /**
     *   This method returns a boolean if the Piece is a king.
     *   @return  True if the Piece is a king
    */
   public boolean isKing ()
   {
      return true;
   }
 
   /**
    *   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 king
    *   @param r               the y postion of the square that holds the king
    *   @param b               a reference to the board
    *	  @return                true if the move is valid
   */  
   public boolean isMoveValid (boolean isBlackSquare, boolean isNotEmpty, 
                               int c, int r, Board b)
   {                
      if (isBlackSquare && !isNotEmpty)
      {         
         if(r == this.getRow() + 1)
         {
            if (c == this.getCol() - 1 || c == this.getCol() + 1)
            {
               return true;
            }
         }
          
         if(r == this.getRow() + 2)
         {
            if(c == this.getCol() - 2)
            {
               if (b.sq[this.getCol()-1]
                       [this.getRow()+1].
                       contents.player == this.player.opponent)
               {
                  return true;
               }                
            }

            if(c == this.getCol() + 2)
            {
               if (b.sq[this.getCol()+1]
                       [this.getRow()+1].
                       contents.player == this.player.opponent)
               {
                  return true;
               }                
            }
         }
         
         if(r == this.getRow() - 1)
         {
            if (c == this.getCol() - 1 || c == this.getCol() + 1)
            {
               return true;
            }
         }
          
         if(r == this.getRow() - 2)
         {
            if(c == this.getCol() - 2)
            {
               if (b.sq[this.getCol()-1]
                       [this.getRow()-1].
                       contents.player == this.player.opponent)
               {
                  return true;
               }                
            }

            if(c == this.getCol() + 2)
            {
               if (b.sq[this.getCol()+1]
                       [this.getRow()-1].
                       contents.player == this.player.opponent)
               {
                  return true;
               }                
            }
         }
      }

      return false;
   }
   
   /**
    *   This method draws a king at the piece's location.
    *   Signifies the king with a star in the middle.
    *   public
    *   @param g      passed Graphic instance
    */
    
   public void drawChecker(Graphics g)
   {      
       GradientPaint checkerGrad = new GradientPaint(0 , 0, Color.magenta, 0,
                                                     0, Color.cyan);    

       Graphics2D g2 = (Graphics2D) g;
             
       g2.setColor(this.player.color);       
       g2.fillOval(this.getXpos(),
                   this.getYpos(), 
                   this.getWidth(), 
                   this.getWidth());
                   
       if (this.player.direction == 1)
       {
          checkerGrad = new GradientPaint(this.getXpos()-(width * 10 / 9),
                           this.getYpos()-(this.width * 10 / 9), Color.black,
                           this.getXpos()+(this.width * 6 / 9), 
                           this.getYpos()+(this.width * 6 / 9), Color.red);
       }
       if (this.player.direction == -1)
       {
          checkerGrad = new GradientPaint(this.getXpos(),
                           this.getYpos(), Color.black, 
                           this.getXpos()+(this.width * 20 / 9), 
                           this.getYpos()+(this.width * 20 / 9), Color.gray);
       }

       g2.setPaint(checkerGrad);
       g2.fillOval(this.getXpos()+(this.width * 1 / 9),
                   this.getYpos()+(this.width * 1 / 9), 
                   this.getWidth()-(this.width * 2 / 9),
                   this.getWidth()-(this.width * 2 / 9));

       //create and set the stroke
       g2.setStroke(new BasicStroke(1.0f)); 
       g2.setColor(Color.white);
 
       //Create a star using a general path object
       GeneralPath p = new GeneralPath(GeneralPath.WIND_NON_ZERO);
       p.moveTo(-(this.width * 13 / 90), -(this.width * 3 / 90));
       p.lineTo(+(this.width * 13 / 90), -(this.width * 3 / 90));
       p.lineTo(-(this.width * 9 / 90), +(this.width * 13 / 90));
       p.lineTo(+(this.width * 0 / 90), -(this.width * 13 / 90));
       p.lineTo(+(this.width * 9 / 90), +(this.width * 13 / 90));

       //translate origin towards center of canvas
       g2.translate((this.pos.x + this.width / 2),
                    (this.pos.y + this.width / 2));
    
       //render the star's path
       g2.fill(p);
       g2.translate(-(this.pos.x + this.width / 2),
                    -(this.pos.y + this.width / 2));
   }
}