/**********************************************************************
 *
 *   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 "gametype.h"
#include "gtk_gui.h"
#include "icon.h"
#include "main_window.h"
#include "playfield.h"

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

namespace GTK_DOKO {
#include <gtk/gtkpixmap.h>
#include <gtk/gtklabel.h>
#include <gtk/gtkdialog.h>
#include <gtk/gtkmain.h>

  /*****************************************************************************
   *
   * Gametype::Gametype(Playfield* playfield_a)
   *
   * Parameters:	playfield_a - pointer to the playfield
   *
   * Result:	none
   * 
   * Version:	0.3.6
   *
   * Description:	Constructor
   *
   ****************************************************************************/
  Gametype::Gametype(Playfield* playfield_a) :
    PlayfieldDialog(playfield_a, "Gametype"), // the base class
  icon(playfield_a->gui().icons().icon(GAMETYPE::NORMAL).widget()), // the gametype
  text(gtk_label_new("")) // the text
  {
    DEBUG_CALLING(INFO_GTK_GAMETYPE && INFO_GTK_INIT,
		  "Gametype::Gametype()");

    // pack the widgets in the window
    gtk_container_add(GTK_CONTAINER(GTK_DIALOG(window())->vbox), icon);
    gtk_container_add(GTK_CONTAINER(GTK_DIALOG(window())->vbox), text);
    gtk_container_add(GTK_CONTAINER(GTK_DIALOG(window())->action_area),
		      button_close);

    DEBUG_RETURNING(VOID,
		    INFO_GTK_GAMETYPE && INFO_GTK_INIT,
		    "Gametype::Gametype()");
  } // Gametype::Gametype(Playfield* playfield_a)

  /*****************************************************************************
   *
   * void Gametype::init()
   *
   * Parameters:	none
   *
   * Result:	none
   * 
   * Version:	0.3.6
   *
   * Description:	inits the 'about' help window
   *
   ****************************************************************************/
  void
    Gametype::init()
    {
      DEBUG_CALLING(INFO_GTK_GAMETYPE && INFO_GTK_INIT,
		    "Gametype::init()");

      this->PlayfieldDialog::init();

      gtk_window_set_transient_for(GTK_WINDOW(this->window()),
				   GTK_WINDOW(gui().main_window().window()));
      { // center the window
	int x, y, width, height, wwidth, wheight;

	gdk_window_get_geometry(gui().main_window().window()->window,
				&x, &y, &width, &height, NULL);
	gdk_window_get_size(this->window()->window, &wwidth, &wheight);
	gdk_window_move(this->window()->window,
			x + width / 2 - wwidth / 2, y + height / 2 - wheight / 2);
      } // center the window

      DEBUG_RETURNING(VOID,
		      INFO_GTK_GAMETYPE && INFO_GTK_INIT,
		      "Gametype::init()");
    } // void Gametype::init()

  /*****************************************************************************
   *
   * void Gametype::show()
   *
   * Parameters:	none
   *
   * Result:	none
   *
   * Version:	0.3.6
   *
   * Description:	shows the window
   *
   ****************************************************************************/
  void
    Gametype::show()
    {
      DEBUG_CALLING(INFO_GTK_GAMETYPE && INFO_GTK_EVENT,
		    "Gametype::show()");

      DEBUG_ASSERTION(((::game_status == GAMESTATUS::GAME_SHOW_GAMETYPE)
		       || (::game_status == GAMESTATUS::GAME_REDISTRIBUTE)),
		      "Gamtype::show()\n"
		      "  'game_status' != GAME_RESERVATION");
      if (party.game().type() != GAMETYPE::NORMAL) {
	if (!FAST_PLAY) {
	  // update the card and the text
	  this->icons_update();
	  this->language_update();

	  // show the window
	  this->PlayfieldDialog::show();
	} // if (!FAST_PLAY)

	// close the window
	while (((::game_status == GAMESTATUS::GAME_SHOW_GAMETYPE)
		|| (::game_status == GAMESTATUS::GAME_REDISTRIBUTE))
	       && (GTK_WIDGET_VISIBLE(window())))
	  gtk_main();

	close();
      }

      DEBUG_RETURNING(VOID,
		      INFO_GTK_GAMETYPE && INFO_GTK_EVENT,
		      "Gametype::show()");
    } // void Gametype::show()

  /*****************************************************************************
   *
   * void Gametype::icons_update()
   *
   * Parameters:	none
   *
   * Result:	none
   * 
   * Version:	0.3.6
   *
   * Description:	updates the cards in the gametype window
   *
   ****************************************************************************/
  void
    Gametype::icons_update()
    {
      DEBUG_CALLING(INFO_GTK_GAMETYPE && INFO_GTK_UPDATE,
		    "Gametype::icons_update()");

      if (::game_status & GAMESTATUS::GAME) {
	gtk_pixmap_set(GTK_PIXMAP(icon),
		       gui().icons().icon(party.game().type()).pixmap(),
		       gui().icons().icon(party.game().type()).mask());

	if (party.game().type() == GAMETYPE::MARRIAGE)
	  gtk_pixmap_set(GTK_PIXMAP(icon),
			 gui().icons().icon(party.game().marriage_selector()).pixmap(),
			 gui().icons().icon(party.game().marriage_selector()).mask());
      } // if (::game_status p& GAMESTATUS::GAME)


      DEBUG_RETURNING(VOID,
		      INFO_GTK_GAMETYPE && INFO_GTK_UPDATE,
		      "Gametype::icons_update()");
    } // void Gametype::icons_update()


  /*****************************************************************************
   *
   * void Gametype::language_update()
   *
   * Parameters:	none
   *
   * Result:	none
   * 
   * Version:	0.3.6
   *
   * Description:	updates the language in the gametype window
   *
   ****************************************************************************/
  void
    Gametype::language_update()
    {
      DEBUG_CALLING(INFO_GTK_GAMETYPE && INFO_GTK_UPDATE,
		    "Gametype::language_update()");

      this->PlayfieldDialog::language_update();

      // the description
      if (game_status & GAMESTATUS::GAME) {
	string description = "";
	if (GAMETYPE::is_solo(party.game().type()))
	  description = ::language("%splayer% plays a %sgametype%.",
				   ::party.game().soloplayer().name().c_str(),
				   ::language(::party.game().type()).c_str());
	else if ((party.game().type() == GAMETYPE::FIVE_NINES)
		 || (party.game().type() == GAMETYPE::SIX_KINGS))
	  description = ::language("%splayer% has %sgametype%.",
				   ::party.game().soloplayer().name().c_str(),
				   ::language(::party.game().type()).c_str());
	else if (party.game().type() == GAMETYPE::POVERTY) {
	  description = ::language("%splayer% has a %sgametype%.",
				   ::party.game().soloplayer().name().c_str(),
				   ::language(::party.game().type()).c_str());
	  if (::game_status != GAMESTATUS::GAME_REDISTRIBUTE) {
	    for (vector<Player*>::const_iterator player
		 = ::party.game().player_begin();
		 player != ::party.game().player_end();
		 player++) {
	      if ((**player != ::party.game().soloplayer())
		  && ((*player)->team() == TEAM::RE))
		description = (description + "        \n        "
			       + ::language("Partner is %splayer%.",
					    (*player)->name().c_str()));
	    } // for (player)
	  } // if (::game_status != GAME_REDISTRIBUTE)
	} else if (party.game().type() == GAMETYPE::MARRIAGE)
	  description = ::language("%splayer% has a %sgametype%: %sselector%.",
				   ::party.game().soloplayer().name().c_str(),
				   ::language(::party.game().type()).c_str(),
				   ::language(party.game().marriage_selector()).c_str()
				  );
	else
	  description = ::language(::party.game().type());

	gtk_label_set_text(GTK_LABEL(text),
			   ("\n        "
			    + description
			    + "        \n").c_str());
      } // if (game_status & GAMESTATUS::GAME)

      DEBUG_RETURNING(VOID,
		      INFO_GTK_GAMETYPE && INFO_GTK_UPDATE,
		      "Gametype::language_update()");
    } // void Gametype::language_update()

}
#endif
