/**********************************************************************
 *
 *   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_GTK

#include "game_summary.h"
#include "gtk_gui.h"

#include "../../utils/string.h"
#include "../../party/party.h"
#include "../../party/rule.h"
#include "../../game/game.h"
#include "../../game/gamepointstable.h"
#include "../../misc/language.h"

namespace GTK_DOKO {
#include <gtk/gtkclist.h>
#include <gtk/gtkvbox.h>
#include <gtk/gtkvbbox.h>

  /*****************************************************************************
   *
   * GameSummary::GameSummary()
   *
   * Parameters:	none
   *
   * Result:	none
   * 
   * Version:	0.3.7
   *
   * Description:	Constructor
   *
   ****************************************************************************/
  GameSummary::GameSummary() :
    gameno_p(UINT_MAX), // the gamenumber
  container(gtk_vbox_new(FALSE, 0)), // the container
  gamenumber(gtk_label_new("")), // the gamenumber
  result(gtk_clist_new(3)), // the trick points and team of the players
  type(gtk_label_new("")), // the gametype
  winner(gtk_label_new("")), // the winnerteam
  points(gtk_label_new("")), // the points
  points_description(gtk_clist_new(3)) //  the description of the points
  {
    DEBUG_CALLING(INFO_GTK_GAME_SUMMARY && INFO_GTK_INIT,
		  "GameSummary::GameSummary()");

    { // the table with the results
      char* row[] = {"Name", "Point(s)", "Team"};

      gtk_clist_column_titles_show(GTK_CLIST(result));

      for (unsigned c = 0; c < 3; c++)
	gtk_clist_set_column_auto_resize(GTK_CLIST(result),
					 c, TRUE);

      for (unsigned p = 0; p < ::party.playerno(); p++)
	gtk_clist_append(GTK_CLIST(result), row);

      gtk_clist_set_column_justification(GTK_CLIST(result), 1,
					 GTK_JUSTIFY_CENTER);
    } // the table with the results

    { // the table with the description of the points
      gtk_clist_column_titles_show(GTK_CLIST(points_description));

      for (unsigned c = 0; c < 2; c++)
	gtk_clist_set_column_auto_resize(GTK_CLIST(points_description),
					 c, TRUE);
    } // the table with the description of the points

    GtkWidget* label_container = gtk_vbutton_box_new();

    GtkWidget* gamenumber_alignment = gtk_alignment_new(0.5, 0.5, 0, 0);
    gtk_container_set_border_width(GTK_CONTAINER(gamenumber_alignment), 10);
    gtk_container_add(GTK_CONTAINER(gamenumber_alignment), gamenumber);
    gtk_box_pack_start(GTK_BOX(container), gamenumber_alignment,
		       FALSE, TRUE, 0);
    gtk_box_pack_start(GTK_BOX(container), result,
		       FALSE, TRUE, 0);
    gtk_box_pack_start(GTK_BOX(container), label_container,
		       FALSE, TRUE, 0);
    gtk_box_pack_start(GTK_BOX(container), points_description,
		       TRUE, TRUE, 0);

    gtk_container_add(GTK_CONTAINER(label_container), type);
    gtk_container_add(GTK_CONTAINER(label_container), winner);
    gtk_container_add(GTK_CONTAINER(label_container), points);

    DEBUG_RETURNING(VOID,
		    INFO_GTK_GAME_SUMMARY && INFO_GTK_INIT,
		    "GameSummary::GameSummary()");
  } // GameSummary::GameSummary()

  /*****************************************************************************
   *
   * void GameSummary::init()
   *
   * Parameters:	none
   *
   * Result:	none
   * 
   * Version:	0.3.7
   *
   * Description:	inits the game summary
   *
   ****************************************************************************/
  void
    GameSummary::init()
    {
      DEBUG_CALLING(INFO_GTK_GAME_SUMMARY && INFO_GTK_INIT,
		    "GameSummary::init()");

      gtk_clist_set_column_justification(GTK_CLIST(points_description), 0,
					 GTK_JUSTIFY_CENTER);
      gtk_clist_set_column_justification(GTK_CLIST(points_description), 1,
					 GTK_JUSTIFY_CENTER);

      gtk_widget_show_all(this->container);

      this->language_update();

      DEBUG_RETURNING(VOID,
		      INFO_GTK_GAME_SUMMARY && INFO_GTK_INIT,
		      "GameSummary::init()");
    } // void GameSummary::init()

  /*****************************************************************************
   *
   * int GameSummary::gameno() const
   *
   * Parameters:	none
   *
   * Result:	the gamenumber
   * 
   * Version:	0.3.7
   *
   * Description:	Result
   *
   ****************************************************************************/
  unsigned
    GameSummary::gameno() const
    {
      DEBUG_CALLING(INFO_GTK_GAME_SUMMARY && INFO_GTK_VALUE,
		    "GameSummary::gameno()");

      DEBUG_RETURNING(this->gameno_p,
		      INFO_GTK_GAME_SUMMARY && INFO_GTK_VALUE,
		      "GameSummary::gameno()");
    } // int GameSummary::gameno() const

  /*****************************************************************************
   *
   * int GameSummary::gameno_set(unsigned const gameno)
   *
   * Parameters:	gameno_a - the new gamenumber
   *
   * Result:	the gamenumber
   * 
   * Version:	0.3.7
   *
   * Description:	sets the gamenumber
   *
   ****************************************************************************/
  unsigned
    GameSummary::gameno_set(unsigned const gameno)
    {
      DEBUG_CALLING(INFO_GTK_GAME_SUMMARY && INFO_GTK_VALUE,
		    "GameSummary::gameno_set()");

      DEBUG_ASSERTION((gameno < (static_cast<Party const&>(::party)).gamepointstable().gameno()),
		      "GTK_DOKO::GameSummary::gameno_set(gameno):\n"
		      "  'gameno' (" << gameno << " not valid (>= "
		      << (static_cast<Party const&>(::party)).gamepointstable().gameno());


      this->gameno_p = gameno;

      this->language_update();

      DEBUG_RETURNING(this->gameno(),
		      INFO_GTK_GAME_SUMMARY && INFO_GTK_VALUE,
		      "GameSummary::gameno_set()");
    } // int GameSummary::gameno_set(unsigned const gameno)

  /*****************************************************************************
   *
   * GtkWidget* GameSummary::widget()
   *
   * Parameters:	none
   *
   * Result:	widget with the menu
   * 
   * Version:	0.3.7
   *
   * Description:	Result
   *
   ****************************************************************************/
  GtkWidget*
    GameSummary::widget()
    {
      DEBUG_CALLING(INFO_GTK_GAME_SUMMARY && INFO_GTK_VALUE,
		    "GameSummary::widget()");

      DEBUG_ASSERTION((container != NULL),
		      "GameSummary::Game::widget()\n"
		      "  `container` == NULL");

      DEBUG_RETURNING(container,
		      INFO_GTK_GAME_SUMMARY && INFO_GTK_VALUE,
		      "GameSummary::widget()");
    } // GtkWidget* GameSummary::widget()


  /*****************************************************************************
   *
   * void GameSummary::language_update()
   *
   * Parameters:	none
   *
   * Result:	none
   * 
   * Version:	0.3.7
   *
   * Description:	updates the language in the gametype window
   *
   ****************************************************************************/
  void
    GameSummary::language_update()
    {
      DEBUG_CALLING(INFO_GTK_GAME_SUMMARY && INFO_GTK_UPDATE,
		    "GameSummary::language_update()");

      if ((game_status & GAMESTATUS::GAME)
	  || (game_status == GAMESTATUS::PARTY_PLAY)
	  || (game_status == GAMESTATUS::PARTY_FINISHED)) {

	if (this->gameno() != UINT_MAX) {

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

	  gtk_label_set_text(GTK_LABEL(gamenumber),
			     language("Game %igameno%", this->gameno() + 1).c_str());

	  { // table with the results
	    gtk_clist_freeze(GTK_CLIST(result));
	    gtk_clist_set_column_title(GTK_CLIST(result), 0,
				       (" " + language("Name") + " ").c_str());
	    gtk_clist_set_column_title(GTK_CLIST(result), 1,
				       (" " + language("Trick points") + " ").c_str());
	    gtk_clist_set_column_title(GTK_CLIST(result), 2,
				       (" " + language("Team") + " ").c_str());
	    for (unsigned p = 0; p < ::party.playerno(); p++) {
	      gtk_clist_set_text(GTK_CLIST(result), p, 0,
				 ::party.player(p).name().c_str());

	      gtk_clist_set_text(GTK_CLIST(result), p, 1,
				 DK::Utils::String::to_string(game_points.playerspoints(p)).c_str());
	      gtk_clist_set_text(GTK_CLIST(result), p, 2,
				 language(game_points.team(p)).c_str());
	    }
	    gtk_clist_thaw(GTK_CLIST(result));
	  } // table with the results

	  // the game type
	  gtk_label_set_text(GTK_LABEL(type),
			     (language("gametype") + ": " +
			      language(game_points.gametype())).c_str());

	  // label with the winner
	  if (game_points.teampoints(game_points.winnerteam()) == 1)
	    gtk_label_set_text(GTK_LABEL(winner),
			       language("Winner: team %steam% with %ipoints% point",

					language(game_points.winnerteam()).c_str(),
					game_points.teampoints(game_points.winnerteam())
				       ).c_str()
			      );
	  else
	    gtk_label_set_text(GTK_LABEL(winner),
			       language("Winner: team %steam% with %ipoints% points",

					language(game_points.winnerteam()).c_str(),
					game_points.teampoints(game_points.winnerteam())
				       ).c_str()
			      );



	  // label with the points
	  gtk_label_set_text(GTK_LABEL(points),
			     language("Game points: %ipoints%",
				      game_points.points()
				     ).c_str()
			    );

	  { // table with the description of the points
	    gtk_clist_freeze(GTK_CLIST(points_description));
	    gtk_clist_set_column_title(GTK_CLIST(points_description), 0,
				       (" " + language("Team") + " ").c_str());
	    gtk_clist_set_column_title(GTK_CLIST(points_description), 1,
				       (" " + language("Point(s)") + " ").c_str());
	    gtk_clist_set_column_title(GTK_CLIST(points_description), 2,
				       (" " + language("Description") + " ").c_str());

	    // remove all other rows
	    gtk_clist_clear(GTK_CLIST(points_description));

	    // create the descriptions
	    for (unsigned i = 0; i < game_points.pointlist().size(); i++) {
	      const char* text[3];
	      string text_str[3];
	      text_str[0] = language(game_points.pointlist()[i].team).c_str();
	      text_str[2] = language(game_points.pointlist()[i].type).c_str();

	      if ((game_points.pointlist()[i].type == SPECIALPOINT::NO120_SAID)
		  && (::party.rule()(Rule::ANNOUNCEMENT_RE_DOUBLES)))
		text_str[1] = "*2";
	      else if (game_points.pointlist()[i].type == SPECIALPOINT::SOLO)
		// a solo
		text_str[1] = "*3";
	      else 
		// all other special points
		text_str[1] = DK::Utils::String::to_string(Value_of_Specialpoint(game_points.pointlist()[i].type)
							  ).c_str();

	      // show whether the points are added or subtracted
	      if (game_points.winnerteam() != TEAM::NOTEAM) {
		if (game_points.pointlist()[i].counting_team
		    == game_points.winnerteam())
		  text_str[1] += " (+)";
		else
		  text_str[1] += " (-)";
	      } else { // if !(game_points.winnerteam() != TEAM::UNKNOWN)
		if (game_points.pointlist()[i].counting_team == TEAM::RE)
		  text_str[1] += " (+)";
		else
		  text_str[1] += " (-)";
	      } // if !(game_points.winnerteam() != TEAM::UNKNOWN)



	      for (unsigned j = 0; j < 3; j++)
		text[j] = text_str[j].c_str();

	      gtk_clist_insert(GTK_CLIST(points_description), i,
			       (gchar**)text);
	    } // table with the description of the points

	    // set the size so that all is shown
	    for (unsigned j = 0; j < 3; j++)
	      gtk_clist_set_column_width(GTK_CLIST(points_description), j,
					 gtk_clist_optimal_column_width(GTK_CLIST(points_description),
									j));


	    // show the table
	    gtk_clist_thaw(GTK_CLIST(points_description));
	  } // table with the description of the points

	} // if (gameno() != UINT_MAX)
      } // if (game_status == GAME || PARTY_PLAY || PARTY_FINISHED)

      DEBUG_RETURNING(VOID,
		      INFO_GTK_GAME_SUMMARY && INFO_GTK_UPDATE,
		      "GameSummary::language_update()");
    } // void GameSummary::language_update()

}
#endif
