/**********************************************************************
 *
 *   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 "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 {

  // position of the window
  int CardSuggestion::position_x = INT_MAX;
  int CardSuggestion::position_y = INT_MAX;

  /**
   **
   ** Constructor
   **
   ** @param	table		the table
   ** @param	player		the player whose turn it is
   **
   **
   ** @return	-
   ** 
   ** @author	Diether Knof
   **
   ** @version	0.6.1
   **
   **/
  CardSuggestion::CardSuggestion(Table* const table,
				 Player const& player) :
    GameinfoDialog(table),
  player(&player),
  wait_text(NULL),
  card_box(NULL),
  card_image(NULL),
  rationals_text(NULL),
  play_button(NULL),
  suggested_card()
  {
    DEBUG_CALLING(INFO_GTKMM_CARD_SUGGESTION && INFO_GTKMM_INIT,
		  "CardSuggestion::CardSuggestion(table, player)");

    DEBUG_RETURNING(VOID,
		    INFO_GTKMM_CARD_SUGGESTION && INFO_GTKMM_INIT,
		    "CardSuggestion::CardSuggestion(table, player)");
  } // CardSuggestion::CardSuggestion(Table* const table, Player const& player)

  /**
   **
   ** Destruktor
   **
   ** @param	-
   **
   ** @return	-
   ** 
   ** @author	Diether Knof
   **
   ** @version	0.6.1
   **
   **/
  CardSuggestion::~CardSuggestion()
  {
    DEBUG_CALLING(INFO_GTKMM_CARD_SUGGESTION && INFO_GTKMM_INIT,
		  "CardSuggestion::~CardSuggestion()");

    DEBUG_RETURNING(VOID,
		    INFO_GTKMM_CARD_SUGGESTION && INFO_GTKMM_INIT,
		    "CardSuggestion::~CardSuggestion()");
  } // CardSuggestion::~CardSuggestion()

  /**
   **
   ** init the dialog
   **
   ** @param	-
   **
   ** @return	-
   ** 
   ** @author	Diether Knof
   **
   ** @version	0.6.1
   **
   **/
  void
    CardSuggestion::init()
    {
      DEBUG_CALLING(INFO_GTKMM_CARD_SUGGESTION && INFO_GTKMM_INIT,
		    "CardSuggestion::init()");

      this->ui->add_for_translation(*this,
				    ::translator("Suggested card"));

      this->play_button
	= Gtk::manage(new Gtk::StockButton(Gtk::Stock::YES, "play card"));
      this->ui->add_for_translation(*(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 = Gtk::manage(new Gtk::Image());
      this->card_box->add(*(this->card_image));
      this->cards_update();
      this->rationals_text = Gtk::manage(new Gtk::Label("Rational"));
      this->ui->add_for_translation(*(this->rationals_text),
				    ::translator("Rational"));
      this->wait_text = Gtk::manage(new Gtk::Label("Thinking, please wait."));
      this->ui->add_for_translation(*(this->wait_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->get_vbox()->add(*(this->wait_text));

      this->show_all_children();
      this->rationals_text->hide();

      DEBUG_RETURNING(VOID,
		      INFO_GTKMM_CARD_SUGGESTION && INFO_GTKMM_INIT,
		      "CardSuggestion::init()");
    } // void CardSuggestion::init()

  /**
   **
   ** show the window
   **
   ** @param	-
   **
   ** @return	-
   ** 
   ** @author	Diether Knof
   **
   ** @version	0.6.1
   **
   **/
  void
    CardSuggestion::show_information()
    {
      DEBUG_CALLING(INFO_GTKMM_CARD_SUGGESTION && INFO_GTKMM_GAMEPLAY,
		    "CardSuggestion::show_information()");

      this->move_to_last_position();
      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() == *(this->player),
		      "CardSuggestion::show_information():\n"
		      "  ::party.game().player_current() != *(this->player)");

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

      //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->cards_update();
      this->wait_text->hide();
      this->rationals_text->show();
      this->ui->add_for_translation(*(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->save_position();
      this->hide();

      DEBUG_RETURNING(VOID,
		      INFO_GTKMM_CARD_SUGGESTION && INFO_GTKMM_GAMEPLAY,
		      "CardSuggestion::show_information()");
    } // void CardSuggestion::show_information()

  /**
   **
   ** play the suggested card
   **
   ** @param	-
   **
   ** @return	-
   ** 
   ** @author	Diether Knof
   **
   ** @version	0.6.1
   **
   **/
  void
    CardSuggestion::play_card()
    {
      DEBUG_CALLING(INFO_GTKMM_CARD_SUGGESTION && INFO_GTKMM_GAMEPLAY,
		    "CardSuggestion::play_card()");

      if (!this->suggested_card)
	DEBUG_RETURNING(VOID,
			INFO_GTKMM_CARD_SUGGESTION && INFO_GTKMM_GAMEPLAY,
			"CardSuggestion::play_card()");

      // ToDo: play the suggested card
      DEBUG_ASSERTION(::party.game().player_current() == *(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();
      this->ui->table->card_got();

      DEBUG_RETURNING(VOID,
		      INFO_GTKMM_CARD_SUGGESTION && INFO_GTKMM_GAMEPLAY,
		      "CardSuggestion::play_card()");
    } // 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*)
    {
      DEBUG_CALLING(INFO_GTKMM_CARD_SUGGESTION && INFO_GTKMM_GAMEPLAY,
		    "CardSuggestion::play_card_signal(GdkEventButton*)");

      if (!this->suggested_card)
	DEBUG_RETURNING(false,
			INFO_GTKMM_CARD_SUGGESTION && INFO_GTKMM_GAMEPLAY,
			"CardSuggestion::play_card_signal(GdkEventButton*)");

      this->play_card();

      DEBUG_RETURNING(true,
		      INFO_GTKMM_CARD_SUGGESTION && INFO_GTKMM_GAMEPLAY,
		      "CardSuggestion::play_card_signal(GdkEventButton*)");
    } // bool CardSuggestion::play_card_signal(GdkEventButton*)

  /**
   **
   ** update the card
   **
   ** @param	-
   **
   ** @return	-
   ** 
   ** @author	Diether Knof
   **
   k** @version	0.6.1
   **
   **/
  void
    CardSuggestion::cards_update()
    {
      DEBUG_CALLING(INFO_GTKMM_CARD_SUGGESTION && INFO_GTKMM_UPDATE,
		    "CardSuggestion::cards_update()");

      if (this->suggested_card)
	this->card_image->set(this->ui->cards->card(this->suggested_card));
      else
	this->card_image->set(this->ui->cards->back());

      this->card_image->set_padding(this->ui->cards->height() / 5,
				    this->ui->cards->height() / 5);


      DEBUG_RETURNING(VOID,
		      INFO_GTKMM_CARD_SUGGESTION && INFO_GTKMM_UPDATE,
		      "CardSuggestion::cards_update()");
    } // void CardSuggestion::cards_update()

  /**
   **
   ** save the position of the window
   **
   ** @param	-
   **
   ** @return	-
   **
   ** @version	0.6.4
   **
   ** @author	Diether Knof
   **
   **/
  void
    CardSuggestion::save_position()
    {
      // Note: 'Gtk::Window::get_position()' does not give the actual position
      this->get_window()->get_root_origin(this->position_x, this->position_y);

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

  /**
   **
   ** move the window to the last position
   **
   ** @param	-
   **
   ** @return	-
   **
   ** @version	0.6.4
   **
   ** @author	Diether Knof
   **
   **/
  void
    CardSuggestion::move_to_last_position()
    {
      if (this->position_x != INT_MAX)
	this->move(this->position_x, this->position_y);

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

} // namespace UI_GTKMM_NS

#endif // #ifdef USE_UI_GTKMM
