/**********************************************************************
 *
 *   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_GTK

#include "game_finished.h"
#include "gtk_gui.h"
#include "bug_report.h"
#include "main_window.h"
#include "playfield.h"
#include "table.h"
#include "last_trick.h"


#include "../../party/party.h"
#include "../../game/gamepointstable.h"
#include "../../misc/language.h"
#include "../../misc/setting.h"

namespace GTK_DOKO {

  /*****************************************************************************
   *
   * GameFinished::GameFinished(class Playfield* playfield_a)
   *
   * Parameters:	playfield_a - pointer to the playfield
   *
   * Result:	none
   * 
   * Version:	0.3.7
   *
   * Description:	Constructor
   *
   ****************************************************************************/
  GameFinished::GameFinished(class Playfield* playfield_a) :
    PlayfieldDialog(playfield_a, "Game finished"), // the base class
  game_summary(), // the game summary
  button_bug_report(gtk_button_new_with_label("")), // button for a bug report
  alignment_bug_report(gtk_alignment_new(0.5, 0.5, 0, 0)), // the alignment for the bug report
  separator_bug_report(gtk_hseparator_new()), // a separator
  button_next_game(gtk_button_new_with_label("")) // button for the next game
  {
    DEBUG_CALLING(INFO_GTK_GAME_FINISHED && INFO_GTK_INIT,
		  "GameFinished::GameFinished()");

    gtk_container_add(GTK_CONTAINER(this->alignment_bug_report),
		      this->button_bug_report);
    gtk_box_pack_start(GTK_BOX(GTK_DIALOG(this->window())->vbox),
		       this->alignment_bug_report, FALSE, FALSE, 10);
    gtk_box_pack_start(GTK_BOX(GTK_DIALOG(this->window())->vbox),
		       this->separator_bug_report, FALSE, FALSE, 0);
    gtk_container_add(GTK_CONTAINER(GTK_DIALOG(this->window())->vbox),
		      this->game_summary.widget());
    gtk_container_add(GTK_CONTAINER(GTK_DIALOG(this->window())->action_area),
		      this->button_next_game);

    DEBUG_RETURNING(VOID,
		    INFO_GTK_GAME_FINISHED && INFO_GTK_INIT,
		    "GameFinished::GameFinished()");
  } // GameFinished::GameFinished(class Playfield* playfield_a)

  /*****************************************************************************
   *
   * void GameFinished::init()
   *
   * Parameters:	none
   *
   * Result:	none
   * 
   * Version:	0.3.7
   *
   * Description:	inits the 'about' help window
   *
   ****************************************************************************/
  void
    GameFinished::init()
    {
      DEBUG_CALLING(INFO_GTK_GAME_FINISHED && INFO_GTK_INIT,
		    "GameFinished::init()");

      this->PlayfieldDialog::init();
      this->game_summary.init();

      gtk_window_set_transient_for(GTK_WINDOW(this->window()),
				   GTK_WINDOW(gui().main_window().window()));
      { // center the window
	int x, y, width, height, wwidth, wheight;

	gdk_window_get_geometry(gui().main_window().window()->window,
				&x, &y, &width, &height, NULL);
	gdk_window_get_size(this->window()->window, &wwidth, &wheight);
	gdk_window_move(this->window()->window,
			x + width / 2 - wwidth / 2, y + height / 2 - wheight / 2);
      } // center the window

      // Buttons
      gtk_signal_connect(GTK_OBJECT(button_bug_report), "clicked",
			 GTK_SIGNAL_FUNC(BugReport::show_event),
			 &(this->gui().bug_report()));
      gtk_signal_connect(GTK_OBJECT(button_next_game), "clicked",
			 GTK_SIGNAL_FUNC(GameFinished::next_game_event),
			 this);

      DEBUG_RETURNING(VOID,
		      INFO_GTK_GAME_FINISHED && INFO_GTK_INIT,
		      "GameFinished::init()");
    } // void GameFinished::init()

  /*****************************************************************************
   *
   * static void GameFinished::next_game_event(GtkWidget*, gpointer game_finished)
   *
   * Parameters:	game_finished - game_finished of the event
   *
   * Result:	none
   *
   * Version:	0.3.7
   *
   * Description:	the next game is to begin
   *
   ****************************************************************************/
  void
    GameFinished::next_game_event(GtkWidget*, gpointer game_finished)
    {
      DEBUG_CALLING(INFO_GTK_GAME_FINISHED && INFO_GTK_EVENT,
		    "GameFinished::next_game_event()");

      ((GameFinished*)game_finished)->next_game();
      DEBUG_RETURNING(VOID,
		      INFO_GTK_GAME_FINISHED && INFO_GTK_EVENT,
		      "GameFinished::next_game_event()");
    } // static void GameFinished::next_game_event(GtkWidget*, gpointer gametype)

  /*****************************************************************************
   *
   * void GameFinished::show()
   *
   * Parameters:	none
   *
   * Result:	none
   *
   * Version:	0.3.7
   *
   * Description:	shows the window
   *
   ****************************************************************************/
  void
    GameFinished::show()
    {
      DEBUG_CALLING(INFO_GTK_GAME_FINISHED && INFO_GTK_EVENT,
		    "GameFinished::show()");

      DEBUG_ASSERTION(game_status == GAMESTATUS::GAME_FINISHED,
		      "GameFinished::show()\n"
		      "  'game_status' != GAME_RESERVATION");

      if (!FAST_PLAY) {
	this->game_summary.gameno_set((static_cast<Party const&>(::party)).gamepointstable().gameno() - 1);

	this->update();

	gtk_widget_show(this->window());

	// wait until the window is closed
	while ( (game_status == GAMESTATUS::GAME_FINISHED)
		&& (GTK_WIDGET_VISIBLE(this->window())) )
	  gtk_main();

	// close the window
	if (GTK_WIDGET_VISIBLE(this->window())) {
	  gtk_widget_hide(this->window());
	  this->playfield().table().last_trick().close();
	}
      } // if (!FAST_PLAY)

      DEBUG_RETURNING(VOID,
		      INFO_GTK_GAME_FINISHED && INFO_GTK_EVENT,
		      "GameFinished::show()");
    } // void GameFinished::show()

  /*****************************************************************************
   *
   * void GameFinished::next_game()
   *
   * Parameters:	none
   *
   * Result:	none
   *
   * Version:	0.3.7
   *
   * Description:	calls the next game
   *
   ****************************************************************************/
  void
    GameFinished::next_game()
    {
      DEBUG_CALLING(INFO_GTK_GAME_FINISHED && INFO_GTK_EVENT,
		    "GameFinished::next_game()");

      if (!(game_status & GAMESTATUS::GAME)
	  || (game_status == GAMESTATUS::GAME_FINISHED)) {

	// hide the window
	if (GTK_WIDGET_VISIBLE(this->window()))
	  gtk_widget_hide(this->window());

	this->gui().main_quit();
      }


      this->playfield().table().last_trick().close();
      // put the main window in the foreground
      gtk_widget_show(this->gui().main_window().window());

      DEBUG_RETURNING(VOID,
		      INFO_GTK_GAME_FINISHED && INFO_GTK_EVENT,
		      "GameFinished::next_game()");
    } // void GameFinished::next_game()

  /**
   **
   ** update the window (whether to show the bug report button)
   **
   ** @param	-
   **
   ** @return	-
   ** 
   ** @version	0.5.3
   **
   ** @author	Diether Knof
   **
   **/
  void
    GameFinished::update()
    {
      DEBUG_CALLING(INFO_GTK_GAME_FINISHED && INFO_GTK_UPDATE,
		    "GameFinished::update()");

      if (game_status == GAMESTATUS::GAME_FINISHED) {
	this->language_update();
	if (::setting(Setting::SHOW_BUG_REPORT_BUTTON_IN_GAME_FINISHED_WINDOW)) {
	  gtk_widget_show(this->button_bug_report);
	  gtk_widget_show(this->alignment_bug_report);
	  gtk_widget_show(this->separator_bug_report);
	} else { // if !(::setting(SHOW_BUG_REPORT_BUTTON_IN_GAME_FINISHED_WINDOW))
	  gtk_widget_hide(this->button_bug_report);
	  gtk_widget_hide(this->alignment_bug_report);
	  gtk_widget_hide(this->separator_bug_report);
	} // if !(::setting(SHOW_BUG_REPORT_BUTTON_IN_GAME_FINISHED_WINDOW))
	// the button
      } // if (game_status == GAMESTATUS::GAME_FINISHED)


      DEBUG_RETURNING(VOID,
		      INFO_GTK_GAME_FINISHED && INFO_GTK_UPDATE,
		      "GameFinished::update()");
    } // void GameFinished::update()

  /**
   **
   ** update the language
   **
   ** @param	-
   **
   ** @return	-
   ** 
   ** @version	0.5.3
   **
   ** @author	Diether Knof
   **
   **/
  void
    GameFinished::language_update()
    {
      DEBUG_CALLING(INFO_GTK_GAME_FINISHED && INFO_GTK_UPDATE,
		    "GameFinished::language_update()");

      if (game_status == GAMESTATUS::GAME_FINISHED) {
	this->PlayfieldDialog::language_update();
	this->game_summary.language_update();

	// the button
	gtk_label_set_text(GTK_LABEL(GTK_BIN(button_bug_report)->child),
			   (" " + language("create bug report") + " ").c_str());
	gtk_label_set_text(GTK_LABEL(GTK_BIN(button_next_game)->child),
			   (" " + language("next game") + " ").c_str());
      } // if (game_status == GAMESTATUS::GAME_FINISHED)


      DEBUG_RETURNING(VOID,
		      INFO_GTK_GAME_FINISHED && INFO_GTK_UPDATE,
		      "GameFinished::language_update()");
    } // void GameFinished::language_update()

  /*****************************************************************************
   *
   * void GameFinished::key(GdkEventKey* key)
   *
   * Parameters:	key - the pressed key
   *
   * Result:	none
   * 
   * Version:	0.3.7
   *
   * Description:	manages the pressed keys for the GameFinished window
   *
   ****************************************************************************/
  void
    GameFinished::key(GdkEventKey* key)
    {
      DEBUG_CALLING(INFO_GTK_GAME_FINISHED && INFO_GTK_KEY,
		    "GameFinished::key()");

#include <gdk/gdkkeysyms.h>

      switch (key->keyval) {
      case GDK_space:
      case GDK_Return: // close this window
	this->next_game();
	break;

      default: // pass the key to the playfield
	this->playfield().key(key);
	break;
      } // switch (key->keyval)

      DEBUG_RETURNING(VOID,
		      INFO_GTK_GAME_FINISHED && INFO_GTK_KEY,
		      "GameFinished::key()");
    } // void GameFinished::key(GdkKeyEvent);

}
#endif
