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

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

#include "../../party/party.h"
#include "../../game/gamepointstable.h"
#include "../../utils/string.h"

#include <gtkmm/button.h>
#include <gtkmm/alignment.h>
#include <gtkmm/separator.h>
#include <gtkmm/label.h>
#include <gtkmm/main.h>
#include <gtkmm/treeview.h>
#include <gtkmm/scrolledwindow.h>

namespace UI_GTKMM_NS {

  /**
   **
   ** Constructor
   **
   ** @param	-
   **
   ** @return	-
   ** 
   ** @author	Diether Knof
   **
   ** @version	0.5.4
   **
   **/
  GameSummary::GameSummary(Base* const parent) :
    Base(parent),
  VBox(),
  gameno(UINT_MAX),
  gamenumber(NULL),
  type(NULL),
  winner(NULL),
  points(NULL),
  trickpoints_model(),
  trickpoints_list(NULL),
  trickpoints_treeview(NULL),
  points_description_model(),
  points_description_list(NULL),
  points_description_treeview(NULL)
  {
    DEBUG_CALLING(INFO_GTKMM_GAME_SUMMARY && INFO_GTKMM_INIT,
		  "GameSummary::GameSummary(parent)");

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

    DEBUG_RETURNING(VOID,
		    INFO_GTKMM_GAME_SUMMARY && INFO_GTKMM_INIT,
		    "GameSummary::GameSummary(parent)");
  } // GameSummary::GameSummary(Base* const parent)

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

    DEBUG_RETURNING(VOID,
		    INFO_GTKMM_GAME_SUMMARY && INFO_GTKMM_INIT,
		    "GameSummary::~GameSummary()");
  } // GameSummary::~GameSummary()

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

      this->set_spacing(10);

      this->gamenumber = Gtk::manage(new Gtk::Label("Game:"));
      this->ui->add_for_translation(*(this->gamenumber),
				    ::translator("Game %ugameno%",
						 0u));
      this->pack_start(*(this->gamenumber), false, true);

      this->type = Gtk::manage(new Gtk::Label("Game type:"));
      this->ui->add_for_translation(*(this->type),
				    ::translator("gametype: %ttype%",
						 ::translator(GAMETYPE::NORMAL)));

      this->pack_start(*(this->type), false, true);


      this->winner = Gtk::manage(new Gtk::Label("Winner:"));
      this->ui->add_for_translation(*(this->winner),
				    ::translator("Winner: no team"));
      this->pack_start(*(this->winner), false, true);


      { // trickpoints
	this->trickpoints_list
	  = Gtk::ListStore::create(this->trickpoints_model);
	this->trickpoints_treeview
	  = Gtk::manage(new Gtk::TreeView(this->trickpoints_list));
	this->trickpoints_treeview->get_selection()->set_mode(Gtk::SELECTION_NONE);

	this->trickpoints_treeview->append_column("Name",
						  this->trickpoints_model.name);
	this->trickpoints_treeview->append_column("Trick points",
						  this->trickpoints_model.points);
	this->trickpoints_treeview->get_column_cell_renderer(1)->property_xalign() = 0.5;
	this->trickpoints_treeview->append_column("Team",
						  this->trickpoints_model.team);

	Gtk::Alignment* alignment
	  = Gtk::manage(new Gtk::Alignment(Gtk::ALIGN_CENTER,
					   Gtk::ALIGN_CENTER,
					   0, 0));
	alignment->add(*(this->trickpoints_treeview));
	this->pack_start(*alignment, false, true);
      } // trickpoints

      this->points = Gtk::manage(new Gtk::Label("Game points:"));
      this->ui->add_for_translation(*(this->points),
				    ::translator("Game points: %ipoints%",
						 0
						)
				   );
      this->pack_start(*(this->points), false, true);

      { // points description
	this->points_description_list
	  = Gtk::ListStore::create(this->points_description_model);
	this->points_description_treeview
	  = Gtk::manage(new Gtk::TreeView(this->points_description_list));

	this->points_description_treeview->append_column("Team",
							 this->points_description_model.team);
	// ToDo: right-align the sign
	this->points_description_treeview->append_column("Player",
							 this->points_description_model.player);
#ifdef OUTDATED
	this->points_description_treeview->append_column("Sign",
							 this->points_description_model.sign);
	this->points_description_treeview->get_column_cell_renderer(2)->property_xalign() = 1;
#endif
	this->points_description_treeview->append_column("Points",
							 this->points_description_model.points);
	this->points_description_treeview->get_column_cell_renderer(2)->property_xalign() = 0.5;
	this->points_description_treeview->append_column("Description",
							 this->points_description_model.description);

#ifdef POSTPHONED
	// ToDo: make the scrolled window show the first four lines of the table
	Gtk::ScrolledWindow* scrolled_window
	  = Gtk::manage(new Gtk::ScrolledWindow);
	scrolled_window->set_policy(Gtk::POLICY_NEVER,
				    Gtk::POLICY_AUTOMATIC);
	scrolled_window->add(*(this->points_description_treeview));
	this->pack_start(*scrolled_window, true, true);
#else
	Gtk::Alignment* alignment
	  = Gtk::manage(new Gtk::Alignment(Gtk::ALIGN_CENTER,
					   Gtk::ALIGN_CENTER,
					   0, 1));
	alignment->add(*(this->points_description_treeview));
	this->pack_start(*alignment, true, true);
	//this->pack_start(*(this->points_description_treeview), true, true);
#endif
      } // trickpoints
      //this->pack_start(*(this->points_description));

      this->language_update();
      this->show_all_children();
      if (this->gameno != UINT_MAX)
	this->show();

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

      DEBUG_RETURNING(VOID,
		      INFO_GTKMM_GAME_SUMMARY && INFO_GTKMM_INIT,
		      "GameSummary::init()");
    } // void GameSummary::init()

  /**
   **
   ** set the gameno
   **
   ** @param	gameno	the new gameno
   **
   ** @return	-
   **
   ** @version	0.6.1
   **
   ** @author	Diether Knof
   **
   **/
  void
    GameSummary::set_gameno(unsigned const gameno)
    {
      DEBUG_CALLING(INFO_GTKMM_GAME_SUMMARY && INFO_GTKMM_VALUE,
		    "GameSummary::set_gameno(gameno)");

      this->gameno = gameno;
      this->update();

      DEBUG_RETURNING(VOID,
		      INFO_GTKMM_GAME_SUMMARY && INFO_GTKMM_VALUE,
		      "GameSummary::set_gameno(gameno)");
    } // void GameSummary::set_gameno(unsigned const gameno)

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

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

      if (this->gameno == UINT_MAX) {
	this->hide();
	DEBUG_RETURNING(VOID,
			INFO_GTKMM_GAME_SUMMARY && INFO_GTKMM_UPDATE,
			"GameSummary::update()");
      }
      this->show();

      GamePoints const& game_points
	= (static_cast<Party const&>(::party)).gamepointstable().gamepoints(this->gameno);

      this->ui->change_translation(*(this->gamenumber),
				   ::translator("Game %ugameno%",
						this->gameno + 1));
      this->ui->change_translation(*(this->type),
				   ::translator("gametype: %ttype%",
						::translator(game_points.gametype())));

      if (game_points.winnerteam() == TEAM::NOTEAM)
	this->ui->change_translation(*(this->winner),
				     ::translator("Winner: no team"));
      else if (game_points.teampoints(game_points.winnerteam()) == 1)
	this->ui->change_translation(*(this->winner),
				     ::translator("Winner: team %tteam% with %upoints% point",

						  ::translator(game_points.winnerteam()),
						  game_points.teampoints(game_points.winnerteam())
						 )
				    );
      else
	this->ui->change_translation(*(this->winner),
				     ::translator("Winner: team %tteam% with %upoints% points",

						  ::translator(game_points.winnerteam()),
						  game_points.teampoints(game_points.winnerteam())
						 )
				    );

      this->ui->change_translation(*(this->points),
				   ::translator("Game points: %ipoints%",
						game_points.points()
					       )
				  );

      { // trickpoints
	this->trickpoints_list->clear();

	for (unsigned p = 0; p < ::party.game().playerno(); p++) {
	  Gtk::TreeModel::Row row = *(this->trickpoints_list->append());
	  row[this->trickpoints_model.name] = Glib::locale_to_utf8(::party.game().player(p).name());
	  row[this->trickpoints_model.points] = game_points.playerspoints(p);
	  row[this->trickpoints_model.team]
	    = Glib::locale_to_utf8(::translator(game_points.team(p)).translation());
	  // ToDo: Mark the winner gray
	} // for (p < ::party.game().playerno())
      } // trickpoints

      { // points description
	if (game_points.pointlist().empty()) {
	  this->points_description_treeview->hide();
	} else { // if !(game_points.pointlist().empty())
	  this->points_description_treeview->show();

	  this->points_description_list->clear();

	  for (unsigned i = 0; i < game_points.pointlist().size(); i++) {
	    Gtk::TreeModel::Row row = *(this->points_description_list->append());
	    row[this->points_description_model.team]
	      = Glib::locale_to_utf8(::translator(game_points.pointlist()[i].team).translation());

	    if (game_points.pointlist()[i].player_get_no == UINT_MAX) {
	      if (game_points.pointlist()[i].player_of_no == UINT_MAX)
		row[this->points_description_model.player] = string();
	      else
		row[this->points_description_model.player]
		  = Glib::locale_to_utf8(::party.game().player(game_points.pointlist()[i].player_of_no).name());
	    } else {
	      row[this->points_description_model.player]
		= Glib::locale_to_utf8(::party.game().player(game_points.pointlist()[i].player_get_no).name());
	    }

	    row[this->points_description_model.description]
	      = Glib::locale_to_utf8(::translator(game_points.pointlist()[i].type).translation());

	    // show whether the points are added or subtracted
	    if ((game_points.winnerteam() == TEAM::RE)
		|| (game_points.winnerteam() == TEAM::CONTRA)) {
	      if (game_points.pointlist()[i].counting_team
		  == game_points.winnerteam())
		row[this->points_description_model.points] = "+";
	      else
		row[this->points_description_model.points] = "-";
	    } else { // if !(game_points.winnerteam() == RE, CONTRA)
	      if (game_points.pointlist()[i].counting_team == TEAM::RE)
		row[this->points_description_model.points] = "+";
	      else if (game_points.pointlist()[i].counting_team == TEAM::CONTRA)
		row[this->points_description_model.points] = "-";
	      else
		row[this->points_description_model.points] = " ";
	    } // if !(game_points.winnerteam() != RE, CONTRA)

	    if ((game_points.pointlist()[i].type == SPECIALPOINT::NO120_SAID)
		&& (::party.rule()(Rule::ANNOUNCEMENT_RE_DOUBLES))) {
	      row[this->points_description_model.points] = "* 2";
	    } else if (game_points.pointlist()[i].type == SPECIALPOINT::SOLO) {
	      // a solo
	      row[this->points_description_model.points] = "* 3";
	    } else {
	      // all other special points
	      row[this->points_description_model.points]
		= (row[this->points_description_model.points]
		   + " " + DK::Utils::String::to_string(Value_of_Specialpoint(game_points.pointlist()[i].type))
		  );

	    }
	  } // for (i < game_points.pointlist().size())
	} // if !(game_points.pointlist().empty())

      } // points description

      DEBUG_RETURNING(VOID,
		      INFO_GTKMM_GAME_SUMMARY && INFO_GTKMM_UPDATE,
		      "GameSummary::update()");
    } // void GameSummary::update()

  /**
   **
   ** the name of 'player' has changed
   **
   ** @param	player	the player with the changed name
   **
   ** @return	-
   **
   ** @version	0.6.4
   **
   ** @author	Diether Knof
   **
   **/
  void
    GameSummary::name_changed(Player const& player)
    {
      DEBUG_CALLING(INFO_GTKMM_GAME_SUMMARY && INFO_GTKMM_UPDATE,
		    "GameSummary::name_changed(player)");

      if (!this->is_realized())
	DEBUG_RETURNING(VOID,
			INFO_GTKMM_GAME_SUMMARY && INFO_GTKMM_UPDATE,
			"GameSummary::name_changed(player)");

      this->update();

      DEBUG_RETURNING(VOID,
		      INFO_GTKMM_GAME_SUMMARY && INFO_GTKMM_UPDATE,
		      "GameSummary::name_changed(player)");
    } // void GameSummary::name_changed(Player const& player)

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

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

      this->trickpoints_treeview->get_column(0)->set_title(Glib::locale_to_utf8(::translator("Name").translation()));
      this->trickpoints_treeview->get_column(1)->set_title(Glib::locale_to_utf8(::translator("Trick points").translation()));
      this->trickpoints_treeview->get_column(2)->set_title(Glib::locale_to_utf8(::translator("Team").translation()));

      this->points_description_treeview->get_column(0)->set_title(Glib::locale_to_utf8(::translator("Team").translation()));
      this->points_description_treeview->get_column(1)->set_title(Glib::locale_to_utf8(::translator("Player").translation()));
#if 0
      this->points_description_treeview->get_column(2)->set_title(Glib::locale_to_utf8(::translator("Sign").translation()));
#endif
      this->points_description_treeview->get_column(2)->set_title(Glib::locale_to_utf8(::translator("Point(s)").translation()));
      this->points_description_treeview->get_column(3)->set_title(Glib::locale_to_utf8(::translator("Description").translation()));

      this->update();

      DEBUG_RETURNING(VOID,
		      INFO_GTKMM_GAME_SUMMARY && INFO_GTKMM_UPDATE,
		      "GameSummary::language_update()");
    } // void GameSummary::language_update()

} // namespace UI_GTKMM_NS

#endif // #ifdef USE_UI_GTKMM
