/**********************************************************************
 *
 *   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_overview.h"
#include "game_summary.h"
#include "ui.h"
#include "main_window.h"

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

#include "widgets/stock_button.h"
#include <gtkmm/stock.h>
#include <gtkmm/label.h>
#include <gtkmm/checkbutton.h>

namespace UI_GTKMM_NS {

  /**
   **
   ** Constructor
   **
   ** @param	parent	the parent widget
   **
   ** @return	-
   ** 
   ** @author	Diether Knof
   **
   ** @version	0.5.4
   **
   **/
  PartyPoints::GameOverview::GameOverview(PartyPoints* const party_points) :
    Base(party_points),
  Dialog("Game overview", false, true),
  gameno_(UINT_MAX),
  game_summary(NULL),
  close_button(NULL)
  {
    DEBUG_CALLING(INFO_GTKMM_GAME_OVERVIEW && INFO_GTKMM_INIT,
		  "GameOverview::GameOverview(party_points)");

    // the game summary is created here, because else I would have first
    // to show this window and afterwards set the gameno.
    this->game_summary
      = Gtk::manage(new GameSummary(this));

    this->signal_realize().connect(SigC::slot(*this,
					      &GameOverview::init));

    DEBUG_RETURNING(VOID,
		    INFO_GTKMM_GAME_OVERVIEW && INFO_GTKMM_INIT,
		    "GameOverview::GameOverview(party_points)");
  } // GameOverview::GameOverview(PartyPoints* const party_points)

  /**
   **
   ** Destruktor
   **
   ** @param	-
   **
   ** @return	-
   ** 
   ** @author	Diether Knof
   **
   ** @version	0.5.4
   **
   **/
  PartyPoints::GameOverview::~GameOverview()
  {
    DEBUG_CALLING(INFO_GTKMM_GAME_OVERVIEW && INFO_GTKMM_INIT,
		  "GameOverview::~GameOverview()");

    DEBUG_RETURNING(VOID,
		    INFO_GTKMM_GAME_OVERVIEW && INFO_GTKMM_INIT,
		    "GameOverview::~GameOverview()");
  } // GameOverview::~GameOverview()

  /**
   **
   ** create all subelements
   **
   ** @param	-
   **
   ** @return	-
   ** 
   ** @author	Diether Knof
   **
   ** @version	0.6.1
   **
   **/
  void
    PartyPoints::GameOverview::init()
    {
      DEBUG_CALLING(INFO_GTKMM_GAME_OVERVIEW && INFO_GTKMM_INIT,
		    "GameOverview::init()");

      this->ui->add_for_translation(*this, 
				    ::translator("Game overview: %ugame%",
						 this->gameno_));

      this->set_icon(this->ui->main_window->icon);

      this->set_transient_for(*dynamic_cast<PartyPoints*>(this->parent));
      this->set_skip_taskbar_hint();

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

      this->get_vbox()->pack_start(*(this->game_summary),
				   true, true);

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

	this->close_button->grab_default();
      } // action area

      // signals
      this->close_button->signal_clicked().connect(SigC::slot(*this, &Gtk::Widget::hide));
      this->signal_hide().connect(SigC::bind<bool>(SigC::slot(*(dynamic_cast<PartyPoints*>(this->parent)->show_game_overview_button),
							      &Gtk::ToggleButton::set_active),
						   false));
      this->signal_hide().connect(SigC::slot(*(dynamic_cast<PartyPoints*>(this->parent)),
					     &PartyPoints::clear_selection));

      this->show_all_children();

      DEBUG_RETURNING(VOID,
		      INFO_GTKMM_GAME_OVERVIEW && INFO_GTKMM_INIT,
		      "GameOverview::init()");
    } // void GameOverview::init()

  /**
   **
   ** -> result
   **
   ** @param	-
   **
   ** @return	the gameno
   **
   ** @version	0.6.3
   **
   ** @author	Diether Knof
   **
   **/
  unsigned
    PartyPoints::GameOverview::gameno() const
    {
      DEBUG_CALLING(INFO_GTKMM_GAME_OVERVIEW && INFO_GTKMM_VALUE,
		    "GameOverview::gameno()");

      DEBUG_RETURNING(this->gameno_,
		      INFO_GTKMM_GAME_OVERVIEW && INFO_GTKMM_VALUE,
		      "GameOverview::gameno()");
    } // unsigned GameOverview::gameno() const

  /**
   **
   ** the game has finished:
   ** show the summary and let the player review the game
   **
   ** @param	gameno	new gameno
   **		(UINT_MAX for no gameno)
   **
   ** @return	-
   **
   ** @version	0.6.3
   **
   ** @author	Diether Knof
   **
   **/
  void
    PartyPoints::GameOverview::set_gameno(unsigned const gameno)
    {
      DEBUG_CALLING(INFO_GTKMM_GAME_OVERVIEW && INFO_GTKMM_VALUE,
		    "GameOverview::set_gameno(gameno)");

      if (!this->is_realized())
	DEBUG_RETURNING(VOID,
			INFO_GTKMM_GAME_OVERVIEW && INFO_GTKMM_VALUE,
			"GameOverview::set_gameno(gameno)");

      if (this->gameno_ == gameno)
	DEBUG_RETURNING(VOID,
			INFO_GTKMM_GAME_OVERVIEW && INFO_GTKMM_VALUE,
			"GameOverview::set_gameno(gameno)");

      this->gameno_ = gameno;

      if (this->gameno() == UINT_MAX) {
	this->hide();
	DEBUG_RETURNING(VOID,
			INFO_GTKMM_GAME_OVERVIEW && INFO_GTKMM_VALUE,
			"GameOverview::set_gameno(gameno)");
      }

      DEBUG_ASSERTION((this->gameno() <= ::party.gameno()),
		      "GameOverview::set_gameno():\n"
		      "  gameno = " << this->gameno()
		      << " > "
		      << ::party.gameno() << " = ::party.gameno()");

      this->game_summary->set_gameno(this->gameno());
      this->ui->change_translation(*this,
				   ::translator("Game overview: %ugame%",
						this->gameno() + 1));

      DEBUG_RETURNING(VOID,
		      INFO_GTKMM_GAME_OVERVIEW && INFO_GTKMM_VALUE,
		      "GameOverview::set_gameno(gameno)");
    } // void GameOverview::set_gameno(unsigned const gameno)

  /**
   **
   ** the name of 'player' has changed
   **
   ** @param	player	the player with the changed name
   **
   ** @return	-
   **
   ** @version	0.6.4
   **
   ** @author	Diether Knof
   **
   **/
  void
    PartyPoints::GameOverview::name_changed(Player const& player)
    {
      DEBUG_CALLING(INFO_GTKMM_GAME_OVERVIEW && INFO_GTKMM_UPDATE,
		    "GameOverview::name_changed(player)");

      if (!this->is_realized())
	DEBUG_RETURNING(VOID,
			INFO_GTKMM_GAME_OVERVIEW && INFO_GTKMM_UPDATE,
			"GameOverview::name_changed(player)");

      if (this->game_summary)
	this->game_summary->name_changed(player);

      DEBUG_RETURNING(VOID,
		      INFO_GTKMM_GAME_OVERVIEW && INFO_GTKMM_UPDATE,
		      "GameOverview::name_changed(player)");
    } // void PartyPoints::GameOverview::name_changed(Player const& player)

} // namespace UI_GTKMM_NS

#endif // #ifdef USE_UI_GTKMM
