/**********************************************************************
 *
 *   FreeDoko a Doppelkopf-Game
 *    
 *   Copyright (C) 2001-2004  by Diether Knof and Borg Enders
 *
 *   This program is free software; you can redistribute it and/or 
 *   modify it under the terms of the GNU General Public License as 
 *   published by the Free Software Foundation; either version 2 of 
 *   the License, or (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details. 
 *   You can find this license in the file 'gpl.txt'.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the Free Software
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, 
 *   MA  02111-1307  USA
 *
 *  Contact:
 *    Diether Knof dknof@gmx.de
 *    Borg Enders  borg@borgsoft.de
 *
 *********************************************************************/

#include "constants.h"

#ifdef USE_UI_GTKMM

#include "hand.h"
#include "table.h"
#include "ui.h"

#include "cards.h"
#include "icongroup.h"

#include "../../game/game.h"
#include "../../player/player.h"
#include "../../card/trick.h"
#include "../../misc/setting.h"

namespace UI_GTKMM_NS {

  /**
   **
   ** Constructor
   **
   ** @param	table	the table
   ** @param	player	the player
   **
   ** @return	-
   ** 
   ** @author	Diether Knof
   **
   ** @version	0.6.1
   **
   **/
  Hand::Hand(Table* const table, Player const* const player) :
    HTIN(table, player)
  {
    return ;
  } // Hand::Hand(Table* const table, Player const* const player)

  /**
   **
   ** Destruktor
   **
   ** @param	-
   **
   ** @return	-
   ** 
   ** @author	Diether Knof
   **
   ** @version	0.6.1
   **
   **/
  Hand::~Hand()
  {
    return ;
  } // Hand::~Hand()

  /**
   **
   ** draws the hand
   **
   ** @param	update	whether to update the area
   **
   ** @return	-
   ** 
   ** @author	Diether Knof
   **
   ** @version	0.6.1
   **
   ** @todo	emphasize the suggested card
   **
   **/
  void
    Hand::draw(bool const update)
    {
      if ((::game_status >= GAMESTATUS::GAME_INIT)
	  && (::game_status <= GAMESTATUS::GAME_FINISHED)) {
	int pos_x = this->pos_x();
	int pos_y = this->pos_y();
	int width = this->width();
	int height = this->height();

	// emphasize the valid cards
	if (::setting(Setting::EMPHASIZE_VALID_CARDS)
	    && (::game_status == GAMESTATUS::GAME_PLAY)
	    && (this->player->type() == Player::HUMAN)
	   ) {
	  switch (this->player->no()) {
	  case 0:
	    pos_y -= (this->margin_y() * 2 / 3);
	    height += (this->margin_y() * 2 / 3);
	    break;
	  case 1:
	    width += (this->margin_x() * 2 / 3);
	    break;
	  case 2:
	    height += (this->margin_y() * 2 / 3);
	    break;
	  case 3:
	    pos_x -= (this->margin_x() * 2 / 3);
	    width += (this->margin_x() * 2 / 3);
	    break;
	  default:
	    DEBUG_ASSERTION(false,
			    "Hand::cardno_at_position():\n"
			    "  player->no not valid: "
			    << this->player->no());
	    break;
	  } // switch (this->player->no())
	} // if (emphasize valid card)
	if (update)
	  this->table().clear(Gdk::Rectangle(pos_x, pos_y, width, height));

	bool const show = ((this->player->type() == Player::HUMAN)
			   || ::setting(Setting::SHOW_ALL_HANDS)
			   || (::game_status == GAMESTATUS::GAME_FINISHED)
			   || ( (::game_status == GAMESTATUS::GAME_REDISTRIBUTE)
				&& (*(this->player)
				    == this->player->game().soloplayer()) )
			  );
	// draw the cards
	for (unsigned c = 0; c < this->player->hand().cardsnumber_all(); c++) {
	  if (this->table().in_game_review())
	    if (this->player->hand().played_trick(c)
		<= this->table().game_review_trickno())
	      continue;
	  if ( (show
		? !this->player->sorted_hand().played(c)
		: !this->player->hand().played(c) )
	       || (::game_status == GAMESTATUS::GAME_FINISHED)) {

	    Glib::RefPtr<Gdk::Pixbuf> const
	      card = (show
		      ? this->ui->cards->card(this->player->sorted_hand().card_all(c),
					      this->rotation())
		      : this->ui->cards->back(this->rotation())
		     );
	    int pos_x = (this->start_x() +
			 static_cast<int>(this->cards_step_x() * c));
	    int pos_y = (this->start_y() +
			 static_cast<int>(this->cards_step_y() * c));

	    // emphasize the valid cards
	    if (::setting(Setting::EMPHASIZE_VALID_CARDS)
		&& (::game_status == GAMESTATUS::GAME_PLAY)
		&& (this->player->game().player_current() == *this->player)
		&& (this->player->type() == Player::HUMAN)
		&& (this->player->game().trick_current().isvalid(this->player->sorted_hand().card_all(c), this->player->hand()))
	       ) {
	      switch (this->player->no()) {
	      case 0:
		pos_y -= (this->margin_y() * 2 / 3);
		break;
	      case 1:
		pos_x += (this->margin_x() * 2 / 3);
		break;
	      case 2:
		pos_y += (this->margin_y() * 2 / 3);
		break;
	      case 3:
		pos_x -= (this->margin_x() * 2 / 3);
		break;
	      default:
		DEBUG_ASSERTION(false,
				"Hand::draw():\n"
				"  player->no not valid: "
				<< this->player->no());
		break;
	      } // switch (this->player->no())
	    } // if (emphasize valid card)

	    this->table().pixmap->draw_pixbuf(this->table().gc,
					      card,
					      0, 0,
					      pos_x, pos_y,
					      -1, -1,
					      Gdk::RGB_DITHER_NONE,
					      0, 0);
	  } // if (!this->player->hand().played(c))
	} // for (c < this->player->hand().cardsnumber_all()

	if (update)
	  this->table().update(Gdk::Rectangle(pos_x, pos_y, width, height));

      } // if (GAME_RESERVATION <= game_status < GAME_FINISHED)

      return ;
    } // void Hand::draw(bool const update = true)

  /**
   **
   ** -> result
   **
   ** @param	position	Position of the hand
   **
   ** @return	upper left x position of the hand at 'position'
   ** 
   ** @version	0.6.1
   **
   ** @author	Diether Knof
   **
   **/
  int
    Hand::pos_x(Position const position) const
    {
      int value = 0; // return value

      switch(position) {
      case NORTH:
      case SOUTH:
	value = (this->table().border_x()
		 + this->width(WEST)
		 + this->margin_x(WEST)
		 + this->icongroup().width(WEST)
		 + this->icongroup().margin_x(WEST));
	break;
      case WEST:
	value = this->table().border_x();
	break;
      case EAST:
	value = (this->table().width() - this->table().border_x()
		 - this->width());
	break;
      } // switch(position)

      return value;
    } // int Hand::pos_x(Position const position)

  /**
   **
   ** -> result
   **
   ** @param	position	Position of the hand
   **
   ** @return	upper left y position of the hand at 'position'
   ** 
   ** @version	0.6.1
   **
   ** @author	Diether Knof
   **
   **/
  int
    Hand::pos_y(Position const position) const
    {
      int value = 0; // return value

      switch(position) {
      case NORTH:
	value = this->table().border_y();
	break;
      case SOUTH:
	value = (this->table().height()
		 - this->table().border_y()
		 - this->height());
	break;
      case WEST:
      case EAST:
	value = (this->table().border_y()
		 + this->height(NORTH)
		 + this->margin_y(NORTH)
		 + this->icongroup().height(NORTH)
		 + this->icongroup().margin_y(NORTH));
	break;
      } // switch(position)

      return value;
    } // int Hand::pos_y(Position const position)

  /**
   **
   ** -> result
   **
   ** @param	position	Position of the hand
   **
   ** @return	width of the hand at 'position'
   ** 
   ** @version	0.6.1
   **
   ** @author	Diether Knof
   **
   **/
  int
    Hand::width(Position const position) const
    {
      int value = 0; // return value

      switch(position) {
      case NORTH:
      case SOUTH:
	value = (this->table().width()
		 - this->table().border_x()
		 - this->width(WEST)
		 - this->margin_x(WEST)
		 - this->icongroup().width(WEST)
		 - this->icongroup().margin_x(WEST)
		 - this->table().border_x()
		 - this->width(EAST)
		 - this->margin_x(EAST)
		 - this->icongroup().width(EAST)
		 - this->icongroup().margin_x(EAST));

      if (value
	  < static_cast<int>(this->ui->cards->width(this->rotation(position))))
	value = this->ui->cards->width(this->rotation(position));

	break;
      case WEST:
      case EAST:
	value = this->ui->cards->width(this->rotation(position));
	break;
      } // switch(position)

      return value;
    } // int Hand::width(Position const position)

  /**
   **
   ** -> result
   **
   ** @param	position	Position of the hand
   **
   ** @return	height of the hand at 'position'
   ** 
   ** @version	0.6.1
   **
   ** @author	Diether Knof
   **
   **/
  int
    Hand::height(Position const position) const
    {
      int value = 0; // return value

      switch(position) {
      case NORTH:
      case SOUTH:
	value = this->ui->cards->height(this->rotation(position));
	break;
      case WEST:
      case EAST:
	value = (this->table().height()
		 - this->table().border_y()
		 - this->height(NORTH)
		 - this->margin_y(NORTH)
		 - this->icongroup().height(NORTH)
		 - this->icongroup().margin_y(NORTH)
		 - this->table().border_y()
		 - this->height(SOUTH)
		 - this->margin_y(SOUTH)
		 - this->icongroup().height(SOUTH)
		 - this->icongroup().margin_y(SOUTH));

      if (value
	  < static_cast<int>(this->ui->cards->height(this->rotation(position))))
	value = this->ui->cards->height(this->rotation(position));

	break;
      } // switch(position)
      
      return value;
    } // int Hand::height(Position const position)

  /**
   **
   ** -> result
   **
   ** @param	position	Position of the hand
   **
   ** @return	starting x position of the hand at 'position'
   ** 
   ** @version	0.6.1
   **
   ** @author	Diether Knof
   **
   **/
  int
    Hand::start_x(Position const position) const
    {
      return this->pos_x(position);
    } // int Hand::start_x(Position const position)

  /**
   **
   ** -> result
   **
   ** @param	position	Position of the hand
   **
   ** @return	starting y position of the hand at 'position'
   ** 
   ** @version	0.6.1
   **
   ** @author	Diether Knof
   **
   **/
  int
    Hand::start_y(Position const position) const
    {
      return this->pos_y(position);
    } // int Hand::start_y(Position const position)

  /**
   **
   ** -> result
   **
   ** @param	position	Position of the hand
   **
   ** @return	the x margin at 'position
   ** 
   ** @version	0.6.1
   **
   ** @author	Diether Knof
   **
   **/
  int
    Hand::margin_x(Position const position) const
    {
      int value = 0;

      switch(position) {
      case NORTH:
      case SOUTH:
	value = (this->ui->cards->height() / 10);
	break;
      case WEST:
      case EAST:
	value = (this->ui->cards->height() / 10);
	break;
      } // switch(position)

      return value;
    } // int Hand::margin_x(Position const position)

  /**
   **
   ** -> result
   **
   ** @param	position	Position of the hand
   **
   ** @return	the y margin at 'position
   ** 
   ** @version	0.6.1
   **
   ** @author	Diether Knof
   **
   **/
  int
    Hand::margin_y(Position const position) const
    {
      int value = 0;

      switch(position) {
      case NORTH:
      case SOUTH:
	value = (this->ui->cards->height() / 10);
	break;
      case WEST:
      case EAST:
	value = (this->ui->cards->height() / 10);
	break;
      } // switch(position)

      return value;
    } // int Hand::margin_y(Position const position)

  /**
   **
   ** -> result
   **
   ** @param	-
   **
   ** @return	upper left x position of the hand
   ** 
   ** @version	0.6.1
   **
   ** @author	Diether Knof
   **
   **/
  int
    Hand::pos_x() const
    {
      return this->pos_x(this->position());
    } // int Hand::pos_x()

  /**
   **
   ** -> result
   **
   ** @param	-
   **
   ** @return	upper left y position of the hand
   ** 
   ** @version	0.6.1
   **
   ** @author	Diether Knof
   **
   **/
  int
    Hand::pos_y() const
    {
      return this->pos_y(this->position());
    } // int Hand::pos_y()

  /**
   **
   ** -> result
   **
   ** @param	-
   **
   ** @return	width of the hand
   ** 
   ** @version	0.6.1
   **
   ** @author	Diether Knof
   **
   **/
  int
    Hand::width() const
    {
      return this->width(this->position());
    } // int Hand::width()

  /**
   **
   ** -> result
   **
   ** @param	-
   **
   ** @return	height of the hand
   ** 
   ** @version	0.6.1
   **
   ** @author	Diether Knof
   **
   **/
  int
    Hand::height() const
    {
      return this->height(this->position());
    } // int Hand::height()

  /**
   **
   ** -> result
   **
   ** @param	-
   **
   ** @return	starting x position of the hand
   ** 
   ** @version	0.6.1
   **
   ** @author	Diether Knof
   **
   **/
  int
    Hand::start_x() const
    {
      return this->start_x(this->position());
    } // int Hand::start_x()

  /**
   **
   ** -> result
   **
   ** @param	-
   **
   ** @return	starting y position of the hand
   ** 
   ** @version	0.6.1
   **
   ** @author	Diether Knof
   **
   **/
  int
    Hand::start_y() const
    {
      return this->start_y(this->position());
    } // int Hand::start_y()

  /**
   **
   ** -> result
   **
   ** @param	-
   **
   ** @return	x step of the cards
   ** 
   ** @version	0.6.1
   **
   ** @author	Diether Knof
   **
   **/
  double
    Hand::cards_step_x() const
    {
      if (this->player->hand().cardsnumber_all() == 0)
	return 0;

      double value = 0;

      switch(this->position()) {
      case NORTH:
      case SOUTH:
	value = ((this->width(this->position())
		  - this->ui->cards->width(this->rotation(this->position())))
		 / static_cast<double>(this->player->hand().cardsnumber_all()
				       - 1));
	break;
      case WEST:
      case EAST:
	value = 0;
	break;
      } // switch(this->position())

      return value;
    } // double Hand::cards_step_x()

  /**
   **
   ** -> result
   **
   ** @param	-
   **
   ** @return	y step of the cards
   ** 
   ** @version	0.6.1
   **
   ** @author	Diether Knof
   **
   **/
  double
    Hand::cards_step_y() const
    {
      if (this->player->hand().cardsnumber_all() <= 1)
	return 0;

      double value = 0;

      switch(this->position()) {
      case NORTH:
      case SOUTH:
	value = 0;
	break;
      case WEST:
      case EAST:
	value = ((this->height(this->position())
		  - this->ui->cards->height(this->rotation(this->position())))
		 / static_cast<double>(this->player->hand().cardsnumber_all()
				       - 1));
	break;
      } // switch(this->position())

      return value;
    } // double Hand::cards_step_y()

  /**
   **
   ** -> result
   **
   ** @param	-
   **
   ** @return	the horizontal margin of the hand (to the center of the table)
   ** 
   ** @version	0.6.1
   **
   ** @author	Diether Knof
   **
   **/
  int
    Hand::margin_x() const
    {
      return this->margin_x(this->position());
    } // int Hand::margin_x()

  /**
   **
   ** -> result
   **
   ** @param	-
   **
   ** @return	the vertical margin of the hand (to the center of the table)
   ** 
   ** @version	0.6.1
   **
   ** @author	Diether Knof
   **
   **/
  int
    Hand::margin_y() const
    {
      return this->margin_y(this->position());
    } // int Hand::margin_y()

  /**
   **
   ** -> result
   **
   ** @param	x	x position
   ** @param	y	y position
   **
   ** @return	the number of the card at the given position,
   **		UINT_MAX, if there is no card
   ** 
   ** @version	0.6.1
   **
   ** @author	Diether Knof
   **
   ** @todo	check for transparency
   **
   **/
  unsigned
    Hand::cardno_at_position(int const x, int const y) const
    {
      if ((x >= this->start_x())
	  && (x < this->start_x() + this->width())
	  && (y >= this->start_y())
	  && (y < this->start_y() + this->height())
	 ) {
	for (unsigned cb = this->player->sorted_hand().cardsnumber_all();
	     cb > 0;
	     cb--) {
	  unsigned const c = cb - 1;
	  if (!this->player->sorted_hand().played(c)) {
	    int pos_x = (this->start_x() +
			 static_cast<int>(this->cards_step_x() * c));
	    int pos_y = (this->start_y() +
			 static_cast<int>(this->cards_step_y() * c));

	    // emphasize the valid cards
	    if (::setting(Setting::EMPHASIZE_VALID_CARDS)
		&& (::game_status == GAMESTATUS::GAME_PLAY)
		&& (this->player->game().player_current() == *this->player)
		&& (this->player->type() == Player::HUMAN)
		&& (this->player->game().trick_current().isvalid(this->player->sorted_hand().card_all(c), this->player->hand()))
	       ) {
	      switch (this->player->no()) {
	      case 0:
		pos_y -= (this->margin_y() * 2 / 3);
		break;
	      case 1:
		pos_x += (this->margin_x() * 2 / 3);
		break;
	      case 2:
		pos_y += (this->margin_y() * 2 / 3);
		break;
	      case 3:
		pos_x -= (this->margin_x() * 2 / 3);
		break;
	      default:
		DEBUG_ASSERTION(false,
				"Hand::cardno_at_position():\n"
				"  player->no not valid: "
				<< this->player->no());
		break;
	      } // switch (this->player->no())
	    } // if (emphasize valid card)
	    if ((x >= pos_x)
		&& (x < (pos_x
			 + static_cast<int>(this->ui->cards->width(this->rotation()))))
		&& (y >= pos_y)
		&& (y < (pos_y
			 + static_cast<int>(this->ui->cards->height(this->rotation()))))
	       )
	      // the position is over the card
	      // ToDo: check for transparency
	      return c;
	  }
	}
      } // if (position over hand)

      return UINT_MAX;
    } // unsigned Hand::cardno_at_position(int const x, int const y) const

  /**
   **
   ** -> result
   **
   ** @param	x	x position
   ** @param	y	y position
   **
   ** @return	the card at the given position
   **		Card(), if there is no card
   ** 
   ** @version	0.6.5
   **
   ** @author	Diether Knof
   **
   **/
  Card
    Hand::card_at_position(int const x, int const y) const
    {
      unsigned const cardno = this->cardno_at_position(x, y);

      if (cardno == UINT_MAX)
	return Card();

      return this->player->sorted_hand().card_all(cardno);
    } // Card Hand::card_at_position(int const x, int const y) const

} // namespace UI_GTKMM_NS

#endif // #ifdef USE_UI_GTKMM
