#include <iostream>
#include <GL/glut.h>

#include "CMove.h"
#include "Pieces.h"
#include "CBoard.h"




//////////////
//          //
//  KNIGHT  //
//          //
//////////////

char CKnight::GetChar() const
{
  return ((GetColor() == WHITE) ? 'N' : 'n');
}

CBasePiece* CKnight::GetCopy(CBoard* par) const
{
  return new CKnight(par, GetX(), GetY(), GetColor());
}

type_piece CKnight::GetType() const
{
  return KNIGHT;
}


bool CKnight::IsCheck(type_color Col) const
{
  if ((Col == EMPTY) || (Col == GetColor()))
    return false;

  char King = ((Col == WHITE) ? 'K' : 'k');

  char x = GetX(), y = GetY();

  // NNE Check
  if ( (x < 7) && (y < 6) )
    if ((*GetParent())(x + 1, y + 2)->GetChar() == King)
      return true;
  // NEE Check
  if ( (x < 6) && (y < 7) )
    if ((*GetParent())(x + 2, y + 1)->GetChar() == King)
      return true;

  // SEE Check
  if ( (x < 6) && (y > 0) )
    if ((*GetParent())(x + 2, y - 1)->GetChar() == King)
      return true;

  // SSE Check
  if ( (x < 7) && (y > 1) )
    if ((*GetParent())(x + 1, y - 2)->GetChar() == King)
      return true;

  // SSW Check
  if ( (x > 0) && (y > 1) )
    if ((*GetParent())(x - 1, y - 2)->GetChar() == King)
      return true;

  // SWW Check
  if ( (x > 1) && (y > 0) )
    if ((*GetParent())(x - 2, y - 1)->GetChar() == King)
      return true;

  // NWW Check
  if ( (x > 1) && (y < 7) )
    if ((*GetParent())(x - 2, y + 1)->GetChar() == King)
      return true;

  // NNW Check
  if ( (x > 0) && (y < 6) )
    if ((*GetParent())(x - 1, y + 2)->GetChar() == King)
      return true;

  return false;
}

bool CKnight::IsMoveValid(const CMove& move) const
{
  if ( (move.GetSourceX() != GetX()) || (move.GetSourceY() != GetY()) )
    return false;

  char DirX = abs(move.GetDestinationX() - move.GetSourceX());
  char DirY = abs(move.GetDestinationY() - move.GetSourceY());

  if (!( ( (DirX == 1) && (DirY == 2) ) ||
         ( (DirX == 2) && (DirY == 1) ) ) )
    return false;


  if ((*GetParent())(move.GetDestinationX(),
                     move.GetDestinationY())->GetColor() == GetColor())
    return false;

  CBoard copy = *GetParent();
  copy.Move(move);

  if (copy.IsCheck(GetColor()))
    return false;

  return true;
}

void CKnight::MoveAlert(const CMove& move)
{
  return;
}
