/**********************************************************************
 *
 *   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 "players.h"
#include "player.h"
#include "aiconfigs.h"

#include "gtk_gui.h"

#include "../../party/party.h"

namespace GTK_DOKO {

  /**
   **
   ** Constructor
   **
   ** @param	party_settings	pointer to the party settings
   **
   ** @return	-
   **
   ** @version	0.5.3
   **
   ** @author	Diether Knof
   **
   **/
  PartySettings::Players::Players(PartySettings* const party_settings) :
    party_settings_(party_settings),
  aiconfigs_(NULL),
  player_(),
  notebook(gtk_notebook_new()),
  player_label()
  {
    DEBUG_CALLING(INFO_GTK_PLAYERS && INFO_GTK_INIT,
		  "PartySettings::Players::Players()");

    // the subnotebook with the players
    for (unsigned p = 0; p < ::party.playerno(); p++) {
      this->player_.push_back(new Player(this, p));
      this->player_label.push_back(gtk_label_new("playername"));
      gtk_notebook_append_page(GTK_NOTEBOOK(this->notebook),
			       this->player(p).widget(),
			       this->player_label[p]);
    } // for (p < ::party.playerno())


    this->aiconfigs_ = new AiConfigs(this);

    gtk_signal_connect(GTK_OBJECT(this->notebook), "switch-page",
		       GTK_SIGNAL_FUNC(switch_page_event),
		       this);

    this->language_update();

    // show all widgets
    gtk_widget_show_all(this->widget());


    DEBUG_RETURNING(VOID,
		    INFO_GTK_PLAYERS && INFO_GTK_INIT,
		    "PartySettings::Players::Players()");
  } // PartySettings::Players::Players(PartySettings* party_settings)

  /**
   **
   ** Destructor
   **
   ** @param	-
   **
   ** @return	-
   **
   ** @version	0.5.3
   **
   ** @author	Diether Knof
   **
   **/
  PartySettings::Players::~Players()
  {
    DEBUG_CALLING(INFO_GTK_PLAYERS && INFO_GTK_INIT,
		  "PartySettings::Players::~Players()");

    for (vector<Player*>::iterator player = this->player_.begin();
	 player != this->player_.end();
	 player++)
      delete *player;
    delete this->aiconfigs_;

    DEBUG_RETURNING(VOID,
		    INFO_GTK_PLAYERS && INFO_GTK_INIT,
		    "PartySettings::Players::~Players()");
  } // PartySettings::Players::~Players()

  /**
   **
   ** update the senisitivity
   **
   ** @param	-
   **
   ** @return	-
   **
   ** @version	0.5.3
   **
   ** @author	Diether Knof
   **
   **/
  void
    PartySettings::Players::sensitivity_update()
    {
      DEBUG_CALLING(INFO_GTK_PLAYERS && INFO_GTK_INIT,
		    "PartySettings::Players::sensitivity_update()");

      for (vector<Player*>::iterator player = this->player_.begin();
	   player != this->player_.end();
	   player++)
	(*player)->sensitivity_update();
      this->aiconfigs().sensitivity_update();

      DEBUG_RETURNING(VOID,
		      INFO_GTK_PLAYERS && INFO_GTK_INIT,
		      "PartySettings::Players::sensitivity_update()");
    } // void PartySettings::Players::sensitivity_update()

  /**
   **
   ** create a backup
   **
   ** @param	-
   **
   ** @return	-
   **
   ** @version	0.5.3
   **
   ** @author	Diether Knof
   **
   **/
  void
    PartySettings::Players::backup()
    {
      DEBUG_CALLING(INFO_GTK_PLAYERS && INFO_GTK_VALUE,
		    "PartySettings::Players::backup()");

      for (vector<Player*>::iterator player = this->player_.begin();
	   player != this->player_.end();
	   player++)
	(*player)->backup();

      DEBUG_RETURNING(VOID,
		      INFO_GTK_PLAYERS && INFO_GTK_VALUE,
		      "PartySettings::Players::backup()");
    } // void PartySettings::Players::backup()

  /**
   **
   ** destroy the backup
   **
   ** @param	-
   **
   ** @return	-
   **
   ** @version	0.5.3
   **
   ** @author	Diether Knof
   **
   **/
  void
    PartySettings::Players::backup_destroy()
    {
      DEBUG_CALLING(INFO_GTK_PLAYERS && INFO_GTK_VALUE,
		    "PartySettings::Players::backup_destroy()");

      for (vector<Player*>::iterator player = this->player_.begin();
	   player != this->player_.end();
	   player++)
	(*player)->backup_destroy();

      DEBUG_RETURNING(VOID,
		      INFO_GTK_PLAYERS && INFO_GTK_VALUE,
		      "PartySettings::Players::backup_destroy()");
    } // void PartySettings::Players::backup_destroy()

  /**
   **
   ** reset all settings
   **
   ** @param	-
   **
   ** @return	-
   **
   ** @version	0.5.3
   **
   ** @author	Diether Knof
   **
   **/
  void
    PartySettings::Players::reset()
    {
      DEBUG_CALLING(INFO_GTK_PLAYERS && INFO_GTK_EVENT,
		    "PartySettings::Players::reset()");

      for (vector<Player*>::iterator player = this->player_.begin();
	   player != this->player_.end();
	   player++)
	(*player)->reset();

      DEBUG_RETURNING(VOID,
		      INFO_GTK_PLAYERS && INFO_GTK_EVENT,
		      "PartySettings::Players::reset()");
    } // void PartySettings::Players::reset()

  /**
   **
   ** -> result
   **
   ** @param	-
   **
   ** @return	the gui
   **
   ** @version	0.5.3
   **
   ** @author	Diether Knof
   **
   **/
  GTK_GUI&
    PartySettings::Players::gui()
    {
      DEBUG_CALLING(INFO_GTK_PLAYERS && INFO_GTK_VALUE,
		    "PartySettings::Players::gui()");

      DEBUG_RETURNING(this->party_settings().gui(),
		      INFO_GTK_PLAYERS && INFO_GTK_VALUE,
		      "PartySettings::Players::gui()");
    } // GTK_GUI& PartySettings::Players::gui()

  /**
   **
   ** -> result
   **
   ** @param	-
   **
   ** @return	the party settings
   **
   ** @version	0.5.3
   **
   ** @author	Diether Knof
   **
   **/
  PartySettings&
    PartySettings::Players::party_settings()
    {
      DEBUG_CALLING(INFO_GTK_PLAYERS && INFO_GTK_VALUE,
		    "PartySettings::Players::party_settings()");

      DEBUG_ASSERTION((this->party_settings_ != NULL),
		      "PartySettings::Players::party_settings()\n"
		      "  `party_settings_` == NULL");

      DEBUG_RETURNING(*(this->party_settings_),
		      INFO_GTK_PLAYERS && INFO_GTK_VALUE,
		      "PartySettings::Players::party_settings()");
    } // PartySettings& PartySettings::Players::party_settings()

  /**
   **
   ** -> result
   **
   ** @param	-
   **
   ** @return	the widget (notebook)
   **
   ** @version	0.5.3
   **
   ** @author	Diether Knof
   **
   **/
  GtkWidget*
    PartySettings::Players::widget()
    {
      DEBUG_CALLING(INFO_GTK_PLAYERS && INFO_GTK_VALUE,
		    "PartySettings::Players::widget()");

      DEBUG_RETURNING(this->notebook,
		      INFO_GTK_PLAYERS && INFO_GTK_VALUE,
		      "PartySettings::Players::widget()");
    } // GtkWidget* PartySettings::Players::widget()

  /**
   **
   ** -> result
   **
   ** @param	playerno	the number of the player
   **
   ** @return	the player 'playerno'
   **
   ** @version	0.5.3
   **
   ** @author	Diether Knof
   **
   **/
  PartySettings::Players::Player&
    PartySettings::Players::player(unsigned const playerno)
    {
      DEBUG_CALLING(INFO_GTK_SETTINGS && INFO_GTK_VALUE,
		    "PartySettings::Players::player()");

      DEBUG_ASSERTION((playerno < ::party.playerno()),
		      "PartySettings::Players::player()\n"
		      "  `playerno` not valid");
      DEBUG_ASSERTION((this->player_[playerno] != NULL),
		      "PartySettings::Players::player()\n"
		      "  `player_[playerno]` == NULL");

      DEBUG_RETURNING(*(this->player_[playerno]),
		      INFO_GTK_SETTINGS && INFO_GTK_VALUE,
		      "PartySettings::Players::player()");
    } // Player& PartySettings::Players::player(unsigned const playerno)

  /**
   **
   ** -> result
   **
   ** @param	-
   **
   ** @return	the aiconfigs
   **
   ** @version	0.5.3
   **
   ** @author	Diether Knof
   **
   **/
  PartySettings::Players::AiConfigs&
    PartySettings::Players::aiconfigs()
    {
      DEBUG_CALLING(INFO_GTK_PLAYERS && INFO_GTK_VALUE,
		    "PartySettings::Players::aiconfigs()");

      DEBUG_ASSERTION((this->aiconfigs_ != NULL),
		      "PartySettings::Players::aiconfigs()\n"
		      "  `aiconfigs_` == NULL");

      DEBUG_RETURNING(*(this->aiconfigs_),
		      INFO_GTK_PLAYERS && INFO_GTK_VALUE,
		      "PartySettings::Players::aiconfigs()");
    } // PartySettings::Players::AiConfigs& PartySettings::Players::aiconfigs()

  /**
   **
   ** event: switch to a new page of the notebook
   ** 
   ** @param	page_num	the new page
   ** @param	players		players of the event
   **
   ** @return	-
   **
   ** @version	0.5.3
   **
   ** @author	Diether Knof
   **
   **/
  void
    PartySettings::Players::switch_page_event(GtkNotebook*, GtkNotebookPage*,
					      gint page_num, gpointer player)
    {
      DEBUG_CALLING(INFO_GTK_PLAYERS && INFO_GTK_EVENT,
		    "PartySettings::Players::switch_page_event(playerno)");

      static_cast<Players*>(player)->switch_page(page_num);

      DEBUG_RETURNING(VOID,
		      INFO_GTK_PLAYERS && INFO_GTK_EVENT,
		      "PartySettings::Players::switch_page_event(playerno)");
    } // static void PartySettings::Players::switch_page_event(GtkNotebook*, GtkNotebookPage*, gint page_num, gpointer player)

  /**
   **
   ** switch the aiconfig to the same page
   ** 
   ** @param	page_num	page number
   **
   ** @return	-
   **
   ** @version	0.5.3
   **
   ** @author	Diether Knof
   **
   **/
  void
    PartySettings::Players::switch_page(unsigned const page_num)
    {
      DEBUG_CALLING(INFO_GTK_PLAYERS && INFO_GTK_EVENT,
		    "PartySettings::Players::switch_page(page_num)");

      if (this->aiconfigs_ != NULL)
	this->aiconfigs().switch_player(page_num);

      DEBUG_RETURNING(VOID,
		      INFO_GTK_PLAYERS && INFO_GTK_EVENT,
		      "PartySettings::Players::switch_page(page_num)");
    } // void PartySettings::Players::switch_page(unsigned const page_num)

  /**
   **
   ** the default name of the user has changed
   **
   ** @param	-
   **
   ** @return	-
   **
   ** @version	0.5.3
   **
   ** @author	Diether Knof
   **
   **/
  void
    PartySettings::Players::update()
    {
      DEBUG_CALLING(INFO_GTK_PLAYERS && INFO_GTK_UPDATE,
		    "PartySettings::Players::update()");

      for (unsigned p = 0; p < ::party.playerno(); p++)
	this->player_update(::party.player(p));
      if (this->aiconfigs_ != NULL)
	this->aiconfigs().update();

      this->language_update();

      DEBUG_RETURNING(VOID,
		      INFO_GTK_PLAYERS && INFO_GTK_UPDATE,
		      "PartySettings::Players::update()");
    } // void PartySettings::Players::update()

  /**
   **
   ** updates the player
   **
   ** @param	player	player to be updated
   **
   ** @return	-
   **
   ** @version	0.5.3
   **
   ** @author	Diether Knof
   **
   **/
  void
    PartySettings::Players::name_update(::Player const& player)
    {
      DEBUG_CALLING(INFO_GTK_PLAYERS && INFO_GTK_UPDATE,
		    "PartySettings::Players::name_update(player)");

      this->player(player.no()).name_update();
      if (this->aiconfigs_ != NULL)
	this->aiconfigs().name_update(player);

      gtk_label_set(GTK_LABEL(this->player_label[player.no()]),
		    player.name().c_str());

      DEBUG_RETURNING(VOID,
		      INFO_GTK_PLAYERS && INFO_GTK_UPDATE,
		      "PartySettings::Players::name_update(player)");
    } // void PartySettings::Players::name_update(::Player const& player)

  /**
   **
   ** updates the player
   **
   ** @param	player	player to be updated
   **
   ** @return	-
   **
   ** @version	0.5.3
   **
   ** @author	Diether Knof
   **
   **/
  void
    PartySettings::Players::player_update(::Player const& player)
    {
      DEBUG_CALLING(INFO_GTK_PLAYERS && INFO_GTK_UPDATE,
		    "PartySettings::Players::player_update(player)");


      this->player(player.no()).update();
      if (this->aiconfigs_ != NULL)
	this->aiconfigs().player_update(player);

      gtk_label_set(GTK_LABEL(this->player_label[player.no()]),
		    player.name().c_str());

      DEBUG_RETURNING(VOID,
		      INFO_GTK_PLAYERS && INFO_GTK_UPDATE,
		      "PartySettings::Players::player_update(player)");
    } // void PartySettings::Players::player_update(::Player const& player)

  /**
   **
   ** updates the language
   **
   ** @param	-
   **
   ** @return	-
   **
   ** @version	0.5.3
   **
   ** @author	Diether Knof
   **
   **/
  void
    PartySettings::Players::language_update()
    {
      DEBUG_CALLING(INFO_GTK_PLAYERS && INFO_GTK_UPDATE,
		    "PartySettings::Players::language_update()");

      for (vector<Player*>::iterator player = this->player_.begin();
	   player != this->player_.end();
	   player++)
	(*player)->language_update();

      if (this->aiconfigs_ != NULL)
	this->aiconfigs().language_update();

      DEBUG_RETURNING(VOID,
		      INFO_GTK_PLAYERS && INFO_GTK_UPDATE,
		      "PartySettings::Players::language_update()");
    } // void PartySettings::Players::language_update()

} // namespace GTK_DOKO
#endif // #ifdef USE_UI_GTK
