/**********************************************************************
 *
 *   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 "gametype_info.h"
#include "table.h"
#include "ui.h"
#include "icons.h"

#include "../../game/game.h"
#include "../../player/player.h"
#include "../../misc/setting.h"

#include <gtkmm/box.h>
#include <gtkmm/image.h>
#include <gtkmm/label.h>

namespace UI_GTKMM_NS {

  // position of the window
  int GametypeInfo::position_x = INT_MAX;
  int GametypeInfo::position_y = INT_MAX;

  /**
   **
   ** Constructor
   **
   ** @param	table	the table
   ** @param	player	the player who has swines
   **
   **
   ** @return	-
   ** 
   ** @author	Diether Knof
   **
   ** @version	0.6.1
   **
   **/
  GametypeInfo::GametypeInfo(Table* const table, Game const& game) :
    GameinfoDialog(table),
  icon(NULL),
  text(NULL),
  game(&game)
  {
    DEBUG_CALLING(INFO_GTKMM_GAMETYPE_INFO && INFO_GTKMM_INIT,
		  "GametypeInfo::GametypeInfo(table, game)");

    DEBUG_RETURNING(VOID,
		    INFO_GTKMM_GAMETYPE_INFO && INFO_GTKMM_INIT,
		    "GametypeInfo::GametypeInfo(table, game)");
  } // GametypeInfo::GametypeInfo(Table* const table, Game const& game)

  /**
   **
   ** Destruktor
   **
   ** @param	-
   **
   ** @return	-
   ** 
   ** @author	Diether Knof
   **
   ** @version	0.6.1
   **
   **/
  GametypeInfo::~GametypeInfo()
  {
    DEBUG_CALLING(INFO_GTKMM_GAMETYPE_INFO && INFO_GTKMM_INIT,
		  "GametypeInfo::~GametypeInfo()");

    DEBUG_RETURNING(VOID,
		    INFO_GTKMM_GAMETYPE_INFO && INFO_GTKMM_INIT,
		    "GametypeInfo::~GametypeInfo()");
  } // GametypeInfo::~GametypeInfo()

  /**
   **
   ** init the dialog
   **
   ** @param	-
   **
   ** @return	-
   ** 
   ** @author	Diether Knof
   **
   ** @version	0.6.1
   **
   **/
  void
    GametypeInfo::init()
    {
      DEBUG_CALLING(INFO_GTKMM_GAMETYPE_INFO && INFO_GTKMM_INIT,
		    "GametypeInfo::init()");

      this->GameinfoDialog::init();

      this->ui->add_for_translation(*this,
				    ::translator(this->game->type()));

      this->icon = Gtk::manage(new Gtk::Image(this->ui->icons->icon(this->game->type())));
      this->text = Gtk::manage(new Gtk::Label("Gametype."));

      Gtk::VBox* vbox = Gtk::manage(new Gtk::VBox(false, 20));
      vbox->set_border_width(20);

      vbox->add(*(this->icon));
      vbox->add(*(this->text));

      this->get_vbox()->add(*vbox);

      this->show_all_children();

      this->language_update();

      DEBUG_RETURNING(VOID,
		      INFO_GTKMM_GAMETYPE_INFO && INFO_GTKMM_INIT,
		      "GametypeInfo::init()");
    } // void GametypeInfo::init()

  /**
   **
   ** show the window
   **
   ** @param	-
   **
   ** @return	-
   ** 
   ** @author	Diether Knof
   **
   ** @version	0.6.1
   **
   **/
  void
    GametypeInfo::show_information()
    {
      DEBUG_CALLING(INFO_GTKMM_GAMETYPE_INFO && INFO_GTKMM_GAMEPLAY,
		    "GametypeInfo::show_information()");

      this->GameinfoDialog::show_information(::setting(Setting::SHOW_GAMETYPE_WINDOW),
					     ::setting(Setting::CLOSE_GAMETYPE_WINDOW_AUTOMATICALLY),
					     ::setting(Setting::GAMETYPE_WINDOW_CLOSE_DELAY));

      DEBUG_RETURNING(VOID,
		      INFO_GTKMM_GAMETYPE_INFO && INFO_GTKMM_GAMEPLAY,
		      "GametypeInfo::show_information()");
    } // void GametypeInfo::show_information()

  /**
   **
   ** the name of 'player' has changed
   **
   ** @param	player	the player whose name has changed
   **
   ** @return	-
   ** 
   ** @author	Diether Knof
   **
   ** @version	0.6.1
   **
   **/
  void
    GametypeInfo::name_changed(Player const& player)
    {
      DEBUG_CALLING(INFO_GTKMM_GAMETYPE_INFO && INFO_GTKMM_UPDATE,
		    "GametypeInfo::name_changed(player)");

      if (this->game->type() != GAMETYPE::NORMAL)
	if (player == this->game->soloplayer())
	  this->language_update();

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

  /**
   **
   ** update the icons
   **
   ** @param	-
   **
   ** @return	-
   ** 
   ** @author	Diether Knof
   **
   ** @version	0.6.1
   **
   **/
  void
    GametypeInfo::icons_update()
    {
      DEBUG_CALLING(INFO_GTKMM_GAMETYPE_INFO && INFO_GTKMM_UPDATE,
		    "GametypeInfo::icons_update()");

      this->icon->set(this->ui->icons->icon(this->game->type()));

      DEBUG_RETURNING(VOID,
		      INFO_GTKMM_GAMETYPE_INFO && INFO_GTKMM_UPDATE,
		      "GametypeInfo::icons_update()");
    } // void GametypeInfo::icons_update()

  /**
   **
   ** update the language
   **
   ** @param	-
   **
   ** @return	-
   ** 
   ** @author	Diether Knof
   **
   ** @version	0.6.1
   **
   **/
  void
    GametypeInfo::language_update()
    {
      DEBUG_CALLING(INFO_GTKMM_GAMETYPE_INFO && INFO_GTKMM_UPDATE,
		    "GametypeInfo::language_update()");

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

      switch (this->game->type()) {
      case GAMETYPE::NORMAL:
	this->text->set_text(Glib::locale_to_utf8(::translator(this->game->type()).translation()));
	break;
      case GAMETYPE::FIVE_NINES:
      case GAMETYPE::SIX_KINGS:
      case GAMETYPE::FOX_HIGHEST_TRUMP:
	this->text->set_text(Glib::locale_to_utf8(::translator("%splayer% has %tgametype%.",
							       this->game->soloplayer().name(),
							       ::translator(this->game->type())
							      ).translation()
						 ));
	break;
      case GAMETYPE::POVERTY:
	this->text->set_text(Glib::locale_to_utf8(::translator("%splayer% has a %tgametype%.",
							       this->game->soloplayer().name(),
							       ::translator(this->game->type())
							      ).translation()
						 ));
	if (::game_status != GAMESTATUS::GAME_REDISTRIBUTE) {
	  for (vector<Player*>::const_iterator player
	       = this->game->player_begin();
	       player != this->game->player_end();
	       player++) {
	    if ((**player != this->game->soloplayer())
		&& ((*player)->team() == TEAM::RE)) {
	      string text = this->text->get_text();
#ifdef POSTPHONED
	      text +=  ("        \n        "
			+ ::translator("Partner is %splayer%.",
				       (*player)->name()));
#endif
	      if (this->game->soloplayer().hand().numberoftrumps() == 1)
		text +=  ("        \n        "
			  + ::translator("%splayer% has returned %utrumpno% trump.",
					 (*player)->name(),
					 this->game->soloplayer().hand().numberoftrumps()
					).translation());
	      else
		text +=  ("        \n        "
			  + ::translator("%splayer% has returned %utrumpno% trumps.",
					 (*player)->name(),
					 this->game->soloplayer().hand().numberoftrumps()
					).translation());
	      this->text->set_text(Glib::locale_to_utf8(text));

	    } // if (player is the partner)
	  } // for (player)
	} // if (::game_status != GAME_REDISTRIBUTE)
	break;
      case GAMETYPE::GENSCHER:
	// cannot happen
	break;
      case GAMETYPE::MARRIAGE:
      case GAMETYPE::MARRIAGE_SOLO:
	this->text->set_text(Glib::locale_to_utf8(::translator("%splayer% has a %tgametype%: %tselector%.",
							       this->game->soloplayer().name(),
							       ::translator(this->game->type()),
							       ::translator(this->game->marriage_selector())
							      ).translation()
						 ));
	break;
      case GAMETYPE::MARRIAGE_SILENT:
	// cannot happen
	break;
      case GAMETYPE::SOLO_MEATLESS:
      case GAMETYPE::SOLO_JACK:
      case GAMETYPE::SOLO_QUEEN:
      case GAMETYPE::SOLO_KING:
      case GAMETYPE::SOLO_QUEEN_JACK:
      case GAMETYPE::SOLO_KING_JACK:
      case GAMETYPE::SOLO_KING_QUEEN:
      case GAMETYPE::SOLO_KOEHLER:
      case GAMETYPE::SOLO_CLUB:
      case GAMETYPE::SOLO_SPADE:
      case GAMETYPE::SOLO_HEART:
      case GAMETYPE::SOLO_DIAMOND:
	this->text->set_text(Glib::locale_to_utf8(::translator("%splayer% plays a %tgametype%.",
							       this->game->soloplayer().name(),
							       ::translator(this->game->type())
							      ).translation()
						 ));
	break;
      } // switch (this->game->type())

      this->GameinfoDialog::language_update();

      DEBUG_RETURNING(VOID,
		      INFO_GTKMM_GAMETYPE_INFO && INFO_GTKMM_UPDATE,
		      "GametypeInfo::language_update()");
    } // void GametypeInfo::language_update()

  /**
   **
   ** save the position of the window
   **
   ** @param	-
   **
   ** @return	-
   **
   ** @version	0.6.4
   **
   ** @author	Diether Knof
   **
   **/
  void
    GametypeInfo::save_position()
    {
      // Note: 'Gtk::Window::get_position()' does not give the actual position
      this->get_window()->get_root_origin(this->position_x, this->position_y);

      return ;
    } // void GametypeInfo::save_position()

  /**
   **
   ** move the window to the last position
   **
   ** @param	-
   **
   ** @return	-
   **
   ** @version	0.6.4
   **
   ** @author	Diether Knof
   **
   **/
  void
    GametypeInfo::move_to_last_position()
    {
      if (this->position_x != INT_MAX)
	this->move(this->position_x, this->position_y);

      return ;
    } // void GametypeInfo::move_to_last_position()

} // namespace UI_GTKMM_NS

#endif // #ifdef USE_UI_GTKMM
