/**********************************************************************
 *
 *   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 "ui.h"

#include "table.h"
#include "trick.h"
#include "hand.h"
#include "name.h"
#include "main_window.h"
#include "menu.h"

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

#include <gtkmm/main.h>

namespace UI_GTKMM_NS {

  /**
   **
   ** marriage: Information of the new team
   **
   ** @param	bridegroom	the player with the marriage
   ** @param	bride		the bride
   **
   ** @return	-
   **
   ** @author	Diether Knof
   **
   ** @version	0.6.1
   **
   **/
  void
    UI_GTKMM::marriage(Player const& bridegroom,
		       Player const& bride)
    {
      DEBUG_CALLING(INFO_UI && INFO_GTKMM_UI,
		    "UI_GTKMM::marriage(bridegroom, bride)");

      this->main_window->menu->announcements_update();
      this->main_window->menu->language_update();
      this->table->marriage(bridegroom, bride);

      DEBUG_RETURNING(VOID,
		      INFO_UI && INFO_GTKMM_UI,
		      "UI_GTKMM::marriage(bridegroom, bride)");
    } // void UI_GTKMM::marriage(Player const& bridegroom, Player const& bride)

  /**
   **
   ** -> result
   **
   ** @param	-
   **
   ** @return	selected partner of the player
   **		(NULL if the genscher is not announced)
   **
   ** @author	Diether Knof
   **
   ** @version	0.6.1
   **
   **/
  Player const*
    UI_GTKMM::genscher_partner()
    {
      DEBUG_CALLING(INFO_UI && INFO_GTKMM_UI,
		    "UI_GTKMM::genscher_partner()");

      DEBUG_RETURNING(this->table->select_genscher_partner(::party.game().player_current()),
		      INFO_UI && INFO_GTKMM_UI,
		      "UI_GTKMM::genscher_partner()");
    } // Player const* UI_GTKMM::genscher_partner()

  /**
   **
   ** genscher: Information of the new team
   **
   ** @param	player	genscher player
   ** @param	partner	partner of the gensher player
   **
   ** @return	-
   **
   ** @author	Diether Knof
   **
   ** @version	0.6.1
   **
   **/
  void
    UI_GTKMM::genscher(Player const& genscher,
		       Player const& partner)
    {
      DEBUG_CALLING(INFO_UI && INFO_GTKMM_UI,
		    "UI_GTKMM::genscher(genscher, partner)");

      this->main_window->menu->announcements_update();
      this->table->genscher(genscher, partner);

      DEBUG_RETURNING(VOID,
		      INFO_UI && INFO_GTKMM_UI,
		      "UI_GTKMM::genscher(genscher, partner)");
    } // void UI_GTKMM::genscher(Player const& genscher, Player const& partner)

  /**
   **
   ** let the user select a card for the given player
   **
   ** @param	player	the player who is to play a card
   **
   ** @return	the selected card
   **
   ** @version	0.6.1
   **
   ** @author	Diether Knof
   **
   **/
  Card
    UI_GTKMM::card_get(Player const& player)
    {
      DEBUG_CALLING(INFO_GTKMM_UI,
		    "UI_GTKMM::card_get(player)");

      this->table->mouse_cursor_update();

      this->main_window->menu->card_get();
	::party.game().player(player.no()).hand().forget_request();
      if (::setting(Setting::AUTOMATIC_CARD_SUGGESTION))
	this->table->show_card_suggestion();

      do {
	while ((player.hand().requested_position() == UINT_MAX)
	       && ((::game_status == GAMESTATUS::GAME_PLAY)
		   || (::game_status == GAMESTATUS::GAME_SHOW_LAST_TRICK)))
	  Gtk::Main::iteration();

	if ((::game_status != GAMESTATUS::GAME_PLAY)
	    && (::game_status != GAMESTATUS::GAME_SHOW_LAST_TRICK))
	  break;

#ifndef OUTDATED
	// the player cannot play an invalid card
	if (!player.game().trick_current().isvalid(player.hand().requested_card(),
						   player.hand())) {
	  ::party.game().player(player.no()).hand().forget_request();
	  continue;
	}
#endif

      } while (player.hand().requested_position() == UINT_MAX);

      this->main_window->menu->card_got();
      this->table->card_got();

      DEBUG_RETURNING(player.hand().requested_card(),
		      INFO_GTKMM_UI,
		      "UI_GTKMM::card_get(player)");
    } // Card UI_GTKMM::card_get(Player const& player)

  /**
   **
   ** the given player has played 'card.
   ** remove the card from the hand
   **
   ** @param	card	the played card
   ** @param	player	the player who has played the card
   **
   ** @return	-
   **
   ** @version	0.6.1
   **
   ** @author	Diether Knof
   **
   **/
  void
    UI_GTKMM::card_played(Card const& card, Player const& player)
    { 
      DEBUG_CALLING(INFO_GTKMM_UI,
		    "UI_GTKMM::played_card(player, card)");

      this->table->hand(player).draw(true);
      this->table->name(player).draw(true);
      this->table->trick().draw(true);
      this->table->name(player.game().player_current()).draw(true);

      this->table->mouse_cursor_update();
      this->update();

      DEBUG_RETURNING(VOID,
		      INFO_GTKMM_UI,
		      "UI_GTKMM::played_card(player, card)");
    } // void UI_GTKMM::card_player(Card const& card, Player const& player)

  /**
   **
   ** the given player has made the announcement 'announcement'
   **
   ** @param	announcement	the annoucnement
   ** @param	player		the player who has made the announcement
   **
   ** @return	-
   **
   ** @version	0.6.1
   **
   ** @author	Diether Knof
   **
   **/
  void
    UI_GTKMM::announcement_made(Announcement const& announcement,
				Player const& player)
    {
      DEBUG_CALLING(INFO_UI && INFO_GTKMM_UI,
		    "UI_GTKMM::announcement_made(announcement, player)");

      this->main_window->menu->announcements_update();
      this->table->announcement_made(announcement, player);

      DEBUG_RETURNING(VOID,
		      INFO_UI && INFO_GTKMM_UI,
		      "UI_GTKMM::announcement_made(announcement, player)");
    } // void UI_GTKMM::announcement_made(Announcement const& announcement, Player const& player)

} // namespace UI_GTKMM_NS

#endif // #ifdef USE_UI_GTKMM
