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

#include "ui.h"
#include "bug_report.h"
#include "main_window.h"
#include "table.h"
#include "party_points.h"

#include "../../party/party.h"
#include "../../misc/setting.h"

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

namespace UI_GTKMM_NS {

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

  /**
   **
   ** Constructor
   **
   ** @param	parent	the parent widget
   **
   ** @return	-
   ** 
   ** @author	Diether Knof
   **
   ** @version	0.5.4
   **
   **/
  GameFinished::GameFinished(Base* const parent) :
    Base(parent),
  Dialog("Game finished", false, true),
  create_bug_report_button(NULL),
  game_summary(NULL),
  show_party_points(NULL),
  next_game_button(NULL)
  {
    DEBUG_CALLING(INFO_GTKMM_GAME_FINISHED && INFO_GTKMM_INIT,
		  "GameFinished::GameFinished(parent)");

    // 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,
					      &GameFinished::init));

    DEBUG_RETURNING(VOID,
		    INFO_GTKMM_GAME_FINISHED && INFO_GTKMM_INIT,
		    "GameFinished::GameFinished(parent)");
  } // GameFinished::GameFinished(Base* const parent)

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

    DEBUG_RETURNING(VOID,
		    INFO_GTKMM_GAME_FINISHED && INFO_GTKMM_INIT,
		    "GameFinished::~GameFinished()");
  } // GameFinished::~GameFinished()

  /**
   **
   ** create all subelements
   **
   ** @param	-
   **
   ** @return	-
   ** 
   ** @author	Diether Knof
   **
   ** @version	0.6.1
   **
   ** @bug	GTK 2.2 seems to have problems with the upper rows:
   **		under Linux sometimes the text is only shown half (vertical!)
   **		under Windows the size is said to be too small for the whole text
   **		The problem seems to be, that first the buttons are drawn
   **		*at the top* of the dialog.
   **		2004-05-26: After inserting the 'show party points' button I
   **			    cannot reproduce the problem any more.
   **/
  void
    GameFinished::init()
    {
      DEBUG_CALLING(INFO_GTKMM_GAME_FINISHED && INFO_GTKMM_INIT,
		    "GameFinished::init()");

      this->ui->add_for_translation(*this, ::translator("Game finished"));

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

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

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

      {
	this->show_party_points
	  = Gtk::manage(new Gtk::StockButton("show party points"));
	this->ui->add_for_translation(*(show_party_points->label),
				      ::translator("show party points"));

	Gtk::Alignment* alignment = Gtk::manage(new Gtk::Alignment(Gtk::ALIGN_CENTER, Gtk::ALIGN_CENTER, 0, 0));
	this->get_vbox()->pack_end(*alignment, false, true);
	alignment->add(*(this->show_party_points));
      }

      { // action area
	this->create_bug_report_button
	  = Gtk::manage(new Gtk::StockButton("create bug report"));
	this->ui->add_for_translation(*(create_bug_report_button->label),
				      ::translator("create bug report"));
	this->add_action_widget(*(this->create_bug_report_button), Gtk::RESPONSE_NONE);
	this->next_game_button
	  = Gtk::manage(new Gtk::StockButton(Gtk::Stock::OK, "next game"));
	this->ui->add_for_translation(*(next_game_button->label),
				      ::translator("next game"));
	this->add_action_widget(*(this->next_game_button), Gtk::RESPONSE_CLOSE);

	this->next_game_button->grab_default();
	this->next_game_button->grab_focus();
      } // action area

      this->show_all_children();
#ifdef WORKAROUND
      // for graphic bugs
      Gtk::Alignment* alignment
	= Gtk::manage(new Gtk::Alignment(Gtk::ALIGN_CENTER,
					 Gtk::ALIGN_CENTER,
					 0, 0));
      this->get_vbox()->add(*alignment);
      alignment->show();
#endif

      // signals
      this->create_bug_report_button->signal_clicked().connect(SigC::slot(*(this->ui->bug_report),
									  &BugReport::create_report));
      this->show_party_points->signal_clicked().connect(SigC::slot(*(this->ui->table->party_points_), &Gtk::Window::present));
      this->next_game_button->signal_clicked().connect(SigC::slot(*this, &Gtk::Widget::hide));

      this->setting_update(Setting::SHOW_BUG_REPORT_BUTTON_IN_GAME_FINISHED_WINDOW);
      this->language_update();

      DEBUG_RETURNING(VOID,
		      INFO_GTKMM_GAME_FINISHED && INFO_GTKMM_INIT,
		      "GameFinished::init()");
    } // void GameFinished::init()

  /**
   **
   ** the game has finished:
   ** show the summary and let the player review the game
   **
   ** @param	-
   **
   ** @return	-
   **
   ** @version	0.6.1
   **
   ** @author	Diether Knof
   **
   **/
  void
    GameFinished::game_finished()
    {
      DEBUG_CALLING(INFO_GTKMM_GAME_FINISHED && INFO_GTKMM_GAMEPLAY,
		    "GameFinished::game_finished()");

      this->game_summary->set_gameno(::party.gameno());

      this->move_to_last_position();
      this->present();

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

      DEBUG_RETURNING(VOID,
		      INFO_GTKMM_GAME_FINISHED && INFO_GTKMM_GAMEPLAY,
		      "GameFinished::game_finished()");
    } // void GameFinished::game_finished()

  /**
   **
   ** a setting has changed
   ** SHOW_BUG_REPORT_BUTTON_IN_GAME_FINISHED_WINDOW:
   **	update whether to show the 'create bug report' button
   **
   ** @param	type	the type that has changed
   **
   ** @return	-
   **
   ** @version	0.6.1
   **
   ** @author	Diether Knof
   **
   **/
  void
    GameFinished::setting_update(int const type)
    {
      DEBUG_CALLING(INFO_GTKMM_GAME_FINISHED && INFO_GTKMM_UPDATE,
		    "GameFinished::setting_update(type)");

      if (!this->is_realized())
	DEBUG_RETURNING(VOID,
			INFO_GTKMM_GAME_FINISHED && INFO_GTKMM_UPDATE,
			"GameFinished::setting_update(type)");

      switch (type) {
      case Setting::SHOW_BUG_REPORT_BUTTON_IN_GAME_FINISHED_WINDOW:
	if (::setting(Setting::SHOW_BUG_REPORT_BUTTON_IN_GAME_FINISHED_WINDOW))
	  this->create_bug_report_button->show();
	else
	  this->create_bug_report_button->hide();
	break;
      default:
	break;
      } // switch(type)

      DEBUG_RETURNING(VOID,
		      INFO_GTKMM_GAME_FINISHED && INFO_GTKMM_UPDATE,
		      "GameFinished::setting_update(type)");
    } // void GameFinished::setting_update(int const type)

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

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

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

      DEBUG_RETURNING(VOID,
		      INFO_GTKMM_GAME_FINISHED && INFO_GTKMM_UPDATE,
		      "GameFinished::name_changed(player)");
    } // void GameFinished::name_changed(Player const& player)

  /**
   **
   ** event: hide the window
   **
   ** @param	-
   **
   ** @return	-
   **
   ** @version	0.6.4
   **
   ** @author	Diether Knof
   **
   **/
  void
    GameFinished::on_hide()
    {
      DEBUG_CALLING(INFO_GTKMM_GAME_FINISHED && INFO_GTKMM_EVENT,
		    "GameFinished::on_hide()");

      this->ui->main_quit();
      this->save_position();
      this->Gtk::Dialog::on_hide();

      DEBUG_RETURNING(VOID,
		      INFO_GTKMM_GAME_FINISHED && INFO_GTKMM_EVENT,
		      "GameFinished::on_hide()");
    } // void GameFinished::on_hide()

  /**
   **
   ** save the position of the window
   **
   ** @param	-
   **
   ** @return	-
   **
   ** @version	0.6.4
   **
   ** @author	Diether Knof
   **
   **/
  void
    GameFinished::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 GameFinished::save_position()

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

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

} // namespace UI_GTKMM_NS

#endif // #ifdef USE_UI_GTKMM
