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

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

namespace UI_GTKMM_NS {

  /**
   **
   ** Constructor
   **
   ** @param	-
   **
   ** @return	-
   ** 
   ** @author	Diether Knof
   **
   ** @version	0.5.4
   **
   **/
  Players::Players(Base* const parent) :
    Base(parent),
  Notebook(),
  aiconfigs(NULL),
  player_()
  {
    DEBUG_CALLING(INFO_GTKMM_PLAYERS && INFO_GTKMM_INIT,
		  "Players::Players(parent)");

#ifdef POSTPHONED
    this->signal_realize().connect(SigC::slot(*this, &Rules::init));
#else
    this->init();
#endif

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

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

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

    DEBUG_RETURNING(VOID,
		    INFO_GTKMM_PLAYERS && INFO_GTKMM_INIT,
		    "Players::~Players()");
  } // Players::~Players()

  /**
   **
   ** creates all subelements
   **
   ** @param	-
   **
   ** @return	-
   ** 
   ** @author	Diether Knof
   **
   ** @version	0.5.4
   **
   **/
  void
  Players::init()
  {
    DEBUG_CALLING(INFO_GTKMM_PLAYERS && INFO_GTKMM_INIT,
		  "Players::init()");

    this->aiconfigs = new AiConfigs(this);

    for (unsigned p = 0; p < ::party.playerno(); p++)
      this->player_.push_back(new Player(this, p));


    for (vector<Player*>::iterator p = this->player_.begin();
	 p != this->player_.end();
	 p++)
      this->append_page(**p, *((*p)->name_bookmark));

#ifdef POSTPHONED
    this->Notebook::signal_change_current_page().connect(SigC::slot(*(this->aiconfigs->notebook),
								    &Gtk::Notebook::set_current_page));
#endif

    this->show_all();

    DEBUG_RETURNING(VOID,
		    INFO_GTKMM_PLAYERS && INFO_GTKMM_INIT,
		    "Players::init()");
  } // void Players::init()

  /**
   **
   ** -> result
   **
   ** @param	playerno	the player number
   **
   ** @return	the player configuration of player 'playerno'
   **
   ** @version	0.5.4
   **
   ** @author	Diether Knof
   **
   **/
  Players::Player&
    Players::player(unsigned const playerno)
    {
      DEBUG_CALLING(INFO_GTKMM_PLAYERS && INFO_GTKMM_VALUE,
		    "Players::player()");

      DEBUG_ASSERTION((this->player_.size() > playerno),
		      "Players::player(playerno):\n"
		      "  playerno too great: "
		      << playerno << " >= " << this->player_.size());

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

  /**
   **
   ** creates a backup
   **
   ** @param	-
   **
   ** @return	-
   **
   ** @version	0.5.4
   **
   ** @author	Diether Knof
   **
   **/
  void
    Players::create_backup()
    {
      DEBUG_CALLING(INFO_GTKMM_PLAYERS && INFO_GTKMM_VALUE,
		    "Players::create_backup()");

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

      DEBUG_RETURNING(VOID,
		      INFO_GTKMM_PLAYERS && INFO_GTKMM_VALUE,
		      "Players::create_backup()");
    } // void Players::create_backup()

  /**
   **
   ** resets the settings
   **
   ** @param	-
   **
   ** @return	-
   **
   ** @version	0.5.4
   **
   ** @author	Diether Knof
   **
   ** @todo	set the elements
   **
   **/
  void
    Players::reset()
    {
      DEBUG_CALLING(INFO_GTKMM_PLAYERS && INFO_GTKMM_UPDATE,
		    "Players::reset()");

      if (!this->is_visible())
	DEBUG_RETURNING(VOID,
			INFO_GTKMM_PLAYERS && INFO_GTKMM_UPDATE,
			"Players::reset()");

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

      DEBUG_RETURNING(VOID,
		      INFO_GTKMM_PLAYERS && INFO_GTKMM_UPDATE,
		      "Players::reset()");
    } // void Players::reset()

  /**
   **
   ** show the page of the given player
   **
   ** @param	playerno	player to show
   **
   ** @return	-
   **
   ** @version	0.6.4
   **
   ** @author	Diether Knof
   **
   **/
  void
    Players::switch_to_player(unsigned const playerno)
    {
      DEBUG_CALLING(INFO_GTKMM_PLAYERS && INFO_GTKMM_UPDATE,
		    "Players::switch_to_player(playerno)");

      this->set_current_page(playerno);

      DEBUG_RETURNING(VOID,
		      INFO_GTKMM_PLAYERS && INFO_GTKMM_UPDATE,
		      "Players::switch_to_player(playerno)");
    } // void Players::switch_to_player(unsigned const playerno)

  /**
   **
   ** signal: show the page of the given player
   **
   ** @param	page		new page
   ** @param	playerno	player to show
   **
   ** @return	-
   **
   ** @version	0.6.4
   **
   ** @author	Diether Knof
   **
   **/
  void
    Players::on_switch_page(GtkNotebookPage* page, guint playerno)
    {
      DEBUG_CALLING(INFO_GTKMM_PLAYERS && INFO_GTKMM_UPDATE,
		    "Players::on_switch_page(page, playerno)");

      this->Notebook::on_switch_page(page, playerno);

      this->aiconfigs->switch_to_player(playerno);

      DEBUG_RETURNING(VOID,
		      INFO_GTKMM_PLAYERS && INFO_GTKMM_UPDATE,
		      "Players::on_switch_page(page, playerno)");
    } // void Players::on_switch_page(GtkNotebookPage* page, guint playerno)

  /**
   **
   ** update the sensitivity of all widgets
   **
   ** @param	-
   **
   ** @return	-
   **
   ** @version	0.5.4
   **
   ** @author	Diether Knof
   **
   **/
  void
    Players::sensitivity_update()
    {
      DEBUG_CALLING(INFO_GTKMM_PLAYERS && INFO_GTKMM_UPDATE,
		    "Players::sensitivity_update()");

      if (!this->is_visible())
	DEBUG_RETURNING(VOID,
			INFO_GTKMM_PLAYERS && INFO_GTKMM_UPDATE,
			"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_GTKMM_PLAYERS && INFO_GTKMM_UPDATE,
		      "Players::sensitivity_update()");
    } // void Players::sensitivity_update()

  /**
   **
   ** update all widgets
   **
   ** @param	-
   **
   ** @return	-
   **
   ** @version	0.5.4
   **
   ** @author	Diether Knof
   **
   **/
  void
    Players::update()
    {
      DEBUG_CALLING(INFO_GTKMM_PLAYERS && INFO_GTKMM_UPDATE,
		    "Players::update()");

      if (!this->is_visible())
	DEBUG_RETURNING(VOID,
			INFO_GTKMM_PLAYERS && INFO_GTKMM_UPDATE,
			"Players::update()");

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

      DEBUG_RETURNING(VOID,
		      INFO_GTKMM_PLAYERS && INFO_GTKMM_UPDATE,
		      "Players::update()");
    } // void Players::update()

  /**
   **
   ** update 'player'
   **
   ** @param	player	player to update
   **
   ** @return	-
   **
   ** @version	0.5.4
   **
   ** @author	Diether Knof
   **
   **/
  void
    Players::player_update(::Player const& player)
    {
      DEBUG_CALLING(INFO_GTKMM_PLAYERS && INFO_GTKMM_UPDATE,
		    "Players::player_update(player)");

      if (!this->is_visible())
	DEBUG_RETURNING(VOID,
			INFO_GTKMM_PLAYERS && INFO_GTKMM_UPDATE,
			"Players::player_update(player)");

      this->player(player.no()).update();
      this->aiconfigs->aiconfig_update(player);

      DEBUG_RETURNING(VOID,
		      INFO_GTKMM_PLAYERS && INFO_GTKMM_UPDATE,
		      "Players::player_update(player)");
    } // void Players::player_update(::Player const& player)

  /**
   **
   ** update the name of 'player'
   **
   ** @param	player	player with the new name
   **
   ** @return	-
   **
   ** @version	0.5.4
   **
   ** @author	Diether Knof
   **
   **/
  void
    Players::name_update(::Player const& player)
    {
      DEBUG_CALLING(INFO_GTKMM_PLAYERS && INFO_GTKMM_UPDATE,
		    "Players::name_update(player)");

      if (!this->is_visible())
	DEBUG_RETURNING(VOID,
			INFO_GTKMM_PLAYERS && INFO_GTKMM_UPDATE,
			"Players::name_update(player)");

      this->player(player.no()).name_update();
      this->aiconfigs->name_update(player);

      DEBUG_RETURNING(VOID,
		      INFO_GTKMM_PLAYERS && INFO_GTKMM_UPDATE,
		      "Players::name_update(player)");
    } // void Players::name_update(::Player const& player)

} // namespace UI_GTKMM_NS

#endif // #ifdef USE_UI_GTKMM
