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

#include "../../player/player.h"

#include "widgets/stock_button.h"
#include <gtkmm/box.h>
#include <gtkmm/label.h>
#include <gtkmm/alignment.h>
#include <gtkmm/table.h>
#include <gtkmm/main.h>

namespace UI_GTKMM_NS {

  // position of the window
  int GenscherSelection::position_x = INT_MAX;
  int GenscherSelection::position_y = INT_MAX;

  /**
   **
   ** Constructor
   **
   ** @param	table		the table
   ** @param	genscher	the player who has the genscher
   **
   **
   ** @return	-
   ** 
   ** @author	Diether Knof
   **
   ** @version	0.6.1
   **
   **/
  GenscherSelection::GenscherSelection(Table* const table,
			     Player const& genscher) :
    GameinfoDialog(table),
  info_label(NULL),
  partner_button(),
  no_announcement_button(NULL),
  genscher(&genscher),
  partner(&genscher)
  {
    DEBUG_CALLING(INFO_GTKMM_GENSCHER_SELECTION && INFO_GTKMM_INIT,
		  "GenscherSelection::GenscherSelection(table, genscher)");

    DEBUG_RETURNING(VOID,
		    INFO_GTKMM_GENSCHER_SELECTION && INFO_GTKMM_INIT,
		    "GenscherSelection::GenscherSelection(table, genscher)");
  } // GenscherSelection::GenscherSelection(Table* const table, Player const& genscher)

  /**
   **
   ** Destruktor
   **
   ** @param	-
   **
   ** @return	-
   ** 
   ** @author	Diether Knof
   **
   ** @version	0.6.1
   **
   **/
  GenscherSelection::~GenscherSelection()
  {
    DEBUG_CALLING(INFO_GTKMM_GENSCHER_SELECTION && INFO_GTKMM_INIT,
		  "GenscherSelection::~GenscherSelection()");

    DEBUG_RETURNING(VOID,
		    INFO_GTKMM_GENSCHER_SELECTION && INFO_GTKMM_INIT,
		    "GenscherSelection::~GenscherSelection()");
  } // GenscherSelection::~GenscherSelection()

  /**
   **
   ** init the dialog
   **
   ** @param	-
   **
   ** @return	-
   ** 
   ** @author	Diether Knof
   **
   ** @version	0.6.1
   **
   **/
  void
    GenscherSelection::init()
    {
      DEBUG_CALLING(INFO_GTKMM_GENSCHER_SELECTION && INFO_GTKMM_INIT,
		    "GenscherSelection::init()");

      this->GameinfoDialog::init();

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

      Gtk::Alignment* alignment = Gtk::manage(new Gtk::Alignment());
      alignment->set_border_width(20);

      this->info_label = Gtk::manage(new Gtk::Label("Genscher: Please choose your partner."));
      this->ui->add_for_translation(*(this->info_label),
				    ::translator("Genscher: Please choose your partner."));
      alignment->add(*(this->info_label));
      this->get_vbox()->add(*alignment);

      Gtk::Table* table = Gtk::manage(new Gtk::Table(3, 3, true));
      table->set_spacings(10);
      table->set_border_width(10);

      for (unsigned p = 0; p < this->genscher->game().playerno(); p++) {
	Player const& player = this->genscher->game().player(p);
	this->partner_button.push_back(Gtk::manage(new Gtk::Button(player.name())));
	this->partner_button.back()->signal_clicked().connect(SigC::bind<Player const* const>(SigC::slot(*this, &GenscherSelection::select_partner_event), &player));
	if (true || player != *(this->genscher)) {
	  switch(static_cast<Table*>(this->parent)->position(player)) {
	  case NORTH:
	    table->attach(*(this->partner_button.back()),
			  1, 2, 0, 1);
	    break;
	  case SOUTH:
	    table->attach(*(this->partner_button.back()),
			  1, 2, 2, 3);
	    break;
	  case WEST:
	    table->attach(*(this->partner_button.back()),
			  0, 1, 1, 2);
	    break;
	  case EAST:
	    table->attach(*(this->partner_button.back()),
			  2, 3, 1, 2);
	    break;
	  } // switch(this->table->position(player))
	} // if (player != *(this->genscher))
      } // for (p < this->genscher->game().playerno())
      this->get_vbox()->add(*table);

      this->no_announcement_button = Gtk::manage(new Gtk::Button("no announcement"));
      this->ui->add_for_translation(*(this->no_announcement_button),
				    ::translator("no announcement"));
      this->no_announcement_button->signal_clicked().connect(SigC::bind<Player const* const>(SigC::slot(*this, &GenscherSelection::select_partner_event), NULL));
      this->get_action_area()->add(*(this->no_announcement_button));

      this->show_all_children();
      this->close_button->hide();

      DEBUG_RETURNING(VOID,
		      INFO_GTKMM_GENSCHER_SELECTION && INFO_GTKMM_INIT,
		      "GenscherSelection::init()");
    } // void GenscherSelection::init()

  /**
   **
   ** show the window
   **
   ** @param	-
   **
   ** @return	-
   ** 
   ** @author	Diether Knof
   **
   ** @version	0.6.1
   **
   **/
  Player const*
    GenscherSelection::select_partner()
    {
      DEBUG_CALLING(INFO_GTKMM_GENSCHER_SELECTION && INFO_GTKMM_GAMEPLAY,
		    "GenscherSelection::select_partner()");

      this->present();

      ::game_status = GAMESTATUS::GAME_GENSCHER;

      while ((this->genscher == this->partner)
	     && (::game_status == GAMESTATUS::GAME_GENSCHER))
	Gtk::Main::iteration();

      this->hide();

      DEBUG_RETURNING(this->partner,
		      INFO_GTKMM_GENSCHER_SELECTION && INFO_GTKMM_GAMEPLAY,
		      "GenscherSelection::select_partner()");
    } // Player const* GenscherSelection::select_partner()

  /**
   **
   ** a partner has been selected
   **
   ** @param	partner	the player who has been selected
   **
   ** @return	-
   ** 
   ** @author	Diether Knof
   **
   ** @version	0.6.1
   **
   **/
  void
    GenscherSelection::select_partner_event(Player const* const player)
    {
      DEBUG_CALLING(INFO_GTKMM_GENSCHER_SELECTION && INFO_GTKMM_UPDATE,
		    "GenscherSelection::select_partner_event(player)");

      this->partner = player;

      DEBUG_RETURNING(VOID,
		      INFO_GTKMM_GENSCHER_SELECTION && INFO_GTKMM_UPDATE,
		      "GenscherSelection::select_partner_event(player)");
    } // void GenscherSelection::select_partner_event(Player const* const player)

  /**
   **
   ** the name of 'player' has changed
   **
   ** @param	player	the player whose name has changed
   **
   ** @return	-
   ** 
   ** @author	Diether Knof
   **
   ** @version	0.6.1
   **
   **/
  void
    GenscherSelection::name_changed(Player const& player)
    {
      DEBUG_CALLING(INFO_GTKMM_GENSCHER_SELECTION && INFO_GTKMM_UPDATE,
		    "GenscherSelection::name_changed(player)");

      if (!this->is_realized())
	DEBUG_RETURNING(VOID,
			INFO_GTKMM_GENSCHER_SELECTION && INFO_GTKMM_UPDATE,
			"GenscherSelection::name_changed(player)");

      dynamic_cast<Gtk::Label*>(this->partner_button[player.no()]->get_child()
			       )->set_text(player.name());

      DEBUG_RETURNING(VOID,
		      INFO_GTKMM_GENSCHER_SELECTION && INFO_GTKMM_UPDATE,
		      "GenscherSelection::name_changed(player)");
    } // void GenscherSelection::name_changed(Player const& player)

  /**
   **
   ** save the position of the window
   **
   ** @param	-
   **
   ** @return	-
   **
   ** @version	0.6.4
   **
   ** @author	Diether Knof
   **
   **/
  void
    GenscherSelection::save_position()
    {
      // Note: 'Gtk::Window::get_position()' does not give the actual position
      this->get_window()->get_root_origin(this->position_x, this->position_y);

      return ;
    } // void GenscherSelection::save_position()

  /**
   **
   ** move the window to the last position
   **
   ** @param	-
   **
   ** @return	-
   **
   ** @version	0.6.4
   **
   ** @author	Diether Knof
   **
   **/
  void
    GenscherSelection::move_to_last_position()
    {
      if (this->position_x != INT_MAX)
	this->move(this->position_x, this->position_y);

      return ;
    } // void GenscherSelection::move_to_last_position()

} // namespace UI_GTKMM_NS

#endif // #ifdef USE_UI_GTKMM
