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

#include "gtk_gui.h"

#include "../../party/party.h"
#include "../../game/game.h"
#include "../../player/player.h"
#include "../../misc/language.h"


namespace GTK_DOKO {
#include <gtk/gtkmain.h>
#include <gtk/gtktable.h>
#include <gtk/gtkframe.h>
#include <gtk/gtklabel.h>
#include <gtk/gtkbutton.h>
#include <gtk/gtksignal.h>
#include <gtk/gtkdialog.h>
#include <gtk/gtkvbox.h>
#include <gtk/gtkbox.h>

  /**
   **
   ** constructor
   **
   ** @param	playfield	the playfield
   **
   ** @return	-
   **
   ** @author	Diether Knof
   **
   ** @version	0.5.4
   **
   **/
  Genscher::Genscher(Playfield* const playfield) :
    PlayfieldDialog(playfield, "Genscher"),
  description(gtk_label_new("Select a new partner or keep the old.")),
  player_frame(gtk_frame_new("new partner")),
  player(),
  selected_player(NULL),
  choice_made(false)
  {
    DEBUG_CALLING(INFO_GTK_GENSCHER && INFO_GTK_INIT,
		  "Genscher::Genscher(playfield)");

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

    GtkWidget* vbox = gtk_vbox_new(FALSE, 10);
    gtk_container_add(GTK_CONTAINER(GTK_DIALOG(this->window())->vbox),
		      vbox);
    gtk_container_add(GTK_CONTAINER(vbox),
		      this->description);
    gtk_container_add(GTK_CONTAINER(vbox),
		      this->player_frame);

    gtk_container_set_border_width(GTK_CONTAINER(vbox), 20);
#if 0
    gtk_box_set_spacing(GTK_BOX(GTK_DIALOG(vbox), 20);
#endif

    gtk_object_set_data(GTK_OBJECT(this->button_close),
			"player", NULL);
    gtk_signal_connect(GTK_OBJECT(this->button_close), "clicked",
		       GTK_SIGNAL_FUNC(Genscher::select_player_event),
		       gpointer(this));
    DEBUG_RETURNING(VOID,
		    INFO_GTK_GENSCHER && INFO_GTK_INIT,
		    "Genscher::Genscher(playfield)");
  } // Genscher::Genscher(Playfield* const playfield)

  /**
   **
   ** destructor
   **
   ** @param	-
   **
   ** @return	-
   **
   ** @author	Diether Knof
   **
   ** @version	0.5.4
   **
   **/
  Genscher::~Genscher()
  {
    DEBUG_CALLING(INFO_GTK_GENSCHER && INFO_GTK_INIT,
		  "Genscher::~Genscher()");

    DEBUG_RETURNING(VOID,
		    INFO_GTK_GENSCHER && INFO_GTK_INIT,
		    "Genscher::~Genscher()");
  } // Genscher::~Genscher()

  /**
   **
   ** a partner is selected: -> result
   **
   ** @param	-
   **
   ** @return	Pointer to the new partner,
   **		NULL, if the old partner is kept
   **
   ** @author	Diether Knof
   **
   ** @version	0.5.4
   **
   **/
  Player const*
    Genscher::select_partner()
    {
      DEBUG_CALLING(INFO_GTK_GENSCHER && INFO_GTK_GAMEPLAY,
		    "Genscher::select_partner()");

      DEBUG_ASSERTION(::game_status == GAMESTATUS::GAME_GENSCHER,
		      "Genscher::select_partner()\n"
		      "  'game_status' != GAME_GENSCHER");

      DEBUG_ASSERTION((::party.game().playerno() == 4),
		      "Genscher::select_partner()\n"
		      "  the game must have four players");

      // reset some variables
      this->selected_player = NULL;
      this->choice_made = false;

      // create the buttons
      for (unsigned p = 0; p < ::party.game().playerno(); p++) {
	this->player.push_back(gtk_button_new_with_label(::party.game().player(p).name().c_str()));
	gtk_object_set_data(GTK_OBJECT(this->player.back()),
			    "player",
			    &(::party.game().player(p)));
	gtk_signal_connect(GTK_OBJECT(this->player.back()), "clicked",
			   GTK_SIGNAL_FUNC(Genscher::select_player_event),
			   gpointer(this));
      } // for (p < ::party.game().playerno())

      // create a table and put the buttons in it
      GtkWidget* table = gtk_table_new(3, 3, TRUE);
      gtk_table_set_row_spacings(GTK_TABLE(table), 10);
      gtk_table_set_col_spacings(GTK_TABLE(table), 10);
      gtk_container_set_border_width(GTK_CONTAINER(table), 10);

      gtk_table_attach_defaults(GTK_TABLE(table), this->player[0],
				1, 2, 2, 3);
      gtk_table_attach_defaults(GTK_TABLE(table), this->player[1],
				0, 1, 1, 2);
      gtk_table_attach_defaults(GTK_TABLE(table), this->player[2],
				1, 2, 0, 1);
      gtk_table_attach_defaults(GTK_TABLE(table), this->player[3],
				2, 3, 1, 2);

      gtk_container_add(GTK_CONTAINER(this->player_frame),
			table);

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

      gtk_widget_hide(this->player[::party.game().player_current().no()]);

      // show the window
      this->show();

      // wait until a choice is made
      while (!this->choice_made
	     && (::game_status == GAMESTATUS::GAME_GENSCHER))
	gtk_main();

      // close the window
      this->close();


      // destroy the buttons
      for (vector<GtkWidget*>::iterator player = this->player.begin();
	   player != this->player.end();
	   player++)
	gtk_widget_destroy(*player);
      this->player.clear();

      gtk_widget_destroy(table);

      DEBUG_RETURNING(this->selected_player,
		      INFO_GTK_GENSCHER && INFO_GTK_GAMEPLAY,
		      "Genscher::select_partner()");
    } // Player const* Genscher::select_partner()

  /**
   **
   ** event:  a player is selected
   **
   ** @param	button		pressed button
   ** @param	genscher	pointer to the object
   **
   ** @return	-
   **
   ** @author	Diether Knof
   **
   ** @version	0.5.4
   **
   **/
  void
    Genscher::select_player_event(GtkWidget* button, gpointer genscher)
    {
      DEBUG_CALLING(INFO_GTK_GENSCHER && INFO_GTK_EVENT,
		    "Genscher::select_player_event(button, genscher)");

      static_cast<Genscher*>(genscher)->select_player(static_cast<Player const*>(const_cast<void const*>(gtk_object_get_data(GTK_OBJECT(button), "player"))));

      DEBUG_RETURNING(VOID,
		      INFO_GTK_GENSCHER && INFO_GTK_EVENT,
		      "Genscher::select_player_event(button, genscher)");
    } // static void Genscher::select_player_event(GtkWidget* button, gpointer genscher)

  /**
   **
   ** event:  a player is selected
   **
   ** @param	player	selected player
   **
   ** @return	-
   **
   ** @author	Diether Knof
   **
   ** @version	0.5.4
   **
   **/
  void
    Genscher::select_player(Player const* const player)
    {
      DEBUG_CALLING(INFO_GTK_GENSCHER && INFO_GTK_EVENT,
		    "Genscher::select_player_event(player)");

      this->selected_player = player;
      this->choice_made = true;
      this->gui().main_quit();

      DEBUG_RETURNING(VOID,
		      INFO_GTK_GENSCHER && INFO_GTK_EVENT,
		      "Genscher::select_player(player)");
    } // void Genscher::select_player(Player const* const player)

  /**
   **
   ** update the playername
   **
   ** @param	player	player with the new name
   **
   ** @return	-
   **
   ** @author	Diether Knof
   **
   ** @version	0.5.4
   **
   **/
  void
    Genscher::name_update(Player const& player)
    {
      DEBUG_CALLING(INFO_GTK_GENSCHER && INFO_GTK_UPDATE,
		    "Genscher::name_update(player)");

      if (this->player.size() > player.no())
	gtk_label_set(GTK_LABEL(GTK_BIN(this->player[player.no()])->child),
		      player.name().c_str());

      DEBUG_RETURNING(VOID,
		      INFO_GTK_GENSCHER && INFO_GTK_UPDATE,
		      "Genscher::name_update(player)");
    } // void Genscher::name_update(Player const& player)

  /**
   **
   ** update the language
   **
   ** @param	-
   **
   ** @return	-
   **
   ** @author	Diether Knof
   **
   ** @version	0.5.4
   **
   **/
  void
    Genscher::language_update()
    {
      DEBUG_CALLING(INFO_GTK_GENSCHER && INFO_GTK_UPDATE,
		    "Genscher::language_update()");

      this->PlayfieldDialog::language_update();
      gtk_label_set_text(GTK_LABEL(this->description),
			 ::language("Select a new partner or keep the old."
				   ).c_str());
      gtk_frame_set_label(GTK_FRAME(this->player_frame),
			  ::language("new partner").c_str());
      gtk_label_set(GTK_LABEL(GTK_BIN(this->button_close)->child),
		    ::language("no genscher").c_str());

      DEBUG_RETURNING(VOID,
		      INFO_GTK_GENSCHER && INFO_GTK_UPDATE,
		      "Genscher::language_update()");
    } // void Genscher::language_update()

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