/**********************************************************************
 *
 *   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 "aiconfigs.h"
#include "aiconfig.h"
#include "ui.h"
#include "main_window.h"

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

#include "widgets/stock_button.h"
#include <gtkmm/stock.h>

namespace UI_GTKMM_NS {

  /**
   **
   ** Constructor
   **
   ** @param	-
   **
   ** @return	-
   ** 
   ** @author	Diether Knof
   **
   ** @version	0.5.4
   **
   **/
  Players::AiConfigs::AiConfigs(Players* const players) :
    Base(players),
  Dialog("Aiconfigs", false, true),
  players(players),
  notebook(NULL),
  close_button(NULL),
  aiconfig_()
  {
    DEBUG_CALLING(INFO_GTKMM_AICONFIGS && INFO_GTKMM_INIT,
		  "Players::AiConfigs::AiConfigs(players)");

    this->signal_realize().connect(SigC::slot(*this, &Players::AiConfigs::init));

    this->signal_show().connect(SigC::slot(*this, &Players::AiConfigs::update));

    DEBUG_RETURNING(VOID,
		    INFO_GTKMM_AICONFIGS && INFO_GTKMM_INIT,
		    "Players::AiConfigs::AiConfigs(players)");
  } // Players::AiConfigs::AiConfigs(AiConfigs* const players)

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

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

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

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

      this->ui->add_for_translation(*this, ::translator("Aiconfigs"));

      this->set_icon(this->ui->main_window->icon);

      this->notebook = Gtk::manage(new Gtk::Notebook());
      this->close_button
	= Gtk::manage(new Gtk::StockButton(Gtk::Stock::CLOSE, "close"));
      this->ui->add_for_translation(*(this->close_button),
				    ::translator("close"));
      this->add_action_widget(*(this->close_button), Gtk::RESPONSE_CLOSE);
      this->close_button->grab_default();

      this->get_vbox()->add(*(this->notebook));
      for (unsigned p = 0; p < ::party.playerno(); p++) {
	this->aiconfig_.push_back(new AiConfig(this, p));
	this->notebook->append_page(*(this->aiconfig_.back()->container),
				    *(this->aiconfig_.back()->name_bookmark));
      } // for (p < ::party.playerno())


      // signals
      this->close_button->signal_clicked().connect(SigC::slot(*this,
							      &Gtk::Widget::hide)
						  );
      this->notebook->signal_switch_page().connect(SigC::slot(*this, &AiConfigs::on_switch_page));


      this->show_all_children();

      this->language_update();

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

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

      DEBUG_RETURNING(this->players->player(playerno),
		      INFO_GTKMM_AICONFIGS && INFO_GTKMM_VALUE,
		      "AiConfigs::player(playerno)");
    } // Players::Player& Players::AiConfigs::player(unsigned const playerno)

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

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

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

  /**
   **
   ** switch to player 'playerno'
   **
   ** @param	playerno	player to switch to
   **
   ** @return	-
   **
   ** @version	0.6.4
   **
   ** @author	Diether Knof
   **
   **/
  void
    Players::AiConfigs::switch_to_player(unsigned const playerno)
    {
      DEBUG_CALLING(INFO_GTKMM_AICONFIGS && INFO_GTKMM_UPDATE,
		    "Players::AiConfigs::switch_to_player(playerno)");

      if (this->notebook)
	this->notebook->set_current_page(playerno);

      DEBUG_RETURNING(VOID,
		      INFO_GTKMM_AICONFIGS && INFO_GTKMM_UPDATE,
		      "Players::AiConfigs::switch_to_player(playerno)");
    } // void Players::AiConfigs::switch_to_player(int 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::AiConfigs::on_switch_page(GtkNotebookPage* page, guint playerno)
    {
      DEBUG_CALLING(INFO_GTKMM_PLAYERS && INFO_GTKMM_UPDATE,
		    "Players::AiConfigs::on_switch_page(page, playerno)");

      this->players->switch_to_player(playerno);

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

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

      if (!this->is_realized())
	DEBUG_RETURNING(VOID,
			INFO_GTKMM_AICONFIGS && INFO_GTKMM_UPDATE,
			"Players::AiConfigs::sensitivity_update()");

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

      DEBUG_RETURNING(VOID,
		      INFO_GTKMM_AICONFIGS && INFO_GTKMM_UPDATE,
		      "Players::AiConfigs::sensitivity_update()");
    } // void Players::AiConfigs::sensitivity_update()

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

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

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

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

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

      if (!this->is_visible())
	DEBUG_RETURNING(VOID,
			INFO_GTKMM_AICONFIGS && INFO_GTKMM_UPDATE,
			"Players::AiConfigs::aiconfig_update(player)");

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

      DEBUG_RETURNING(VOID,
		      INFO_GTKMM_AICONFIGS && INFO_GTKMM_UPDATE,
		      "Players::AiConfigs::aiconfig_update(player)");
    } // void Players::AiConfigs::aiconfig_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::AiConfigs::name_update(::Player const& player)
    {
      DEBUG_CALLING(INFO_GTKMM_AICONFIGS && INFO_GTKMM_UPDATE,
		    "Players::AiConfigs::name_update(player)");

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

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

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

} // namespace UI_GTKMM_NS

#endif // #ifdef USE_UI_GTKMM
