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

#include "ui.h"
#include "translations.h"
#include "bug_report.h"

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

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

extern OS_NS::BugReportReplay* bug_report_replay;

#include "gameplay_actions.h"
#include "widgets/stock_button.h"
#include <gtkmm/notebook.h>
#include <gtkmm/label.h>
#include <gtkmm/textview.h>
#include <gtkmm/stock.h>
#include <gtkmm/alignment.h>
#include <gtkmm/scrolledwindow.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),
    bug_report_replay(NULL),
    notebook(NULL),
    version(NULL),
    compiled(NULL),
    system(NULL),
    time(NULL),
    language(NULL),
    trick(NULL),
    message(NULL),
    seed(NULL),
    startplayer(NULL),
    soloplayer(NULL),
    actions(NULL),
    human_actions(NULL)
  {
    this->actions = Gtk::manage(new GameplayActions(this));
    this->human_actions = Gtk::manage(new GameplayActions(this));

    this->signal_realize().connect(sigc::mem_fun(*this, &BugReportReplay::init));

    this->ui->bug_report->set_dnd_destination(*this);

    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, ::translation("FreeDoko %ttitle%",
						      ::translation("Bug report replay")));

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

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

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

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

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

      { // informations
	this->version = Gtk::manage(new Gtk::Label(""));
	this->ui->translations->add(*this->version, ::translation(""));
	this->version->set_justify(Gtk::JUSTIFY_LEFT);
	this->compiled = Gtk::manage(new Gtk::Label(""));
	this->ui->translations->add(*this->compiled, ::translation(""));
	this->compiled->set_justify(Gtk::JUSTIFY_LEFT);
	this->system = Gtk::manage(new Gtk::Label(""));
	this->ui->translations->add(*this->system, ::translation(""));
	this->system->set_justify(Gtk::JUSTIFY_LEFT);
	this->time = Gtk::manage(new Gtk::Label(""));
	this->ui->translations->add(*this->time, ::translation(""));
	this->time->set_justify(Gtk::JUSTIFY_LEFT);
	this->language = Gtk::manage(new Gtk::Label(""));
	this->ui->translations->add(*this->language, ::translation(""));
	this->language->set_justify(Gtk::JUSTIFY_LEFT);
	this->trick = Gtk::manage(new Gtk::Label(""));
	this->ui->translations->add(*this->trick, ::translation(""));
	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, ::translation(""));
	this->seed->set_justify(Gtk::JUSTIFY_LEFT);

	this->startplayer = Gtk::manage(new Gtk::Label(""));
	this->ui->translations->add(*this->startplayer, ::translation(""));
	this->startplayer->set_justify(Gtk::JUSTIFY_LEFT);

	this->soloplayer = Gtk::manage(new Gtk::Label(""));
	this->ui->translations->add(*this->soloplayer, ::translation(""));
	this->soloplayer->set_justify(Gtk::JUSTIFY_LEFT);

	// hide the 'player' column
	this->human_actions->get_column(1)->set_visible(false);
      } // informations
      { // vbox
	this->notebook = Gtk::manage(new Gtk::Notebook());
	{ // general
	  Gtk::Label* label = Gtk::manage(new Gtk::Label("general"));
	  this->ui->translations->add(*label, ::translation("BugReportReplay::Group::general"));

	  Gtk::Box* general_box = Gtk::manage(new Gtk::VBox(false, 1 em));
	  general_box->set_border_width(1 em);

	  { // game box
	    Gtk::Alignment* alignment
	      = Gtk::manage(new Gtk::Alignment(Gtk::ALIGN_CENTER,
					       Gtk::ALIGN_CENTER,
					       0, 0));
	    Gtk::Box* game_box = Gtk::manage(new Gtk::VBox(false, 1 ex));

	    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);

	    alignment->add(*game_box);
	    general_box->pack_start(*alignment, false, true);
	  } // game box
	  { // message
	    general_box->add(*this->message);
	  } // message

	  this->notebook->append_page(*general_box, *label);
	} // general
	{ // actions
	  Gtk::Label* label = Gtk::manage(new Gtk::Label("actions"));
	  this->ui->translations->add(*label, ::translation("BugReportReplay::Group::actions"));
	  Gtk::ScrolledWindow* scrolled_window
	    = Gtk::manage(new Gtk::ScrolledWindow);
	  scrolled_window->set_policy(Gtk::POLICY_AUTOMATIC,
				      Gtk::POLICY_AUTOMATIC);

	  scrolled_window->add(*this->actions);

	  this->notebook->append_page(*scrolled_window, *label);
	} // actions
	{ // human actions
	  Gtk::Label* label = Gtk::manage(new Gtk::Label("human actions"));
	  this->ui->translations->add(*label, ::translation("BugReportReplay::Group::human actions"));
	  Gtk::ScrolledWindow* scrolled_window
	    = Gtk::manage(new Gtk::ScrolledWindow);
	  scrolled_window->set_policy(Gtk::POLICY_AUTOMATIC,
				      Gtk::POLICY_AUTOMATIC);

	  scrolled_window->add(*this->human_actions);

	  this->notebook->append_page(*scrolled_window, *label);
	} // human actions

	this->get_vbox()->add(*this->notebook);
      } // 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,
				     ::translation("Version: %sversion%",
						  (this->bug_report_replay->version
						   ?  static_cast<string const>(*this->bug_report_replay->version)
						   : "?")));
      this->ui->translations->change(*this->compiled,
				     ::translation("Compiled: %stime%",
						  this->bug_report_replay->compiled));
      this->ui->translations->change(*this->system,
				     ::translation("System: %ssystem%",
						  this->bug_report_replay->system));
      this->ui->translations->change(*this->time,
				     ::translation("Time: %stime%",
						  this->bug_report_replay->time));
      this->ui->translations->change(*this->language,
				     ::translation("Language: %slanguage%",
						  this->bug_report_replay->language));
      this->ui->translations->change(*this->trick,
				     ::translation("Trick: %utrick%",
						  this->bug_report_replay->trickno));

      this->message->get_buffer()->set_text(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,
				     ::translation("Seed: %useed%",
						  this->bug_report_replay->seed));
      this->ui->translations->change(*this->startplayer,
				     ::translation("Startplayer: %ustartplayer%",
						  this->bug_report_replay->startplayer_no));
      if (this->bug_report_replay->soloplayer_no != UINT_MAX)
	this->ui->translations->change(*this->soloplayer,
				       ::translation("Soloplayer: %usoloplayer%",
						    this->bug_report_replay->soloplayer_no));
      else
	this->ui->translations->change(*this->soloplayer,
				       ::translation("Soloplayer: -"));

      this->actions->update();
      this->human_actions->update();

      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->realize();

      this->actions->set_actions(bug_report_replay.actions);
      this->human_actions->set_actions(bug_report_replay.human_actions);

      this->update_info();

      if (this->notebook)
	this->notebook->set_current_page(0);

      this->show();

      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->actions->remove_actions();
      this->human_actions->remove_actions();
      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
