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

#include "../../party/party.h"
#include "../../game/game.h"
#include "../../player/player.h"
#include "../../card/trick.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)
    {
      this->main_window->menu->announcements_update();
      this->main_window->menu->language_update();
      this->table->marriage(bridegroom, bride);

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

      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
   **
   **/
  Card
    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();
      if (::setting(Setting::AUTOMATIC_CARD_SUGGESTION))
	this->table->show_card_suggestion();

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

	if (::game_status != GAMESTATUS::GAME_PLAY)
	  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();

      return player.hand().requested_card();
    } // 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)
    { 

      // 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();

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

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

} // namespace UI_GTKMM_NS

#endif // #ifdef USE_UI_GTKMM
