/**********************************************************************
 *
 *   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 "game_review.h"
#include "game_finished.h"
#include "trick_summary.h"

#include "ui.h"
#include "translations.h"
#include "table.h"

#include "../../game/game.h"
#include "../../card/trick.h"
#include "../../player/player.h"

#include "widgets/stock_button.h"
#include <gtkmm/alignment.h>
#include <gtkmm/stock.h>
#include <gdk/gdkkeysyms.h>

namespace UI_GTKMM_NS {

  /**
   **
   ** Constructor
   **
   ** @param	table	the table
   **
   ** @return	-
   ** 
   ** @author	Diether Knof
   **
   ** @version	0.6.7
   **
   **/
  GameReview::GameReview(GameFinished* const game_finished) :
    Base(game_finished),
    Gtk::StickyDialog("Game review", *game_finished, false, true),
    trickno_(UINT_MAX),
    previous_trick_button(NULL),
    following_trick_button(NULL),
    trick_summary(NULL),
  close_button(NULL)
  {
    this->signal_realize().connect(SigC::slot(*this, &GameReview::init));
    this->signal_show().connect(SigC::bind<bool>(SigC::slot(*this->ui->table, &Table::draw_all), true));
    this->signal_hide().connect(SigC::bind<bool>(SigC::slot(*this->ui->table, &Table::draw_all), true));

    return ;
  } // GameReview::GameReview(GameFinished* const game_finished)

  /**
   **
   ** Destruktor
   **
   ** @param	-
   **
   ** @return	-
   ** 
   ** @author	Diether Knof
   **
   ** @version	0.6.7
   **
   **/
  GameReview::~GameReview()
  {
    delete this->trick_summary;

    return ;
  } // GameReview::~GameReview()

  /**
   **
   ** create all subelements
   **
   ** @param	-
   **
   ** @return	-
   ** 
   ** @author	Diether Knof
   **
   ** @version	0.6.7
   **
   **/
  void
    GameReview::init()
    {
      this->ui->translations->add(*this,
				  ::translator("Game review"));


      this->get_vbox()->set_spacing(10);

      { // close button
      this->close_button
	= Gtk::manage(new Gtk::StockButton(Gtk::Stock::CLOSE, "close"));
      this->ui->translations->add(*(this->close_button),
				    ::translator("close"));
      this->add_action_widget(*this->close_button, Gtk::RESPONSE_CLOSE);

      this->close_button->grab_default();

      this->close_button->signal_clicked().connect(SigC::slot(*this, &Dialog::hide));
      } // close button

      { // navigation
	Gtk::HBox* navigation_container = Gtk::manage(new Gtk::HBox());
	this->previous_trick_button = Gtk::manage(new Gtk::Button());
	this->following_trick_button = Gtk::manage(new Gtk::Button());
	this->previous_trick_button->add(*(Gtk::manage(new Gtk::Image(Gtk::Stock::GO_BACK, Gtk::ICON_SIZE_BUTTON))));
	this->following_trick_button->add(*(Gtk::manage(new Gtk::Image(Gtk::Stock::GO_FORWARD, Gtk::ICON_SIZE_BUTTON))));
	navigation_container->pack_start(*(this->previous_trick_button),
					 false, true);
	navigation_container->pack_end(*(this->following_trick_button),
				       false, true);
	navigation_container->pack_start(*(Gtk::manage(new Gtk::Alignment())),
					 Gtk::PACK_EXPAND_WIDGET);

	this->previous_trick_button->signal_clicked().connect(SigC::slot(*this, &GameReview::previous_trick));
	this->following_trick_button->signal_clicked().connect(SigC::slot(*this, &GameReview::following_trick));

	this->get_vbox()->pack_start(*navigation_container, false, true);
      } // navigation

      this->trick_summary = new TrickSummary(this, false);

      this->get_vbox()->add(*this->trick_summary);

      this->show_all_children();

      this->signal_key_press_event().connect(SigC::slot(*this,
							&GameReview::key_press));

      if (this->game)
	this->set_trickno(0);

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

  /**
   **
   ** set the game
   **
   ** @param	game	game to review
   **
   ** @return	-
   ** 
   ** @author	Diether Knof
   **
   ** @version	0.6.7
   **
   **/
  void
    GameReview::set_game(Game const& game)
    {
      this->game = &game;

      if (this->is_realized())
	this->set_trickno(0);

      return ;
    } // void GameReview::set_game(Game const& game)

  /**
   **
   ** -> result
   **
   ** @param	-
   **
   ** @return	the trick number
   ** 
   ** @author	Diether Knof
   **
   ** @version	0.6.7
   **
   **/
  unsigned
    GameReview::trickno() const
    {
      return this->trickno_;
    } // unsigned GameReview::trickno() const

  /**
   **
   ** -> result
   **
   ** @param	-
   **
   ** @return	the current trick that is viewed
   ** 
   ** @author	Diether Knof
   **
   ** @version	0.6.7
   **
   **/
  ::Trick const&
    GameReview::trick() const
    {
      DEBUG_ASSERTION(this->game,
		      "GameReview::trickno():\n"
		      "  'this->game' is not set");

      DEBUG_ASSERTION((this->trickno() != UINT_MAX),
		      "GameReview::trickno():\n"
		      "  'this->trickno()' == UINT_MAX");

      return this->game->trick(this->trickno());
    } // ::Trick const& GameReview::trick() const

  /**
   **
   ** set the trickno
   **
   ** @param	trickno	new trickno
   **
   ** @return	-
   ** 
   ** @author	Diether Knof
   **
   ** @version	0.6.7
   **
   **/
  void
    GameReview::set_trickno(unsigned const trickno)
    {
      this->trickno_ = trickno;
      this->trick_summary->set_trick(this->game->trick(this->trickno()));
      this->previous_trick_button->set_sensitive(this->trickno() > 0);
      this->following_trick_button->set_sensitive(this->trickno()
						  < this->game->trickno() - 1);

      if (this->trickno() == this->game->trick_current_no() - 1)
	this->ui->translations->change(*this,
				       ::translator("Game review: %ttrick%",
						    ::translator("Last trick")));
      else
	this->ui->translations->change(*this,
				       ::translator("Game review: %ttrick%",
						    ::translator("%unumber%. trick", this->trickno() + 1)));

      this->ui->table->draw_all();

      return ;
    } // void GameReview::set_trickno(unsigned const trickno)

  /**
   **
   ** show the previous trickno
   **
   ** @param	-
   **
   ** @return	-
   ** 
   ** @author	Diether Knof
   **
   ** @version	0.6.7
   **
   **/
  void
    GameReview::previous_trick()
    {
      if (this->trickno() > 0)
	this->set_trickno(this->trickno() - 1);

      return ;
    } // void GameReview::previous_trick()

  /**
   **
   ** show the following trickno
   **
   ** @param	-
   **
   ** @return	-
   ** 
   ** @author	Diether Knof
   **
   ** @version	0.6.7
   **
   **/
  void
    GameReview::following_trick()
    {
      if (this->trickno() < this->game->trickno() - 1)
	this->set_trickno(this->trickno() + 1);

      return ;
    } // void GameReview::following_trick()

  /**
   **
   ** the name of 'player' has changed
   **
   ** @param	player	the player whose name has changed
   **
   ** @return	-
   ** 
   ** @author	Diether Knof
   **
   ** @version	0.6.7
   **
   **/
  void
    GameReview::name_changed(Player const& player)
    {
      if (this->game)
	this->trick_summary->update();

      return ;
    } // void GameReview::name_changed(Player const& player)

  /**
   **
   ** a key has been pressed
   **
   ** @param	key	the key
   **
   ** @return	from 'ui->key_press(key)'
   **
   ** @version	0.6.7
   **
   ** @author	Diether Knof
   **
   ** @bug	signal does not get here for '->', '<-' and '<Esc>'
   **
   **/
  bool
    GameReview::key_press(GdkEventKey* key)
    {
      // whether the key was accepted
      bool accepted = false;

      if ((key->state & GDK_SHIFT_MASK) == 0) {
	switch (key->keyval) {
	case GDK_Left: // show previous trick
	  this->previous_trick();
	  accepted = true;
	  break;
	case GDK_Right: // show following trick
	  this->following_trick();
	  accepted = true;
	  break;
	case GDK_Escape: // close the window
	  this->hide();
	  accepted = true;
	  break;
	} // switch (key->keyval)
      } // if (key->state == 0)

      if (accepted)
	return false;

      return this->ui->key_press(key);
    } // bool GameReview::key_press(GdkEventKey* key)

} // namespace UI_GTKMM_NS

#endif // #ifdef USE_UI_GTKMM
