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

#include "player.h"
#include "aiconfig.h"

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

namespace GTK_DOKO {

  /**
   **
   ** Constructor
   **
   ** @param	players	the corresponding players
   **
   ** @return	-
   ** 
   ** @author	Diether Knof
   **
   ** @version	0.5.3
   **
   **/
  PartySettings::Players::AiConfigs::AiConfigs(PartySettings::Players* const players) :
    Dialog(&(players->gui()), "aiconfigs"),
  players_(players),
  aiconfig_(),
  notebook(gtk_notebook_new()),
  player_label_()
  {
    DEBUG_CALLING(INFO_GTK_AICONFIG && INFO_GTK_INIT,
		  "PartySettings::Players::AiConfigs::AiConfigs()");

    gtk_window_set_transient_for(GTK_WINDOW(this->window()),
				 GTK_WINDOW(this->players().party_settings().window()));

    for (unsigned p = 0; p < ::party.playerno(); p++) {
      this->aiconfig_.push_back(new AiConfig(this,
					     &(this->players().player(p))));
      this->player_label_.push_back(gtk_label_new(::party.player(p).name().c_str()));
      gtk_notebook_append_page(GTK_NOTEBOOK(this->notebook),
			       this->aiconfig_.back()->widget(),
			       this->player_label_.back());
    } // for (aiconfig \in this->aiconfig_)

    gtk_container_add(GTK_CONTAINER(GTK_DIALOG(this->window())->vbox),
		      this->notebook);
    gtk_container_add(GTK_CONTAINER(GTK_DIALOG(this->window())->action_area),
		      this->button_close);

    gtk_widget_show_all(GTK_DIALOG(this->window())->vbox);
    gtk_widget_show_all(GTK_DIALOG(this->window())->action_area);

    DEBUG_RETURNING(VOID,
		    INFO_GTK_AICONFIG && INFO_GTK_INIT,
		    "PartySettings::Players::AiConfigs::AiConfigs()");
  } // PartySettings::Players::AiConfigs::AiConfigs(PartySettings::Players::Player* player)

  /**
   **
   ** Destruktor:
   ** nothing to do
   **
   ** @param	-
   **
   ** @return	-
   ** 
   ** @author	Diether Knof
   **
   ** @version	0.5.3
   **
   **/
  PartySettings::Players::AiConfigs::~AiConfigs()
  {
    DEBUG_CALLING(INFO_GTK_AICONFIG && INFO_GTK_INIT,
		  "PartySettings::Players::AiConfigs::~AiConfigs()");

    for (vector<AiConfig*>::iterator
	 aiconfig = this->aiconfig_.begin();
	 aiconfig != this->aiconfig_.end();
	 aiconfig++)
      delete *aiconfig;

    gtk_widget_destroy(this->notebook);
    this->notebook = NULL;

    this->player_label_.clear();

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


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

      DEBUG_RETURNING(*(this->players_),
		      INFO_GTK_AICONFIG && INFO_GTK_VALUE,
		      "PartySettings::Players::AiConfigs::players()");
    } // PartySettings::Players& PartySettings::Players::AiConfigs::players()

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

      DEBUG_ASSERTION((this->aiconfig_.size() > playerno),
		      "PartySettings::Players::AiConfigs::aiconfig(playerno):\n"
		      "  'playerno' = " << playerno
		      << " > " << this->aiconfig_.size()
		      << " = this->aiconfig_.size()");

      DEBUG_RETURNING(*(this->aiconfig_[playerno]),
		      INFO_GTK_AICONFIG && INFO_GTK_VALUE,
		      "PartySettings::Players::AiConfigs::aiconfig(playerno)");
    } // AiConfig& PartySettings::Players::AiConfigs::aiconfig(unsigned const playerno)

  /**
   **
   ** show the window
   **
   ** @param	-
   **
   ** @return	-
   ** 
   ** @author	Diether Knof
   **
   ** @version	0.5.3
   **
   **/
  void
    PartySettings::Players::AiConfigs::show()
    {
      DEBUG_CALLING(INFO_GTK_AICONFIG && INFO_GTK_INIT,
		    "PartySettings::Players::AiConfigs::show()");

      this->Dialog::show();

      DEBUG_RETURNING(VOID,
		      INFO_GTK_AICONFIG && INFO_GTK_INIT,
		      "PartySettings::Players::AiConfigs::show()");
    } // PartySettings::Players::AiConfigs::show()

  /**
   **
   ** show the window with the configuration
   ** of player 'playerno'
   **
   ** @param	playerno	number of the player, whose configuration
   **				is to be shown
   **
   ** @return	-
   ** 
   ** @author	Diether Knof
   **
   ** @version	0.5.3
   **
   **/
  void
    PartySettings::Players::AiConfigs::show(unsigned const playerno)
    {
      DEBUG_CALLING(INFO_GTK_AICONFIG && INFO_GTK_INIT,
		    "PartySettings::Players::AiConfigs::show(playerno)");

      gtk_notebook_set_page(GTK_NOTEBOOK(this->notebook), playerno);
      this->show();

      DEBUG_RETURNING(VOID,
		      INFO_GTK_AICONFIG && INFO_GTK_INIT,
		      "PartySettings::Players::AiConfigs::show(playerno)");
    } // PartySettings::Players::AiConfigs::show(unsigned const playerno)

  /**
   **
   ** switch to the configuration of player 'playerno'
   **
   ** @param	playerno	number of the player, whose configuration
   **				is to be shown
   **
   ** @return	-
   ** 
   ** @author	Diether Knof
   **
   ** @version	0.5.3
   **
   **/
  void
    PartySettings::Players::AiConfigs::switch_player(unsigned const playerno)
    {
      DEBUG_CALLING(INFO_GTK_AICONFIG && INFO_GTK_INIT,
		    "PartySettings::Players::AiConfigs::switch_player(playerno)");

      gtk_notebook_set_page(GTK_NOTEBOOK(this->notebook), playerno);

      DEBUG_RETURNING(VOID,
		      INFO_GTK_AICONFIG && INFO_GTK_INIT,
		      "PartySettings::Players::AiConfigs::switch_player(playerno)");
    } // PartySettings::Players::AiConfigs::switch_player(unsigned const playerno)

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

      if (this->notebook == NULL)
	DEBUG_RETURNING(VOID,
			INFO_GTK_AICONFIG && INFO_GTK_UPDATE,
			"PartySettings::Players::AiConfigs::sensitivity_update()");

      for (vector<AiConfig*>::iterator
	   aiconfig = this->aiconfig_.begin();
	   aiconfig != this->aiconfig_.end();
	   aiconfig++)
	(*aiconfig)->sensitivity_update();

      DEBUG_RETURNING(VOID,
		      INFO_GTK_AICONFIG && INFO_GTK_UPDATE,
		      "PartySettings::Players::AiConfigs::sensitivity_update()");
    } // void PartySettings::Players::sensitivity_update()

  /**
   **
   ** update the aiconfig
   **
   ** @param	-
   **
   ** @return	-
   ** 
   ** @author	Diether Knof
   **
   ** @version	0.5.3
   **
   **/
  void
    PartySettings::Players::AiConfigs::update()
    {
      DEBUG_CALLING(INFO_GTK_AICONFIG && INFO_GTK_UPDATE,
		    "PartySettings::Players::AiConfigs::update()");

      if (this->notebook == NULL)
	DEBUG_RETURNING(VOID,
			INFO_GTK_AICONFIG && INFO_GTK_UPDATE,
			"PartySettings::Players::AiConfigs::update()");

      for (vector<AiConfig*>::iterator
	   aiconfig = this->aiconfig_.begin();
	   aiconfig != this->aiconfig_.end();
	   aiconfig++)
	(*aiconfig)->update();

      DEBUG_RETURNING(VOID,
		      INFO_GTK_AICONFIG && INFO_GTK_UPDATE,
		      "PartySettings::Players::AiConfigs::update()");
    } // PartySettings::Players::AiConfigs::update()

  /**
   **
   ** update the name of the player
   **
   ** @param	player	player, whose name is to be updated
   **
   ** @return	-
   ** 
   ** @author	Diether Knof
   **
   ** @version	0.5.3
   **
   **/
  void
    PartySettings::Players::AiConfigs::name_update(::Player const& player)
    {
      DEBUG_CALLING(INFO_GTK_AICONFIG && INFO_GTK_UPDATE,
		    "PartySettings::Players::AiConfigs::name_update(player)");

      if (this->notebook == NULL)
	DEBUG_RETURNING(VOID,
			INFO_GTK_AICONFIG && INFO_GTK_UPDATE,
			"PartySettings::Players::AiConfigs::name_update(player)");

      gtk_label_set_text(GTK_LABEL(this->player_label_[player.no()]),
			 player.name().c_str());
      this->aiconfig(player.no()).language_update();

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

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

      if (this->notebook == NULL)
	DEBUG_RETURNING(VOID,
			INFO_GTK_AICONFIG && INFO_GTK_UPDATE,
			"PartySettings::Players::AiConfigs::player_update(player)");

      this->name_update(player);
      this->aiconfig(player.no()).update();

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

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

      this->Dialog::language_update();
      
      if (this->notebook == NULL)
	DEBUG_RETURNING(VOID,
			INFO_GTK_AICONFIG && INFO_GTK_UPDATE,
			"PartySettings::Players::AiConfigs::language_update()");

      for (unsigned p = 0; p < this->player_label_.size(); p++)
	gtk_label_set_text(GTK_LABEL(this->player_label_[p]),
			   this->players().player(p).player().name().c_str());

      for (vector<AiConfig*>::iterator
	   aiconfig = this->aiconfig_.begin();
	   aiconfig != this->aiconfig_.end();
	   aiconfig++)
	(*aiconfig)->language_update();

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


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