/**********************************************************************
 *
 *   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 "card_suggestion.h"
#include "table.h"
#include "ui.h"
#include "translations.h"
#include "cards.h"

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

#include "widgets/stock_button.h"
#include <gtkmm/stock.h>
#include <gtkmm/alignment.h>
#include <gtkmm/box.h>
#include <gtkmm/eventbox.h>
#include <gtkmm/image.h>
#include <gtkmm/label.h>
#include <gtkmm/main.h>
#include <gdk/gdkkeysyms.h>

#ifdef WORKAROUND
// else I get
//   /usr/include/gtkmm-2.0/glibmm/refptr.h:176: error: `unreference' undeclared
#include <gtkmm/menu.h>
#endif

namespace UI_GTKMM_NS {

  /**
   **
   ** Constructor
   **
   ** @param	table		the table
   **
   ** @return	-
   ** 
   ** @author	Diether Knof
   **
   ** @version	0.6.6
   **
   **/
  CardSuggestion::CardSuggestion(Table* const table) :
    GameinfoDialog(table),
  player(NULL),
  card_box(NULL),
  card_image(NULL),
  rationals_text(NULL),
  play_button(NULL),
  suggested_card()
  {
    return ;
  } // CardSuggestion::CardSuggestion(Table* const table)

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

  /**
   **
   ** init the dialog
   **
   ** @param	-
   **
   ** @return	-
   ** 
   ** @author	Diether Knof
   **
   ** @version	0.6.1
   **
   **/
  void
    CardSuggestion::init()
    {
      this->ui->translations->add(*this,
				    ::translator("Suggested card"));

      this->play_button
	= Gtk::manage(new Gtk::StockButton(Gtk::Stock::YES, "play card"));
      this->ui->translations->add(*(this->play_button),
				    ::translator("play card"));
      this->add_action_widget(*(this->play_button), Gtk::RESPONSE_NONE);
      this->play_button->signal_clicked().connect(SigC::slot(*this, &CardSuggestion::play_card));

      this->play_button->add_accelerator("activate",
					 this->get_accel_group(),
					 GDK_t,
					 Gdk::ModifierType(0),
					 Gtk::AccelFlags(0));

      this->GameinfoDialog::init();
      this->get_vbox()->set_spacing(20);

      this->card_box = Gtk::manage(new Gtk::EventBox());
      this->card_image = this->ui->cards->new_managed_image();
      this->card_box->add(*(this->card_image));
      this->card_image->set_padding(2 em, 2 ex);
      this->rationals_text = Gtk::manage(new Gtk::Label("Thinking, please wait."));
      this->ui->translations->add(*(this->rationals_text),
				    ::translator("Thinking, please wait."));

      Gtk::Alignment* alignment
	= Gtk::manage(new Gtk::Alignment(Gtk::ALIGN_CENTER, Gtk::ALIGN_CENTER,
					 0, 0));
      alignment->add(*(this->card_box));
      this->get_vbox()->add(*alignment);
      this->get_vbox()->add(*(this->rationals_text));

      this->show_all_children();

      return ;
    } // void CardSuggestion::init()

  /**
   **
   ** show the window
   **
   ** @param	player		the player whose turn it is
   **
   ** @return	-
   ** 
   ** @author	Diether Knof
   **
   ** @version	0.6.6
   **
   **/
  void
    CardSuggestion::show_information(Player const& player)
    {
      this->player = &player;
      if (!this->is_realized())
	this->realize();

      this->ui->translations->change(*(this->rationals_text),
				   ::translator("Thinking, please wait."));
      this->ui->cards->change_managed(this->card_image, Card());

      this->present();
      this->play_button->set_sensitive(false);

      DEBUG_ASSERTION((dynamic_cast<Ai const*>(this->player) != NULL),
		      "CardSuggestion::show_information():\n"
		      "  'this->player' cannot be cast to an ai.");
      DEBUG_ASSERTION(::party.game().player_current().is_same(*(this->player)),
		      "CardSuggestion::show_information():\n"
		      "  ::party.game().player_current() != *(this->player)");

      this->suggested_card
 	= dynamic_cast<Ai&>(::party.game().player_current()).card_suggestion();

      //this->card_box->set_events(Gdk::BUTTON_PRESS_MASK);
      this->card_box->signal_button_press_event().connect(SigC::slot(*this, &CardSuggestion::play_card_signal));

      this->ui->cards->change_managed(this->card_image, this->suggested_card);
      this->ui->translations->change(*(this->rationals_text),
				   ::translator(::name(dynamic_cast<Ai const*>(this->player)->lastHeuristic_)));

      this->play_button->set_sensitive(true);

      while ((::game_status & GAMESTATUS::GAME)
	     && (this->is_visible()))
	Gtk::Main::iteration();

      this->hide();

      this->ui->cards->change_managed(this->card_image, Card());
      this->player = NULL;

      return ;
    } // void CardSuggestion::show_information(Player const& player)

  /**
   **
   ** play the suggested card
   **
   ** @param	-
   **
   ** @return	-
   ** 
   ** @author	Diether Knof
   **
   ** @version	0.6.1
   **
   **/
  void
    CardSuggestion::play_card()
    {
      if (!this->suggested_card)
	return ;

      DEBUG_ASSERTION(::party.game().player_current().is_same(*(this->player)),
		      "CardSuggestion::play_card():\n"
		      "  ::party.game().player_current() != *(this->player)");

      const_cast<Player*>(this->player)->hand().request_card(this->suggested_card);

      this->hide();

      return ;
    } // void CardSuggestion::play_card()

  /**
   **
   ** play the suggested card
   **
   ** @param	-
   **
   ** @return	whether the card can be played
   ** 
   ** @author	Diether Knof
   **
   ** @version	0.6.1
   **
   **/
  bool
    CardSuggestion::play_card_signal(GdkEventButton*)
    {
      if (!this->suggested_card)
	return false;

      this->play_card();

      return true;
    } // bool CardSuggestion::play_card_signal(GdkEventButton*)

} // namespace UI_GTKMM_NS

#endif // #ifdef USE_UI_GTKMM
