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

#include "../../party/party.h"
#include "../../player/player.h"
#include "../../player/namegen.h"

#include "widgets/stock_button.h"
#include <gtkmm/stock.h>
#include <gtkmm/label.h>
#include <gtkmm/entry.h>
#include <gtkmm/radiobutton.h>
#include <gtkmm/box.h>
#include <gtkmm/alignment.h>

namespace UI_GTKMM_NS {

  /**
   **
   ** Constructor
   **
   ** @param	-
   **
   ** @return	-
   ** 
   ** @author	Diether Knof
   **
   ** @version	0.5.4
   **
   **/
  Players::Player::Player(Players* const players, unsigned const playerno) :
    Base(players),
  SigC::Object(),
  players(players),
  aiconfig_(NULL),
  playerno_(playerno),
  player_bak(this->player().clone()),
  name_label(NULL),
  name_entry(NULL),
  random_name_button(NULL),
  type_button(),
  reset_button(NULL)
  {
    this->init();

    return ;
  } // Players::Player::Player(Base* const parent)

  /**
   **
   ** Destruktor
   **
   ** @param	-
   **
   ** @return	-
   ** 
   ** @author	Diether Knof
   **
   ** @version	0.5.4
   **
   **/
  Players::Player::~Player()
  {
    delete this->aiconfig_;

    delete this->player_bak;

    for (vector<Gtk::RadioButton*>::iterator type = this->type_button.begin();
	 type != this->type_button.end();
	 type++)
      delete static_cast<PlayerType*>((*type)->steal_data("type"));

    return ;
  } // Players::Player::~Player()

  /**
   **
   ** init all subelements
   **
   ** @param	-
   **
   ** @return	-
   **
   ** @version	0.5.4
   **
   ** @author	Diether Knof
   **
   **/
  void
    Players::Player::init()
    {
      this->aiconfig_ = new AiConfig(this);

      this->name_label = Gtk::manage(new Gtk::Label("name"));
      this->name_entry = Gtk::manage(new Gtk::Entry());

      this->random_name_button = Gtk::manage(new Gtk::Button("random name"));
      this->ui->translations->add(*this->random_name_button,
				    ::translator("random name"));

      this->reset_button = Gtk::manage(new Gtk::StockButton(Gtk::Stock::UNDO,
						       "reset"));
      this->ui->translations->add(*this->reset_button,
				    ::translator("reset"));

      { // the player types
	Gtk::RadioButton::Group group;
	// the type is 'int', because there are compilation errors with
	// '::Player::Type'
	// Human
	this->type_button.push_back(Gtk::manage(new Gtk::RadioButton(group,
								     "human")));
	this->type_button.back()->set_data("type",
					   new PlayerType(::Player::HUMAN));
	// Ai
	this->type_button.push_back(Gtk::manage(new Gtk::RadioButton(group,
								     "ai")));
	this->type_button.back()->set_data("type",
					   new PlayerType(::Player::AI));
#ifndef RELEASE
	// AiDummy
	this->type_button.push_back(Gtk::manage(new Gtk::RadioButton(group,
								     "dummy ai")));
	this->type_button.back()->set_data("type",
					   new PlayerType(::Player::AI_DUMMY));
#endif

	for (vector<Gtk::RadioButton*>::iterator type = this->type_button.begin();
	     type != this->type_button.end();
	     type++)
	  this->ui->translations->add(**type,
					::translator(*static_cast<PlayerType*>((*type)->get_data("type"))));
      } // the player types

      // signals
      //this->name_entry->signal_changed().connect(SigC::slot(*this, &Player::name_change));
      this->name_entry->signal_focus_out_event().connect(SigC::slot(*this, &Player::name_changed_event));
      this->random_name_button->signal_clicked().connect(SigC::slot(*this, &Player::random_name));
      this->reset_button->signal_clicked().connect(SigC::slot(*this, &Player::reset));

      for (vector<Gtk::RadioButton*>::iterator type = this->type_button.begin();
	   type != this->type_button.end();
	   type++)
	(*type)->signal_toggled().connect(SigC::bind<PlayerType const>(SigC::slot(*this, &Player::type_change),
								       *static_cast<PlayerType*>((*type)->get_data("type"))));


      return ;
    } // void Players::Player::init()

  /**
   **
   ** -> result
   **
   ** @param	-
   **
   ** @return	the player
   **
   ** @version	0.5.4
   **
   ** @author	Diether Knof
   **
   **/
  ::Player const&
    Players::Player::player() const
    {
      return ::party.player(this->playerno());
    } // ::Player const& Players::Player::player() const

  /**
   **
   ** -> result
   **
   ** @param	-
   **
   ** @return	the player
   **
   ** @version	0.6.6
   **
   ** @author	Diether Knof
   **
   **/
  ::Player&
    Players::Player::player()
    {
      return ::party.player(this->playerno());
    } // ::Player& Players::Player::player()

  /**
   **
   ** -> result
   **
   ** @param	-
   **
   ** @return	the aiconfig
   **
   ** @version	0.6.6
   **
   ** @author	Diether Knof
   **
   **/
  Players::Player::AiConfig&
    Players::Player::aiconfig()
    {
      return *this->aiconfig_;
    } // Players::Player::AiConfig& Players::Player::aiconfig()

  /**
   **
   ** -> result
   **
   ** @param	-
   **
   ** @return	the playernumber
   **
   ** @version	0.5.4
   **
   ** @author	Diether Knof
   **
   **/
  unsigned
    Players::Player::playerno() const
    {
      return this->playerno_;
    } // unsigned Players::Player::playerno() const

  /**
   **
   ** creates a backup
   **
   ** @param	-
   **
   ** @return	-
   **
   ** @version	0.5.4
   **
   ** @author	Diether Knof
   **
   **/
  void
    Players::Player::create_backup()
    {
      delete this->player_bak;
      this->player_bak = this->player().clone();

      return ;
    } // void Players::Player::create_backup()

  /**
   **
   ** resets the settings
   **
   ** @param	-
   **
   ** @return	-
   **
   ** @version	0.5.4
   **
   ** @author	Diether Knof
   **
   **/
  void
    Players::Player::reset()
    {
      ::party.player_set(this->player_bak->clone(), this->playerno());
      this->update();

      return ;
    } // void Players::Player::reset()

  /**
   **
   ** the name has changed
   **
   ** @param	GdkEventFocus*	ignored
   **
   ** @return	-
   **
   ** @version	0.6.3
   **
   ** @author	Diether Knof
   **
   **/
  bool
    Players::Player::name_changed_event(GdkEventFocus*)
    {
      if (::party.player(this->playerno()).name()
	  != Glib::locale_from_utf8(this->name_entry->get_text()))
	::party.playername_set(this->playerno(),
			       Glib::locale_from_utf8(this->name_entry->get_text()));

      return true;
    } // bool Players::Player::name_changed_event(GdkEventFocus*)

  /**
   **
   ** create a random name
   **
   ** @param	-
   **
   ** @return	-
   **
   ** @version	0.5.4
   **
   ** @author	Diether Knof
   **
   **/
  void
    Players::Player::random_name()
    {
      ::party.playername_set(this->playerno(),
			     ::generate_name(4 + RAND(3)));

      return ;
    } // void Players::Player::random_name()

  /**
   **
   ** the type has changed
   **
   ** @param	type	new type
   **
   ** @return	-
   **
   ** @version	0.5.4
   **
   ** @author	Diether Knof
   **
   **/
  void
    Players::Player::type_change(PlayerType const type)
    {
      for (vector<Gtk::RadioButton*>::iterator typei
	   = this->type_button.begin();
	   typei != this->type_button.end();
	   typei++)
	if (*static_cast<PlayerType*>((*typei)->get_data("type"))
	    == type) {
	  if ((*typei)->get_active())
	    ::party.playertype_set(this->playerno(),
				   type);
	  break;
	}

      return ;
    } // void Players::Player::type_change(PlayerType const type)

  /**
   **
   ** update the sensitivity of all widgets
   **
   ** @param	-
   **
   ** @return	-
   **
   ** @version	0.6.0
   **
   ** @author	Diether Knof
   **
   **/
  void
    Players::Player::sensitivity_update()
    {
      if (::game_status == GAMESTATUS::PARTY_INIT) {
	for (vector<Gtk::RadioButton*>::iterator t = this->type_button.begin();
	     t != this->type_button.end();
	     t++)
	  (*t)->set_sensitive(true);
	this->reset_button->set_sensitive(true);
	this->aiconfig().sensitivity_update();
      } else { // if !(::game_status == GAMESTATUS::PARTY_INIT)
	for (vector<Gtk::RadioButton*>::iterator t = this->type_button.begin();
	     t != this->type_button.end();
	     t++)
	  (*t)->set_sensitive(false);
	this->reset_button->set_sensitive(false);
	this->aiconfig().sensitivity_update();
      } // if (::game_status == GAMESTATUS::PARTY_INIT) 

      return ;
    } // void Players::Player::sensitivity_update()

  /**
   **
   ** update all widgets
   **
   ** @param	-
   **
   ** @return	-
   **
   ** @version	0.5.4
   **
   ** @author	Diether Knof
   **
   **/
  void
    Players::Player::update()
    {
      this->sensitivity_update();
      this->name_update();
      for (vector<Gtk::RadioButton*>::iterator type = this->type_button.begin();
	   type != this->type_button.end();
	   type++)
	if (*static_cast<PlayerType*>((*type)->get_data("type"))
	    == this->player().type()) {
	  (*type)->set_active();
	  break;
	}

      this->aiconfig().update();

      return ;
    } // void Players::Player::update()

  /**
   **
   ** update the name
   **
   ** @param	-
   **
   ** @return	-
   **
   ** @version	0.5.4
   **
   ** @author	Diether Knof
   **
   ** @todo	block 'Entry::change_signal()'
   **
   **/
  void
    Players::Player::name_update()
    {
      // ToDo: block 'Entry::change_signal()'
      // Now, the entry is first cleared (that leads to an inserting of the 
      // default name), afterwards the new name is inserted.
      // So in the end the name is the old name followed by the default name.

      // workaround
      static bool in_change = false;
      if (!in_change) {
	in_change = true;
	this->name_entry->set_text(Glib::locale_to_utf8(this->player().name()));
	this->name_label->set_text(Glib::locale_to_utf8(this->player().name()));
	in_change = false;
      } // if (!in_change)
      this->aiconfig().name_update();

      return ;
    } // void Players::Player::name_update()

} // namespace UI_GTKMM_NS

#endif // #ifdef USE_UI_GTKMM
