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

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),
  Alignment(Gtk::ALIGN_CENTER, Gtk::ALIGN_CENTER, 0, 0),
  players(players),
  playerno_(playerno),
  player_bak(this->player().clone()),
  name_bookmark(NULL),
  name_label(NULL),
  name_entry(NULL),
  random_name_button(NULL),
  type_label(NULL),
  type_button(),
  configure_button(NULL)
  {
    DEBUG_CALLING(INFO_GTKMM_PLAYER && INFO_GTKMM_INIT,
		  "Players::Player::Player(parent)");

    this->init();

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

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

    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"));

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

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

      this->name_bookmark = Gtk::manage(new Gtk::Label(Glib::locale_to_utf8(this->player().name())));

      this->name_label = Gtk::manage(new Gtk::Label("name: "));
      this->ui->add_for_translation(*(this->name_label),
				    ::translator("%ttranslation%: ",
						 ::translator("name")));

      this->name_entry = Gtk::manage(new Gtk::Entry());

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

      this->type_label = Gtk::manage(new Gtk::Label("type: "));
      this->ui->add_for_translation(*(this->type_label),
				    ::translator("%ttranslation%: ",
						 ::translator("type")));

      this->configure_button
	= Gtk::manage(new Gtk::StockButton(Gtk::Stock::PREFERENCES,
					   "configure"));
      this->ui->add_for_translation(*(this->configure_button),
				    ::translator("configure"));





      { // 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->add_for_translation(**type,
					::translator(*static_cast<PlayerType*>((*type)->get_data("type"))));
      } // the player types

      Gtk::VBox* vbox = Gtk::manage(new Gtk::VBox(false, 20));
      this->add(*vbox);

      { // the name widgets
	Gtk::Alignment* alignment
	  = Gtk::manage(new Gtk::Alignment(Gtk::ALIGN_CENTER, Gtk::ALIGN_CENTER,
					   0, 0)); 
	vbox->add(*alignment);

	Gtk::HBox* hbox = Gtk::manage(new Gtk::HBox(false, 10));
	alignment->add(*hbox);

	hbox->add(*(this->name_label));
	hbox->add(*(this->name_entry));
	hbox->add(*(this->random_name_button));
      } // the name widgets
      { // the type widgets
	Gtk::Alignment* alignment
	  = Gtk::manage(new Gtk::Alignment(Gtk::ALIGN_CENTER, Gtk::ALIGN_CENTER,
					   0, 0)); 
	vbox->add(*alignment);

	Gtk::HBox* hbox = Gtk::manage(new Gtk::HBox(false, 10));
	alignment->add(*hbox);

	hbox->add(*(this->type_label));

	Gtk::HBox* hbox2 = Gtk::manage(new Gtk::HBox(true, 5));
	hbox->add(*hbox2);

	for (vector<Gtk::RadioButton*>::iterator type = this->type_button.begin();
	     type != this->type_button.end();
	     type++)
	  hbox2->add(**type);
      } // the type widgets
      { // the configure button
	Gtk::Alignment* alignment
	  = Gtk::manage(new Gtk::Alignment(Gtk::ALIGN_CENTER, Gtk::ALIGN_CENTER,
					   0, 0)); 
	vbox->add(*alignment);

	alignment->add(*(this->configure_button));
      } // the configure button

      // 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));

      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"))));

#if 0
      // no need, because the page is always changed (-> players.cpp)
      this->configure_button->signal_clicked().connect(SigC::bind<int>(SigC::slot(*(this->players->aiconfigs->notebook), &Gtk::Notebook::set_current_page),
								       this->playerno())
						      );
#endif

      this->configure_button->signal_clicked().connect(SigC::slot(*(this->players->aiconfigs), &Gtk::Window::present));
      this->configure_button->signal_clicked().connect(SigC::bind<unsigned const>(SigC::slot(*(this->players->aiconfigs), &Players::AiConfigs::switch_to_player), this->playerno()));

      this->show_all();

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

  /**
   **
   ** -> result
   **
   ** @param	-
   **
   ** @return	the player
   **
   ** @version	0.5.4
   **
   ** @author	Diether Knof
   **
   **/
  ::Player const&
    Players::Player::player() const
    {
      DEBUG_CALLING(INFO_GTKMM_PLAYER && INFO_GTKMM_VALUE,
		    "Players::Player::player()");

      DEBUG_RETURNING(::party.player(this->playerno()),
		      INFO_GTKMM_PLAYER && INFO_GTKMM_VALUE,
		      "Players::Player::player()");
    } // ::Player const& Players::Player::player() const

  /**
   **
   ** -> result
   **
   ** @param	-
   **
   ** @return	the playernumber
   **
   ** @version	0.5.4
   **
   ** @author	Diether Knof
   **
   **/
  unsigned
    Players::Player::playerno() const
    {
      DEBUG_CALLING(INFO_GTKMM_PLAYER && INFO_GTKMM_VALUE,
		    "Players::Player::playerno()");

      DEBUG_RETURNING(this->playerno_,
		      INFO_GTKMM_PLAYER && INFO_GTKMM_VALUE,
		      "Players::Player::playerno()");
    } // unsigned Players::Player::playerno() const

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

      delete this->player_bak;
      this->player_bak = this->player().clone();

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

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

      ::party.player_set(this->player_bak->clone(), this->playerno());
      this->update();

      DEBUG_RETURNING(VOID,
		      INFO_GTKMM_PLAYER && INFO_GTKMM_UPDATE,
		      "Players::Player::reset()");
    } // 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*)
    {
      DEBUG_CALLING(INFO_GTKMM_PLAYER && INFO_GTKMM_EVENT,
		    "Players::Player::name_changed_event()");

      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()));

      DEBUG_RETURNING(true,
		      INFO_GTKMM_PLAYER && INFO_GTKMM_EVENT,
		      "Players::Player::name_changed_event()");
    } // 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()
    {
      DEBUG_CALLING(INFO_GTKMM_PLAYER && INFO_GTKMM_EVENT,
		    "Players::Player::random_name()");

      ::party.playername_set(this->playerno(),
			     ::generate_name(4 + RAND(3)));

      DEBUG_RETURNING(VOID,
		      INFO_GTKMM_PLAYER && INFO_GTKMM_EVENT,
		      "Players::Player::random_name()");
    } // 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)
    {
      DEBUG_CALLING(INFO_GTKMM_PLAYER && INFO_GTKMM_EVENT,
		    "Players::Player::type_change(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;
	}

      DEBUG_RETURNING(VOID,
		      INFO_GTKMM_PLAYER && INFO_GTKMM_EVENT,
		      "Players::Player::type_change(type)");
    } // 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()
    {
      DEBUG_CALLING(INFO_GTKMM_PLAYER && INFO_GTKMM_UPDATE,
		    "Players::Player::sensitivity_update()");

      if (::game_status == GAMESTATUS::PARTY_INIT) {
	this->type_label->set_sensitive(true);
	for (vector<Gtk::RadioButton*>::iterator t = this->type_button.begin();
	     t != this->type_button.end();
	     t++)
	  (*t)->set_sensitive(true);
      } else { // if !(::game_status == GAMESTATUS::PARTY_INIT)
	this->type_label->set_sensitive(false);
	for (vector<Gtk::RadioButton*>::iterator t = this->type_button.begin();
	     t != this->type_button.end();
	     t++)
	  (*t)->set_sensitive(false);
      } // if (::game_status == GAMESTATUS::PARTY_INIT) 

      DEBUG_RETURNING(VOID,
		      INFO_GTKMM_PLAYER && INFO_GTKMM_UPDATE,
		      "Players::Player::sensitivity_update()");
    } // void Players::Player::sensitivity_update()

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

      if (!this->is_visible())
	DEBUG_RETURNING(VOID,
			INFO_GTKMM_PLAYER && INFO_GTKMM_UPDATE,
			"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;
	}

      DEBUG_RETURNING(VOID,
		      INFO_GTKMM_PLAYER && INFO_GTKMM_UPDATE,
		      "Players::Player::update()");
    } // 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()
    {
      DEBUG_CALLING(INFO_GTKMM_PLAYER && INFO_GTKMM_UPDATE,
		    "Players::Player::name_update()");

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

      this->name_bookmark->set_text(Glib::locale_to_utf8(this->player().name()));
      // 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()));
	in_change = false;
      } // if (!in_change)

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

} // namespace UI_GTKMM_NS

#endif // #ifdef USE_UI_GTKMM
