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

extern string const OS_NS::BUG_REPORT_FILE;

#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),
  StickyDialog("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)
  {
    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));

    return ;
  } // BugReport::BugReport(Base* const parent, string const message = "")

  /**
   **
   ** Destruktor
   **
   ** @param	-
   **
   ** @return	-
   ** 
   ** @author	Diether Knof
   **
   ** @version	0.5.4
   **
   **/
  BugReport::~BugReport()
  {
    delete this->load_file_selector;

    return ;
  } // BugReport::~BugReport()

  /**
   **
   ** create all subelements
   **
   ** @param	-
   **
   ** @return	-
   ** 
   ** @author	Diether Knof
   **
   ** @version	0.5.4
   **
   **/
  void
    BugReport::init()
    {
      this->ui->translations->add(*this, ::translator("FreeDoko %ttitle%",
						      ::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->translations->add(*(this->description),
				    ::translator("_bug report_"));
      this->file_label
	= Gtk::manage(new Gtk::Label("File: " + OS_NS::BUG_REPORT_FILE));
      this->ui->translations->add(*(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->translations->add(*(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->translations->add(*(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->translations->add(*(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);

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

  /**
   **
   ** load a report
   **
   ** @param	-
   **
   ** @return	-
   **
   ** @version	0.6.1
   **
   ** @author	Diether Knof
   **
   **/
  void
    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(this->ui->party());
	  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)

      return ;
    } // void BugReport::load_report()

  /**
   **
   ** creates a report
   **
   ** @param	-
   **
   ** @return	-
   **
   ** @version	0.5.4
   **
   ** @author	Diether Knof
   **
   **/
  void
    BugReport::create_report()
    {
      this->realize();

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

      this->present();

      return ;
    } // void BugReport::create_report()

  /**
   **
   ** save the report
   **
   ** @param	-
   **
   ** @return	-
   **
   ** @version	0.5.4
   **
   ** @author	Diether Knof
   **
   **/
  void
    BugReport::save()
    {
      ::bug_report.report(Glib::locale_from_utf8(this->message->get_buffer()->get_text() + "\n"));

      return ;
    } // 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()
    {
      ::setting.set(Setting::SAVE_BUG_REPORTS_ON_DESKTOP,
		    this->save_on_desktop_button->get_active());

      return ;
    } // 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)
    {
      if (!this->is_realized())
	return ;

      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)

      return ;
    } // void BugReport::setting_update(int const type)

} // namespace UI_GTKMM_NS

#endif // #ifdef USE_UI_GTKMM
