/**********************************************************************
 *
 *   FreeDoko a Doppelkopf-Game
 *    
 *   Copyright (C) 2001-2005  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 "bug_report_replay.h"

#include "ui.h"
#include "translations.h"
#include "main_window.h"

#include "../ui.wrap.h"
#include "../../os/bug_report_replay.h"

#include "../../utils/string.h"

extern OS_NS::BugReportReplay* bug_report_replay;

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

namespace UI_GTKMM_NS {

  /**
   **
   ** Constructor
   **
   ** @param	parent	the parent widget
   **
   ** @return	-
   ** 
   ** @author	Diether Knof
   **
   ** @version	0.6.8
   **
   **/
  BugReportReplay::BugReportReplay(Base* const parent) :
    Base(parent),
    StickyDialog("bug report replay", false, true),
    bug_report_replay(NULL),
    version(NULL),
    compiled(NULL),
    system(NULL),
    time(NULL),
    language(NULL),
    trick(NULL),
    message(NULL),
    seed(NULL),
    startplayer(NULL),
    soloplayer(NULL)
  {

    this->signal_realize().connect(SigC::slot(*this, &BugReportReplay::init));
    if (::bug_report_replay)
      this->bug_report_replay_open(*::bug_report_replay);

    static_cast<UI_Wrap*>(::ui)->append(this);

    return ;
  } // BugReportReplay::BugReportReplay(Base* parent)

  /**
   **
   ** Destruktor
   **
   ** @param	-
   **
   ** @return	-
   ** 
   ** @author	Diether Knof
   **
   ** @version	0.6.8
   **
   **/
  BugReportReplay::~BugReportReplay()
  {
    static_cast<UI_Wrap*>(::ui)->remove(this);
    return ;
  } // BugReportReplay::~BugReportReplay()

  /**
   **
   ** create all subelements
   **
   ** @param	-
   **
   ** @return	-
   ** 
   ** @author	Diether Knof
   **
   ** @version	0.6.8
   **
   **/
  void
    BugReportReplay::init()
    {
      this->ui->translations->add(*this, ::translator("FreeDoko %ttitle%",
						      ::translator("Bug report replay")));

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

      { // action area
	Gtk::StockButton* end_button
	  = Gtk::manage(new Gtk::StockButton(Gtk::Stock::CLOSE, "end bug report"));
	this->ui->translations->add(*end_button,
				    ::translator("end bug report"));
	this->add_action_widget(*end_button, Gtk::RESPONSE_CLOSE);

	end_button->signal_clicked().connect(SigC::slot(*this,
							&BugReportReplay::end_bug_report));

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

	close_button->grab_default();
	close_button->signal_clicked().connect(SigC::slot(*this,
							  &Gtk::Widget::hide));
      } // action area

      { // informations
	this->version = Gtk::manage(new Gtk::Label(""));
	this->ui->translations->add(*this->version, ::translator(""));
	this->version->set_justify(Gtk::JUSTIFY_LEFT);
	this->compiled = Gtk::manage(new Gtk::Label(""));
	this->ui->translations->add(*this->compiled, ::translator(""));
	this->compiled->set_justify(Gtk::JUSTIFY_LEFT);
	this->system = Gtk::manage(new Gtk::Label(""));
	this->ui->translations->add(*this->system, ::translator(""));
	this->system->set_justify(Gtk::JUSTIFY_LEFT);
	this->time = Gtk::manage(new Gtk::Label(""));
	this->ui->translations->add(*this->time, ::translator(""));
	this->time->set_justify(Gtk::JUSTIFY_LEFT);
	this->language = Gtk::manage(new Gtk::Label(""));
	this->ui->translations->add(*this->language, ::translator(""));
	this->language->set_justify(Gtk::JUSTIFY_LEFT);
	this->trick = Gtk::manage(new Gtk::Label(""));
	this->ui->translations->add(*this->trick, ::translator(""));
	this->trick->set_justify(Gtk::JUSTIFY_LEFT);
	this->message = Gtk::manage(new Gtk::TextView());
	this->message->get_buffer()->set_text("");
	this->message->set_wrap_mode(Gtk::WRAP_WORD);
#ifdef RELEASE
	this->message->set_editable(false);
	this->message->set_cursor_visible(false);
#else
	// Note: the changes in the message box are not saved.
#endif

	this->seed = Gtk::manage(new Gtk::Label(""));
	this->ui->translations->add(*this->seed, ::translator(""));
	this->seed->set_justify(Gtk::JUSTIFY_LEFT);
	this->startplayer = Gtk::manage(new Gtk::Label(""));
	this->ui->translations->add(*this->startplayer, ::translator(""));
	this->startplayer->set_justify(Gtk::JUSTIFY_LEFT);
	this->soloplayer = Gtk::manage(new Gtk::Label(""));
	this->ui->translations->add(*this->soloplayer, ::translator(""));
	this->soloplayer->set_justify(Gtk::JUSTIFY_LEFT);
      } // informations
      { // vbox
	Gtk::Box* global_box = Gtk::manage(new Gtk::HBox(false, 1 em));
	this->get_vbox()->add(*global_box);
	global_box->set_border_width(1 em);
#if 0
	{ // informations box
	  Gtk::Alignment* alignment
	    = Gtk::manage(new Gtk::Alignment(Gtk::ALIGN_TOP,
					     Gtk::ALIGN_LEFT,
					     0, 0));
	  global_box->add(*alignment);
	  Gtk::Box* information_box = Gtk::manage(new Gtk::VBox(false, 1 ex));
	  alignment->add(*information_box);

	  information_box->pack_start(*this->version, Gtk::PACK_EXPAND_WIDGET);
	  information_box->pack_start(*this->compiled, true, false);
	  information_box->pack_start(*this->system, Gtk::PACK_EXPAND_WIDGET);
	  information_box->pack_start(*this->time, Gtk::PACK_EXPAND_WIDGET);
	  information_box->pack_start(*this->language, Gtk::PACK_EXPAND_WIDGET);
	  information_box->pack_start(*this->trick, Gtk::PACK_EXPAND_WIDGET);
	} // informations box
#endif
	{ // game box
	  Gtk::Alignment* alignment
	    = Gtk::manage(new Gtk::Alignment(Gtk::ALIGN_TOP,
					     Gtk::ALIGN_LEFT,
					     0, 0));
	  global_box->add(*alignment);
	  Gtk::Box* game_box = Gtk::manage(new Gtk::VBox(false, 1 ex));
	  alignment->add(*game_box);

	  game_box->pack_start(*this->version, Gtk::PACK_EXPAND_WIDGET);
	  game_box->pack_start(*this->seed, Gtk::PACK_EXPAND_WIDGET);
	  game_box->pack_start(*this->startplayer, Gtk::PACK_EXPAND_WIDGET);
	  game_box->pack_start(*this->soloplayer, Gtk::PACK_EXPAND_WIDGET);
	  game_box->pack_start(*this->trick, Gtk::PACK_EXPAND_WIDGET);
	} // game box
	this->get_vbox()->add(*this->message);
      } // vbox

      this->show_all_children();

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

  /**
   **
   ** updates the information
   **
   ** @param	-
   **
   ** @return	-
   ** 
   ** @author	Diether Knof
   **
   ** @version	0.6.8
   **
   **/
  void
    BugReportReplay::update_info()
    {
      if (!this->bug_report_replay)
	return ;

      DEBUG_ASSERTION((this->bug_report_replay == ::bug_report_replay),
		      "BugReportReplay::update_info():\n"
		      "  this->bug_report_replay != ::bug_report_replay:\n"
		      "  " << this->bug_report_replay
		      << " != " << ::bug_report_replay);

      this->ui->translations->change(*this->version,
				     ::translator("Version: %sversion%",
						  (this->bug_report_replay->version
						   ?  static_cast<string const>(*this->bug_report_replay->version)
						   : "?")));
      this->ui->translations->change(*this->compiled,
				     ::translator("Compiled: %stime%",
						  this->bug_report_replay->compiled));
      this->ui->translations->change(*this->system,
				     ::translator("System: %ssystem%",
						  this->bug_report_replay->system));
      this->ui->translations->change(*this->time,
				     ::translator("Time: %stime%",
						  this->bug_report_replay->time));
      this->ui->translations->change(*this->language,
				     ::translator("Language: %slanguage%",
						  this->bug_report_replay->language));
      this->ui->translations->change(*this->trick,
				     ::translator("Trick: %utrick%",
						  this->bug_report_replay->trickno));

      this->message->get_buffer()->set_text(Glib::locale_to_utf8(this->bug_report_replay->message));

      this->seed->set_label(DK::Utils::String::to_string(this->bug_report_replay->seed));

      this->startplayer->set_label(DK::Utils::String::to_string(this->bug_report_replay->startplayer_no));
      this->ui->translations->change(*this->seed,
				     ::translator("Seed: %useed%",
						  this->bug_report_replay->seed));
      this->ui->translations->change(*this->startplayer,
				     ::translator("Startplayer: %ustartplayer%",
						  this->bug_report_replay->startplayer_no));
#ifdef POSTPHONED
      this->ui->translations->change(*this->soloplayer,
				     ::translator("Soloplayer: %usoloplayer%",
						  this->bug_report_replay->soloplayer_no));
#else
      this->ui->translations->change(*this->soloplayer,
				     ::translator("Soloplayer: ?"));
#endif

      return ;
    } // void BugReportReplay::update_info()

  /**
   **
   ** a bug report replay is opened
   **
   ** @param	bug_report_replay	the bug report replay that is opened
   **
   ** @return	-
   ** 
   ** @author	Diether Knof
   **
   ** @version	0.6.8
   **
   **/
  void
    BugReportReplay::bug_report_replay_open(OS_NS::BugReportReplay const&
					    bug_report_replay)
    {
      this->bug_report_replay = &bug_report_replay;
      this->show();
      this->update_info();
      return ;
    } // void BugReportReplay::bug_report_replay_open(OS_NS::BugReportReplay bug_report_replay)

  /**
   **
   ** the bug report replay is closed
   **
   ** @param	-
   **
   ** @return	-
   ** 
   ** @author	Diether Knof
   **
   ** @version	0.6.8
   **
   **/
  void
    BugReportReplay::bug_report_replay_close()
    {
      this->bug_report_replay = NULL;
      this->hide();
      return ;
    } // BugReportReplay::bug_report_replay_close()

  /**
   **
   ** update the information
   **
   ** @param	party	party that is opened
   **
   ** @return	-
   ** 
   ** @author	Diether Knof
   **
   ** @version	0.6.8
   **
   **/
  void
    BugReportReplay::party_open(Party const& party)
    {
      this->OS::party_open(party);
      this->update_info();
      return ;
    } // void BugReportReplay::party_open(Party party)

  /**
   **
   ** update the information
   **
   ** @param	-
   **
   ** @return	-
   ** 
   ** @author	Diether Knof
   **
   ** @version	0.6.8
   **
   **/
  void
    BugReportReplay::party_get_settings()
    {
      this->update_info();
      return ;
    } // void BugReportReplay::party_get_settings()

  /**
   **
   ** update the information
   **
   ** @param	game	game that is opened
   **
   ** @return	-
   ** 
   ** @author	Diether Knof
   **
   ** @version	0.6.8
   **
   **/
  void
    BugReportReplay::game_open(Game const& game)
    {
      this->OS::game_open(game);
      this->update_info();
      return ;
    } // void BugReportReplay::game_open(Game game)

  /**
   **
   ** update the information
   **
   ** @param	-
   **
   ** @return	-
   ** 
   ** @author	Diether Knof
   **
   ** @version	0.6.8
   **
   **/
  void
    BugReportReplay::game_redistribute()
    {
      this->update_info();
      return ;
    } // void BugReportReplay::game_redistribute()

  /**
   **
   ** end the bug report
   **
   ** @param	-
   **
   ** @return	-
   ** 
   ** @author	Diether Knof
   **
   ** @version	0.6.8
   **
   **/
  void
    BugReportReplay::end_bug_report()
    {
      delete ::bug_report_replay;

      return ;
    } // void BugReportReplay::end_bug_report()

} // namespace UI_GTKMM_NS

#endif // #ifdef USE_UI_GTKMM
