/**********************************************************************
 *
 *   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_GTK

#include "hands.h"
#include "gtk_gui.h"
#include "card.h"
#include "table.h"

#include "../../player/player.h"
#include "../../game/game.h"
#include "../../party/party.h"

namespace GTK_DOKO {

  // part of all hands
#define HANDS_MARGIN_LEFT int(0.2 * this->gui().cards().width(const_cast<Game const&>(::party.game()).player(1)))
#define HANDS_MARGIN_RIGHT int(0.2 * this->gui().cards().width(const_cast<Game const&>(::party.game()).player(3)))
#define HANDS_MARGIN_TOP int(0.2 * this->gui().cards().height(const_cast<Game const&>(::party.game()).player(2)))
#define HANDS_MARGIN_BOTTOM int(0.2 * this->gui().cards().height(const_cast<Game const&>(::party.game()).player(0)))

  /**
   *
   * constructor
   *
   * @param	table	the table, the hands belong to
   *
   * @return	-
   * 
   * @version	0.4.4
   *
   * @author	Diether Knof
   *
   * @bug		takes the players from 'party' and not from 'game'
   *
   */
  Handgs::Handgs(Table* table) :
    HTINBaseGroup(table)
    {
      DEBUG_CALLING(INFO_GTK_HAND && INFO_GTK_INIT,
		    "Handgs::Handgs()");

      for (unsigned p = 0; p < ::party.playerno(); p++)
	this->object_p.push_back(new Handg(this, ::party.player(p)));

      DEBUG_RETURNING(VOID,
		      INFO_GTK_HAND && INFO_GTK_INIT,
		      "Handgs::Handgs()");
    } // Handgs::Handgs()

  /**
   *
   * Handg& Handgs::hand(Player const& player)
   *
   * @param	player	the player, whose hand is returned
   *
   * @return	The hand of the given player
   * 
   * @version	0.4.4
   *
   * Description:	->result
   *
   * @author	Diether Knof
   *
   */
  Handg&
    Handgs::hand(Player const& player)
    {
      DEBUG_CALLING(INFO_GTK_HAND && INFO_GTK_VALUE,
		    "Handgs::hand(player)");

      DEBUG_RETURNING(static_cast<Handg&>(this->object(player)),
		      INFO_GTK_HAND && INFO_GTK_VALUE,
		      "Handgs::hand(player)");
    } // Handg& Handgs::hand(Player const& player)

  /**
   *
   * -> result
   *
   * @param	-
   *
   * @return	starting x position of the hands
   * 
   * @version	0.4.4
   *
   * @author	Diether Knof
   *
   */
  int
    Handgs::start_x()
    {
      DEBUG_CALLING(INFO_GTK_HAND && INFO_GTK_VALUE,
		    "Handgs::start_x()");

      DEBUG_RETURNING(HANDS_MARGIN_LEFT,
		      INFO_GTK_HAND && INFO_GTK_VALUE,
		      "Handgs::start_x()");
    } // Handgs::start_x()

  /**
   *
   * -> result
   *
   * @param	-
   *
   * @return	starting y position of the hands
   * 
   * @version	0.4.4
   *
   * @author	Diether Knof
   *
   */
  int
    Handgs::start_y()
    {
      DEBUG_CALLING(INFO_GTK_HAND && INFO_GTK_VALUE,
		    "Handgs::start_y()");

      DEBUG_RETURNING(HANDS_MARGIN_TOP,
		      INFO_GTK_HAND && INFO_GTK_VALUE,
		      "Handgs::start_y()");
    } // Handgs::start_y()

  /**
   *
   * -> result
   *
   * @param	-
   *
   * @return	width of the hands
   * 
   * @version	0.4.4
   *
   * @author	Diether Knof
   *
   */
  unsigned
    Handgs::width()
    {
      DEBUG_CALLING(INFO_GTK_HAND && INFO_GTK_VALUE,
		    "Handgs::width()");

      DEBUG_RETURNING(this->table().width()
		      - HANDS_MARGIN_RIGHT
		      - HANDS_MARGIN_LEFT,
		      INFO_GTK_HAND && INFO_GTK_VALUE,
		      "Handgs::width()");
    } // unsigned Handgs::width()

  /**
   *
   * -> result
   *
   * @param	-
   *
   * @return	height of the hands
   * 
   * @version	0.4.4
   *
   * @author	Diether Knof
   *
   */
  unsigned
    Handgs::height()
    {
      DEBUG_CALLING(INFO_GTK_HAND && INFO_GTK_VALUE,
		    "Handgs::height()");

      DEBUG_RETURNING(this->table().height()
		      - HANDS_MARGIN_TOP
		      - HANDS_MARGIN_BOTTOM,
		      INFO_GTK_HAND && INFO_GTK_VALUE,
		      "Handgs::height()");
    } // unsigned Handgs::height()

  /**
   *
   * bool Handgs::mouse_click(GdkEventButton* button_event)
   *
   * @param	button_event	- data of the button event
   *
   * @return	whether the click has been accepted
   * 
   * @version	0.4.4
   *
   * Description:	a mouse button is pressed:
   *		if it is a human's turn, pick the selected card
   *
   * @author	Diether Knof
   *
   */
  bool
    Handgs::mouse_click(GdkEventButton* button_event)
    {
      DEBUG_CALLING(INFO_GTK_HAND && INFO_GTK_EVENT,
		    "Handgs::mouse_click()");

      bool accepted = false;

      if ((::game_status == GAMESTATUS::GAME_PLAY)
	  || (::game_status == GAMESTATUS::GAME_POVERTY_SHIFT)) {
	// perhaps get a card
	Player const& player = ::party.game().player_current();
	if (player.type() & Player::HUMAN) {
	  // the player is a human and has to play a card
	  accepted = this->hand(player).mouse_click(button_event);
	} // if (player == HUMAN)
      } // if (game_status == ...)

      DEBUG_RETURNING(accepted,
		      INFO_GTK_HAND && INFO_GTK_EVENT,
		      "Handgs::mouse_click()");
    } // bool Handgs::mouse_click(GdkEventButton)

} // namespace GTK_DOKO
#endif // #ifdef USE_UI_GTK
