/**********************************************************************
 *
 *   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 "bug_report.h"

#include "ui.h"
#include "party_settings.h"
#include "main_window.h"

#include "../../misc/setting.h"
#include "../../misc/bug_report.h"
#include "../../os/bug_report_replay.h"
#include "../ui.wrap.h"

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

namespace UI_GTKMM_NS {

  /**
   **
   ** Constructor
   **
   ** @param	parent	the parent widget
   ** @param	message	the default message
   **
   ** @return	-
   ** 
   ** @author	Diether Knof
   **
   ** @version	0.6.3
   **
   ** @todo	make use of the 'RESPONSE' things
   **
   **/
  BugReport::BugReport(Base* const parent, string const message) :
    Base(parent),
  Dialog("BugReport", false, true),
  description(NULL),
  file_label(NULL),
  save_on_desktop_button(NULL),
  message(NULL),
  abort_button(NULL),
  save_button(NULL),
  load_file_selector(NULL)
  {
    DEBUG_CALLING(INFO_GTKMM_BUG_REPORT && INFO_GTKMM_INIT,
		  "BugReport::BugReport(parent, message)");

    this->message = Gtk::manage(new Gtk::TextView());
    this->message->get_buffer()->set_text(message);

    this->load_file_selector = new Gtk::FileSelection();
#ifndef RELEASE
    this->load_file_selector->set_filename("../Bugreports/");
#endif

    this->load_file_selector->get_cancel_button()->signal_clicked().connect(SigC::slot(*(this->load_file_selector), &Widget::hide));
    this->load_file_selector->get_ok_button()->signal_clicked().connect(SigC::slot(*(this->load_file_selector), &Widget::hide));
    this->load_file_selector->get_ok_button()->signal_clicked().connect(SigC::slot(*this, &BugReport::load_report));


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

    DEBUG_RETURNING(VOID,
		    INFO_GTKMM_BUG_REPORT && INFO_GTKMM_INIT,
		    "BugReport::BugReport(parent, message)");
  } // BugReport::BugReport(Base* const parent, string const message = "")

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

    delete this->load_file_selector;

    DEBUG_RETURNING(VOID,
		    INFO_GTKMM_BUG_REPORT && INFO_GTKMM_INIT,
		    "BugReport::~BugReport()");
  } // BugReport::~BugReport()

  /**
   **
   ** create all subelements
   **
   ** @param	-
   **
   ** @return	-
   ** 
   ** @author	Diether Knof
   **
   ** @version	0.5.4
   **
   **/
  void
    BugReport::init()
    {
      DEBUG_CALLING(INFO_GTKMM_BUG_REPORT && INFO_GTKMM_INIT,
		    "BugReport::init()");

      this->ui->add_for_translation(*this, ::translator("Bug report"));

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

      this->description
	= Gtk::manage(new Gtk::TextView());
      this->ui->add_for_translation(*(this->description),
				    ::translator("_bug report_"));
      this->file_label
	= Gtk::manage(new Gtk::Label("File: FreeDoko.bug_report"));
      this->ui->add_for_translation(*(this->file_label),
				    ::translator("File: %sfile%",
						 ::bug_report.filename()));
      this->save_on_desktop_button
	= Gtk::manage(new Gtk::CheckButton("save on the desktop"));
      this->ui->add_for_translation(*(dynamic_cast<Gtk::Label*>(this->save_on_desktop_button->get_child())),
				    ::translator("save on desktop"));
#if 0
      // this is initialized in the constructor
      this->message
	= Gtk::manage(new Gtk::TextView());
#endif
      this->abort_button
	= Gtk::manage(new Gtk::StockButton(Gtk::Stock::CANCEL, "abort"));
      this->ui->add_for_translation(*(this->abort_button),
				    ::translator("abort"));
      this->add_action_widget(*(this->abort_button), Gtk::RESPONSE_CLOSE);
      this->save_button
	= Gtk::manage(new Gtk::StockButton(Gtk::Stock::SAVE, "save"));
      this->ui->add_for_translation(*(this->save_button),
				    ::translator("save"));
      this->add_action_widget(*(this->save_button), Gtk::RESPONSE_CLOSE);

      this->save_button->grab_default();

      this->set_default_size(this->ui->logo->get_width(),
			     static_cast<int>(this->ui->logo->get_height() * 1.5));

      { // the description
	this->description->set_editable(false);
	this->description->set_wrap_mode(Gtk::WRAP_WORD);
	this->description->set_cursor_visible(false);

	this->get_vbox()->pack_start(*(this->description), false, true);
      } // the description
      this->get_vbox()->pack_start(*(this->file_label), false, true);
      this->get_vbox()->pack_start(*(this->save_on_desktop_button), false, true);
      { // the message
	Gtk::ScrolledWindow* message_window
	  = Gtk::manage(new Gtk::ScrolledWindow());
	message_window->add(*(this->message));
	message_window->set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
	this->message->set_editable(true);
	this->message->set_wrap_mode(Gtk::WRAP_WORD);
	this->message->set_cursor_visible(true);

	this->get_vbox()->pack_end(*message_window, true, true);
      } // the message

      this->show_all_children();
#ifndef WINDOWS
#ifndef LINUX
      this->save_on_desktop_button->hide();
#endif
#endif

      // signals
      this->abort_button->signal_clicked().connect(SigC::slot(*this, &Gtk::Widget::hide));
      this->save_button->signal_clicked().connect(SigC::slot(*this, &BugReport::save));
      this->save_button->signal_clicked().connect(SigC::slot(*this, &Gtk::Widget::hide));

      this->language_update();

      // events
      this->save_on_desktop_button->signal_toggled().connect(SigC::slot(*this, &BugReport::save_on_desktop_toggled_event));
      this->setting_update(Setting::SAVE_BUG_REPORTS_ON_DESKTOP);

      DEBUG_RETURNING(VOID,
		      INFO_GTKMM_BUG_REPORT && INFO_GTKMM_INIT,
		      "BugReport::init()");
    } // void BugReport::init()

  /**
   **
   ** load a report
   **
   ** @param	-
   **
   ** @return	-
   **
   ** @version	0.6.1
   **
   ** @author	Diether Knof
   **
   **/
  void
    BugReport::load_report()
    {
      DEBUG_CALLING(INFO_GTKMM_BUG_REPORT && INFO_GTKMM_EVENT,
		    "BugReport::load_report()");

      if (::game_status == GAMESTATUS::PARTY_INIT) {
#ifndef RELEASE
	cout << "loading bugreport "
	  << '\'' << this->load_file_selector->get_filename() << '\''
	  << endl;
#endif
	OS* bug_report_replay
	  = new (OS_NS::BugReportReplay)(this->load_file_selector->get_filename());
	if (OS_NS::BugReportReplay::bug_report_replay != NULL) {
	  static_cast<UI_Wrap*>(::ui)->append(bug_report_replay);
	  bug_report_replay->party_open();
	  bug_report_replay->party_get_settings();
	  this->ui->party_settings->update();
	} // if (OS_NS::BugReportReplay::bug_report_replay != NULL)
      } // if (::game_status == GAMESTATUS::PARTY_INIT)

      DEBUG_RETURNING(VOID,
		      INFO_GTKMM_BUG_REPORT && INFO_GTKMM_EVENT,
		      "BugReport::load_report()");
    } // void BugReport::load_report()

  /**
   **
   ** creates a report
   **
   ** @param	-
   **
   ** @return	-
   **
   ** @version	0.5.4
   **
   ** @author	Diether Knof
   **
   **/
  void
    BugReport::create_report()
    {
      DEBUG_CALLING(INFO_GTKMM_BUG_REPORT && INFO_GTKMM_EVENT,
		    "BugReport::create_report()");

      this->realize();

      this->message->get_buffer()->set_text("");
      if (this->file_label->is_realized())
	this->ui->change_translation(*(this->file_label),
				     ::translator("File: %sfile%",
						  ::bug_report.filename()));
      this->message->grab_focus();

      this->present();

      DEBUG_RETURNING(VOID,
		      INFO_GTKMM_BUG_REPORT && INFO_GTKMM_EVENT,
		      "BugReport::create_report()");
    } // void BugReport::create_report()

  /**
   **
   ** save the report
   **
   ** @param	-
   **
   ** @return	-
   **
   ** @version	0.5.4
   **
   ** @author	Diether Knof
   **
   **/
  void
    BugReport::save()
    {
      DEBUG_CALLING(INFO_GTKMM_BUG_REPORT && INFO_GTKMM_EVENT,
		    "BugReport::save()");

      ::bug_report.report(Glib::locale_from_utf8(this->message->get_buffer()->get_text() + "\n"));

      DEBUG_RETURNING(VOID,
		      INFO_GTKMM_BUG_REPORT && INFO_GTKMM_EVENT,
		      "BugReport::save()");
    } // void BugReport::save()

  /**
   **
   ** the button 'save on desktop' has been toggled
   **
   ** @param	-
   **
   ** @return	-
   **
   ** @version	0.6.1
   **
   ** @author	Diether Knof
   **
   **/
  void
    BugReport::save_on_desktop_toggled_event()
    {
      DEBUG_CALLING(INFO_GTKMM_BUG_REPORT && INFO_GTKMM_EVENT,
		    "BugReport::save_on_desktop_toggled_event()");

      ::setting.set(Setting::SAVE_BUG_REPORTS_ON_DESKTOP,
		    this->save_on_desktop_button->get_active());

      DEBUG_RETURNING(VOID,
		      INFO_GTKMM_BUG_REPORT && INFO_GTKMM_EVENT,
		      "BugReport::save_on_desktop_toggled_event()");
    } // void BugReport::save_on_desktop_toggled_event()

  /**
   **
   ** a setting has changed
   ** SAVE_BUG_REPORTS_ON_DESKTOP:
   **	update the toggle button
   **
   ** @param	type	the type that has changed
   **
   ** @return	-
   **
   ** @version	0.6.1
   **
   ** @author	Diether Knof
   **
   **/
  void
    BugReport::setting_update(int const type)
    {
      DEBUG_CALLING(INFO_GTKMM_BUG_REPORT && INFO_GTKMM_UPDATE,
		    "BugReport::setting_update(type)");

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

      switch (type) {
      case Setting::SAVE_BUG_REPORTS_ON_DESKTOP:
	this->save_on_desktop_button->set_active(::setting(Setting::SAVE_BUG_REPORTS_ON_DESKTOP));
	break;
      default:
	break;
      } // switch(type)

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

} // namespace UI_GTKMM_NS

#endif // #ifdef USE_UI_GTKMM
