/**********************************************************************
 *
 *   FreeDoko a Doppelkopf-Trick
 *    
 *   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 "trick_summary.h"

#include "trick_drawing.h"
#include "ui.h"
#include "table.h"

#include "../../card/trick.h"

#include <gtkmm/alignment.h>
#include <gtkmm/drawingarea.h>
#include <gtkmm/label.h>
#include <gtkmm/treeview.h>

namespace UI_GTKMM_NS {

  /**
   **
   ** Constructor
   **
   ** @param	parent			parent widget
   ** @param	show_trick_picture	whether to show the trick picture
   **
   ** @return	-
   ** 
   ** @author	Diether Knof
   **
   ** @version	0.5.4
   **
   **/
  TrickSummary::TrickSummary(Base* const parent, bool const show_trick_picture) :
    Base(parent),
  HBox(),
  trick(NULL),
  show_trick_picture(show_trick_picture),
  trick_drawing(NULL),
  trick_picture(NULL),
  winner(NULL),
  points(NULL),
  specialpoints_model(),
  specialpoints_list(NULL),
  specialpoints_treeview(NULL)
  {
    DEBUG_CALLING(INFO_GTKMM_TRICK_SUMMARY && INFO_GTKMM_INIT,
		  "TrickSummary::TrickSummary(parent, show_trick_picture)");

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

    DEBUG_RETURNING(VOID,
		    INFO_GTKMM_TRICK_SUMMARY && INFO_GTKMM_INIT,
		    "TrickSummary::TrickSummary(parent, show_trick_picture)");
  } // TrickSummary::TrickSummary(Base* const parent, bool const show_trick_picture = true)

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

    delete this->trick_drawing;

    DEBUG_RETURNING(VOID,
		    INFO_GTKMM_TRICK_SUMMARY && INFO_GTKMM_INIT,
		    "TrickSummary::~TrickSummary()");
  } // TrickSummary::~TrickSummary()

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

      this->set_spacing(10);

      if (show_trick_picture) {
	this->trick_drawing = new TrickDrawing(this);
	if (this->trick != NULL)
	  this->trick_drawing->set_trick(*(this->trick));

	this->trick_picture = Gtk::manage(new Gtk::DrawingArea());
	this->trick_picture->set_size_request(this->trick_drawing->width()
					      * 6 / 5,
					      this->trick_drawing->height()
					      * 6 / 5);
	this->trick_picture->signal_expose_event().connect(SigC::slot(*this, &TrickSummary::trick_picture_expose));
	this->trick_picture->signal_configure_event().connect(SigC::slot(*this, &TrickSummary::trick_picture_configure));

	this->pack_start(*(this->trick_picture), false, true);
      } // if (show_trick_picture)

      Gtk::VBox* info_box = Gtk::manage(new Gtk::VBox(true));
      this->pack_end(*info_box);

      { // the labels
	Gtk::Alignment* alignment = Gtk::manage(new Gtk::Alignment(Gtk::ALIGN_CENTER, Gtk::ALIGN_BOTTOM, 0, 0));
	Gtk::VBox* vbox = Gtk::manage(new Gtk::VBox(false, 20));
	vbox->set_border_width(20);
	alignment->add(*vbox);

	this->winner = Gtk::manage(new Gtk::Label("Winner:"));
	this->ui->add_for_translation(*(this->winner),
				      ::translator("The trick goes to %swinner%.",
						   this->trick->winnerplayer().name()));
	vbox->pack_start(*(this->winner), false, true);

	this->points = Gtk::manage(new Gtk::Label("Points:"));
	this->ui->add_for_translation(*(this->points),
				      ::translator("Points in the trick: %upoints%",
						   this->trick->points()));

	vbox->pack_start(*(this->points), false, true);

	info_box->pack_start(*alignment, false, true);
      } // the labels

      { // specialpoints
	Gtk::Alignment* alignment = Gtk::manage(new Gtk::Alignment(Gtk::ALIGN_CENTER, Gtk::ALIGN_TOP, 0, 0));
	Gtk::VBox* vbox = Gtk::manage(new Gtk::VBox(false, 20));
	vbox->set_border_width(20);
	alignment->add(*vbox);

	this->specialpoints_list
	  = Gtk::ListStore::create(this->specialpoints_model);
	this->specialpoints_treeview
	  = Gtk::manage(new Gtk::TreeView(this->specialpoints_list));
	this->specialpoints_treeview->get_selection()->set_mode(Gtk::SELECTION_NONE);

	this->specialpoints_treeview->append_column("Specialpoint",
						    this->specialpoints_model.special_point);
	this->specialpoints_treeview->append_column("Player",
						    this->specialpoints_model.player);

	vbox->pack_start(*(this->specialpoints_treeview), false, true);

	info_box->pack_end(*alignment, false, true);
      } // specialpoints

      this->show_all();
      this->update();
      this->language_update();

      DEBUG_RETURNING(VOID,
		      INFO_GTKMM_TRICK_SUMMARY && INFO_GTKMM_INIT,
		      "TrickSummary::init()");
    } // void TrickSummary::init()

  /**
   **
   ** set the gameno
   **
   ** @param	trick	the trick to show the info of
   **
   ** @return	-
   **
   ** @version	0.6.1
   **
   ** @author	Diether Knof
   **
   **/
  void
    TrickSummary::set_trick(::Trick const& trick)
    {
      DEBUG_CALLING(INFO_GTKMM_TRICK_SUMMARY && INFO_GTKMM_VALUE,
		    "TrickSummary::set_trick(trick)");

      this->trick = &trick;
      if (this->trick_drawing != NULL)
	this->trick_drawing->set_trick(trick);
      this->update();

      DEBUG_RETURNING(VOID,
		      INFO_GTKMM_TRICK_SUMMARY && INFO_GTKMM_VALUE,
		      "TrickSummary::set_trick(trick)");
    } // void TrickSummary::set_trick(::Trick const& trick)

  /**
   **
   ** update (rewrite) all information
   **
   ** @param	-
   **
   ** @return	-
   **
   ** @version	0.6.1
   **
   ** @author	Diether Knof
   **
   ** @todo	if there is no 'player_of' don't show the column
   **
   **/
  void
    TrickSummary::update()
    {
      DEBUG_CALLING(INFO_GTKMM_TRICK_SUMMARY && INFO_GTKMM_UPDATE,
		    "TrickSummary::update()");

      if (!this->is_realized())
	DEBUG_RETURNING(VOID,
			INFO_GTKMM_TRICK_SUMMARY && INFO_GTKMM_UPDATE,
			"TrickSummary::update()");

      this->update_trick_picture();

      { // special points

	this->specialpoints_list->clear();

	bool valid_point = false;
	Specialpointsvector const specialpoints = this->trick->specialpoints();
	for (Specialpointsvector::const_iterator
	     sp = specialpoints.begin();
	     sp != specialpoints.end();
	     sp++) {
	  // test whether to show the point
	  if (sp->player_get_no == UINT_MAX)
#ifndef RELEASE
	    // no 'player of' (can this happen?)
	    DEBUG_ASSERTION(false,
			    "Gtkmm::TrickSummary(trick):\n"
			    "  no 'player_get_no' in specialpoint");
#else
	  continue;
#endif
	  else if (sp->player_of_no != UINT_MAX) {
	    Team const team_get = this->trick->game().teaminfo_for_humans(this->trick->game().player(sp->player_get_no));
	    Team const team_of = this->trick->game().teaminfo_for_humans(this->trick->game().player(sp->player_of_no));

	    if ( ( (team_get == TEAM::RE)
		   || (team_get == TEAM::CONTRA) )
		 && (team_get == team_of) )
	      continue;
	  } // if !(player_get_no == UINT_MAX) && (player_of_no != UINT_MAX)

	  valid_point |= true;

	  Gtk::TreeModel::Row row = *(this->specialpoints_list->append());
	  row[this->specialpoints_model.special_point]
	    = Glib::locale_to_utf8(::translator(::name(sp->type)).translation());
	  if (sp->player_of_no != UINT_MAX)
	    row[this->specialpoints_model.player]
	      = Glib::locale_to_utf8(this->trick->game().player(sp->player_of_no).name());
	  else
	    row[this->specialpoints_model.player] = "";
	} // for (sp \in specialpoints)

	this->specialpoints_treeview->show_all();

	if (valid_point)
	  this->specialpoints_treeview->show();
	else
	  this->specialpoints_treeview->hide();

      } // special points

      DEBUG_RETURNING(VOID,
		      INFO_GTKMM_TRICK_SUMMARY && INFO_GTKMM_UPDATE,
		      "TrickSummary::update()");
    } // void TrickSummary::update()

  /**
   **
   ** update the trick in the drawing area
   **
   ** @param	-
   **
   ** @return	-
   **
   ** @version	0.6.1
   **
   ** @author	Diether Knof
   **
   **/
  void
    TrickSummary::update_trick_picture()
    {
      DEBUG_CALLING(INFO_GTKMM_TRICK_SUMMARY && INFO_GTKMM_UPDATE,
		    "TrickSummary::update_trick_picture()");

      if ((this->trick == NULL)
	  || (this->trick_picture == NULL))
	DEBUG_RETURNING(VOID,
			INFO_GTKMM_TRICK_SUMMARY && INFO_GTKMM_UPDATE,
			"TrickSummary::update_trick_picture()");

#ifdef POSTPHONED
      this->trick_picture->get_window()->draw_rectangle(this->ui->table->gc,
							true,
							0, 0, -1, -1);
#endif
      this->trick_drawing->draw(this->trick_picture->get_window());


      DEBUG_RETURNING(VOID,
		      INFO_GTKMM_TRICK_SUMMARY && INFO_GTKMM_UPDATE,
		      "TrickSummary::update_trick_picture()");
    } // void TrickSummary::update_trick_picture()

  /**
   **
   ** event: redraw part of the window
   **
   ** @param	event	the event
   **
   ** @return	true
   **
   ** @version	0.6.1
   **
   ** @author	Diether Knof
   **
   **/
  bool
    TrickSummary::trick_picture_expose(GdkEventExpose* event)
    {
      DEBUG_CALLING(INFO_GTKMM_TRICK_SUMMARY && INFO_GTKMM_EVENT,
		    "TrickSummary::trick_picture_expose(event)");

      if (this->trick == NULL)
	DEBUG_RETURNING(true,
			INFO_GTKMM_TRICK_SUMMARY && INFO_GTKMM_EVENT,
			"Table::trick_picture_expose(event)");

      this->trick_drawing->draw(this->trick_picture->get_window());

      DEBUG_RETURNING(true,
		      INFO_GTKMM_TRICK_SUMMARY && INFO_GTKMM_EVENT,
		      "Table::trick_picture_expose(event)");
    } // bool TrickSummary::trick_picture_expose(GdkEventExpose* event)

  /**
   **
   ** event: the size has changed
   **
   ** @param	event	the event
   **
   ** @return	true
   **
   ** @version	0.6.1
   **
   ** @author	Diether Knof
   **
   **/
  bool
    TrickSummary::trick_picture_configure(GdkEventConfigure* event)
    {
      DEBUG_CALLING(INFO_GTKMM_TRICK_SUMMARY && INFO_GTKMM_EVENT,
		    "TrickSummary::trick_picture_configure(event)");

      this->trick_drawing->set_center(Gdk::Point(this->trick_picture->get_width() / 2,
						 this->trick_picture->get_height() / 2));
      this->update_trick_picture();

      DEBUG_RETURNING(true,
		      INFO_GTKMM_TRICK_SUMMARY && INFO_GTKMM_EVENT,
		      "TrickSummary::trick_picture_configure(event)");
    } // bool TrickSummary::trick_picture_configure(GdkEventConfigure* event)

  /**
   **
   ** updates the cards
   **
   ** @param	-
   **
   ** @return	-
   **
   ** @version	0.6.1
   **
   ** @author	Diether Knof
   **
   **/
  void
    TrickSummary::cards_update()
    {
      DEBUG_CALLING(INFO_GTKMM_TRICK_SUMMARY && INFO_GTKMM_UPDATE,
		    "TrickSummary::cards_update()");

      if (!this->is_realized())
	DEBUG_RETURNING(VOID,
			INFO_GTKMM_TRICK_SUMMARY && INFO_GTKMM_UPDATE,
			"TrickSummary::cards_update()");

      if (this->trick_picture != NULL)
	this->trick_picture->set_size_request(this->trick_drawing->width()
					      * 6 / 5,
					      this->trick_drawing->height()
					      * 6 / 5);

      DEBUG_RETURNING(VOID,
		      INFO_GTKMM_TRICK_SUMMARY && INFO_GTKMM_UPDATE,
		      "TrickSummary::cards_update()");
    } // void TrickSummary::cards_update()

  /**
   **
   ** the setting has changed
   **
   ** @param	type	the type that has changed
   **
   ** @return	-
   ** 
   ** @author	Diether Knof
   **
   ** @version	0.6.1
   **
   **/
  void
    TrickSummary::setting_update(int const type)
    {
      DEBUG_CALLING(INFO_GTKMM_TRICK_SUMMARY && INFO_GTKMM_UPDATE,
		    "TrickSummary::setting_update(type)");

      switch(type) {
      case Setting::ROTATE_TRICK_CARDS:
	if (this->trick_picture != NULL) {
	  this->trick_picture->set_size_request(this->trick_drawing->width()
						* 6 / 5,
						this->trick_drawing->height()
						* 6 / 5);
	  if (this->trick_picture->is_realized())
	    this->trick_picture->get_window()->clear();
	} // if (this->trick_picture != NULL)
	this->update_trick_picture();
	break;
      case Setting::LANGUAGE:
	this->update();
	break;
      default:
	break;
      } // switch(type)

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

  /**
   **
   ** updates all language elements
   **
   ** @param	-
   **
   ** @return	-
   **
   ** @version	0.6.1
   **
   ** @author	Diether Knof
   **
   **/
  void
    TrickSummary::language_update()
    {
      DEBUG_CALLING(INFO_GTKMM_TRICK_SUMMARY && INFO_GTKMM_UPDATE,
		    "TrickSummary::language_update()");

      if (!this->is_realized())
	DEBUG_RETURNING(VOID,
			INFO_GTKMM_TRICK_SUMMARY && INFO_GTKMM_UPDATE,
			"TrickSummary::language_update()");

      this->specialpoints_treeview->get_column(0)->set_title(Glib::locale_to_utf8(::translator("Specialpoint").translation()));
      this->specialpoints_treeview->get_column(1)->set_title(Glib::locale_to_utf8(::translator("of player").translation()));

      this->update();

      DEBUG_RETURNING(VOID,
		      INFO_GTKMM_TRICK_SUMMARY && INFO_GTKMM_UPDATE,
		      "TrickSummary::language_update()");
    } // void TrickSummary::language_update()

} // namespace UI_GTKMM_NS

#endif // #ifdef USE_UI_GTKMM
