/**********************************************************************
 *
 *   FreeDoko a Doppelkopf-Game
 *    
 *   Copyright (C) 2001-2005  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 "players_db.h"

#include "../../party/party.h"
#include "../../game/game.h"
#include "../../player/player.h"
#include "../../card/trick.h"
#include "../../os/bug_report_replay.h"

#include <gtkmm/main.h>

extern OS_NS::BugReportReplay* bug_report_replay;

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)
    {
      this->main_window->menu->announcements_update();
      this->main_window->menu->language_update();
      this->table->marriage(bridegroom, bride);

      this->thrower.throw_it();

      return ;
    } // 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()
    {
      Player const* const partner
	= this->table->select_genscher_partner(::party.game().player_current());

      this->thrower.throw_it();

      return 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)
    {
      this->main_window->menu->announcements_update();
      this->table->genscher(genscher, partner);

      this->thrower.throw_it();

      return ;
    } // 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
   **
   **/
  HandCard
    UI_GTKMM::card_get(Player const& player)
    {
      this->table->card_get(player);

      this->main_window->menu->card_get();
      ::party.game().player(player.no()).hand().forget_request();

      try {
	if (::setting(Setting::AUTOMATIC_CARD_SUGGESTION)
	    || ::bug_report_replay)
	  this->table->show_card_suggestion(false);

	do {
	  while (!this->ui->thrower
		 && (player.hand().requested_position() == UINT_MAX)
		 && (::game_status == GAMESTATUS::GAME_PLAY))
	    Gtk::Main::iteration();
	  if (this->ui->thrower) {
	    try {
	      this->thrower.throw_it();
	    } catch (Card const& card) {
	      DEBUG_ASSERTION((card == player.hand().requested_card()),
			      "UI_GTKMM::card_get(player):\n"
			      "  did not catch the requested card:\n"
			      "  " << card << " != " << player.hand().requested_card()
			     );
	    } // try
	  } // if (this->ui->thrower)

#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);
      } catch (...) {
	this->table->card_got();
	this->main_window->menu->card_got();
	throw;
      } // catch(...)

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

      return player.hand().requested_card();
    } // HandCard UI_GTKMM::card_get(Player const& player)

  /**
   **
   ** the given player has played 'card.
   ** remove the card from the hand
   **
   ** @param	card	the played card
   **
   ** @return	-
   **
   ** @version	0.6.1
   **
   ** @author	Diether Knof
   **
   **/
  void
    UI_GTKMM::card_played(HandCard const& card)
    { 
      Player const& player = card.player();

      // for the sensitivity of the swines button
      this->main_window->menu->announcements_update();

      // for updating the heuristics
      this->players_db->update_db();

      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->card_played(player);
      this->update();

      this->thrower.throw_it();

      return ;
    } // void UI_GTKMM::card_player(HandCard const& card)

  /**
   **
   ** 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)
    {
      this->main_window->menu->announcements_update();
      this->table->announcement_made(announcement, player);

      this->thrower.throw_it();

      return ;
    } // void UI_GTKMM::announcement_made(Announcement const& announcement, Player const& player)

} // namespace UI_GTKMM_NS

#endif // #ifdef USE_UI_GTKMM
