/**********************************************************************
 *
 *   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 "menu.h"
#include "gtk_gui.h"

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

// Announcement
// +-Player 0
// | +-re/contra
// | +-no 90
// | +-no 60
// | +-no 30
// | `-no 0
// +-Player 1
// | :...
// +-Player 2
// | :...
// `-Player 3
//   :...

namespace GTK_DOKO {

  /**
   **
   ** inits the announcements submenu
   **
   ** @param	-
   **
   ** @return	-
   **
   ** @author	Diether Knof
   ** 
   ** @version	0.5.1
   **
   **/
  void
    Menu::announcements_init()
    {
      DEBUG_CALLING(INFO_GTK_MENU && INFO_GTK_INIT,
		    "Menu::announcements_init()");

      gtk_object_set_data(GTK_OBJECT(GTK_BIN(this->announcements_item)->child),
			  "text",
			  new string("announcements"));
      this->labels.push_back(GTK_LABEL(GTK_BIN(this->announcements_item)->child));
      gtk_menu_bar_append(GTK_MENU_BAR(this->bar), this->announcements_item);

      DEBUG_RETURNING(VOID,
		      INFO_GTK_MENU && INFO_GTK_INIT,
		      "void Menu::announcements_init()");
    } // void Menu::announcements_init()

  /**
   **
   ** recreate the menu
   **
   ** @param	-
   **
   ** @return	-
   ** 
   ** @author	Diether Knof
   **
   ** @version	0.5.1
   **
   **/
  void
    Menu::announcements_game_start()
    {
      DEBUG_CALLING(INFO_GTK_MENU && INFO_GTK_GAMEPLAY,
		    "Menu::announcements_game_start()");

      // free the data
      if (!this->announcements_announcement.empty())
	for (unsigned p = 0; p < ::party.game().playerno(); p++)
	  for (unsigned a = 0; a < ANNOUNCEMENT::NUMBER; a++)
	    delete (Announcement*)(gtk_object_get_data(GTK_OBJECT(this->announcements_announcement[p][a]),
						       "announcement"));

      // recreate the whole menu
      //  gtk_menu_detach(GTK_MENU(this->menu));
      //  gtk_widget_destroy(this->menu);
      GtkWidget* menu = gtk_menu_new();
      gtk_menu_item_set_submenu(GTK_MENU_ITEM(this->announcements_item),
				menu); 

      this->announcements_name.clear();
      this->announcements_announcement.clear();

      for (vector<Player*>::const_iterator player = ::party.game().player_begin();
	   player != ::party.game().player_end();
	   player++) {

	GtkWidget* item;
	GtkWidget* submenu;

	item = gtk_menu_item_new_with_label((*player)->name().c_str());
	this->announcements_name.push_back(item);

	if ((*player)->game().humanno() > 1) {
	  if ((*player)->type() == Player::HUMAN)
	    gtk_menu_append(GTK_MENU(menu), item);
	  submenu = gtk_menu_new();
	  gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), submenu); 
	} else { // if !((*player)->game().humanno() > 1)
	  item = this->announcements_item;
	  if ((*player)->type() == Player::HUMAN)
	    submenu = menu;
	  else
	    submenu = gtk_menu_new();
	} // if ((*player)->game().humanno() == 1)
	gtk_menu_append(GTK_MENU(submenu), gtk_tearoff_menu_item_new());

	this->announcements_announcement.push_back(vector<GtkWidget*>());
	for (unsigned a = 0; a < ANNOUNCEMENT::NUMBER; a++) {
	  item = gtk_radio_menu_item_new_with_label(((a == 0)
						     ? NULL
						     : gtk_radio_menu_item_group(GTK_RADIO_MENU_ITEM(this->announcements_announcement[(*player)->no()][0]))),
						    "");
	  this->announcements_announcement[(*player)->no()].push_back(item);

	  gtk_object_set_data(GTK_OBJECT(item),
			      "player",
			      &(**player));
	  gtk_object_set_data(GTK_OBJECT(item),
			      "announcement",
			      new Announcement(Announcement(a)));
	  gtk_signal_connect(GTK_OBJECT(item), "toggled",
			     GTK_SIGNAL_FUNC(Menu::announcements_announce_event),
			     NULL);

	  if (a > 0)
	    gtk_menu_append(GTK_MENU(submenu), item);
	} // for (a < ANNOUNCEMENT::NUMBER)
	gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(this->announcements_announcement[(*player)->no()][0]),
				       TRUE);
	// don't create a menu, if the player is not a human
	if ((*player)->type() != Player::HUMAN)
	  continue;
      } // for (p < ::party.game().playerno())

      this->language_update();

      gtk_widget_show_all(menu);
      gtk_widget_set_sensitive(this->announcements_item, TRUE);

      DEBUG_RETURNING(VOID,
		      INFO_GTK_MENU && INFO_GTK_GAMEPLAY,
		      "Menu::announcements_game_start()");
    } // void Menu::announcements_game_start()

  /**
   **
   ** update the sensitivity
   **
   ** @param	-
   **
   ** @return	-
   ** 
   ** @author	Diether Knof
   **
   ** @version	0.5.1
   **
   **/
  void
    Menu::announcements_sensitivity_update()
    {
      DEBUG_CALLING(INFO_GTK_MENU && INFO_GTK_GAMEPLAY,
		    "Menu::announcements_sensitivity_update()");

      bool possible = false;

      for (vector<Player*>::const_iterator player = ::party.game().player_begin();
	   player != ::party.game().player_end();
	   player++) {
	for (unsigned a = ANNOUNCEMENT::NO120; a < ANNOUNCEMENT::NUMBER; a++) {
	  gtk_widget_set_sensitive(this->announcements_announcement[(*player)->no()][a],
				   (::party.game().announcement_valid(Announcement(a),
								      **player)
				    ? TRUE
				    : FALSE));
	  if (((*player)->type() == Player::HUMAN)
	      && (this->announcements_announcement[(*player)->no()][a],
		  (::party.game().announcement_valid(Announcement(a),
						     **player))))
	    possible = true;
	} // for (a \in ANNOUNCEMENT)
      } // for (player)

      gtk_widget_set_sensitive(this->announcements_item,
			       (possible ? TRUE : FALSE));

      DEBUG_RETURNING(VOID,
		      INFO_GTK_MENU && INFO_GTK_GAMEPLAY,
		      "Menu::announcements_sensitivity_update()");
    } // void Menu::announcements_sensitivity_update()

  /**
   **
   ** make the announcement
   **
   ** @param	button	button which has been pressed
   ** @param	-
   **
   ** @return	-
   ** 
   ** @author	Diether Knof
   **
   ** @version	0.5.1
   **
   **/
  void
    Menu::announcements_announce_event(GtkWidget* button, gpointer)
    {
      DEBUG_CALLING(INFO_GTK_MENU && INFO_GTK_EVENT,
		    "Menu::announcements_announce_event()");

      Player& player = *static_cast<Player*>(gtk_object_get_data(GTK_OBJECT(button),
								 "player"));
      Announcement const announcement
	= *static_cast<Announcement*>(gtk_object_get_data(GTK_OBJECT(button),
							  "announcement"));
      player.game().announcement_make(announcement, player);

      DEBUG_RETURNING(VOID,
		      INFO_GTK_MENU && INFO_GTK_EVENT,
		      "Menu::announcements_announce_event()");
    } // static void Menu::announcements_announce_event(GtkWidget* window, gpointer)

  /**
   **
   ** update the name of the player
   **
   ** @param	player	the player
   **
   ** @return	-
   ** 
   ** @author	Diether Knof
   **
   ** @version	0.5.1
   **
   **/
  void
    Menu::announcements_name_update(Player const& player)
    {
      DEBUG_CALLING(INFO_UI && INFO_GTK_UI,
		    "Menu::announcements_name_update(player)");

      if (::game_status & GAMESTATUS::GAME)
	if (!this->announcements_name.empty())
	  gtk_label_set_text(GTK_LABEL(GTK_BIN(this->announcements_name[player.no()])->child),
			     player.name().c_str());

      DEBUG_RETURNING(VOID,
		      INFO_UI && INFO_GTK_UI,
		      "Menu::announcements_name_update(player)");
    } // void Menu::announcements_name_update(Player const& player)

  /**
   **
   ** update the announcement of the player
   **
   ** @param	player	the player
   **
   ** @return	-
   ** 
   ** @author	Diether Knof
   **
   ** @version	0.5.1
   **
   **/
  void
    Menu::announcement_update(Player const& player)
    {
      DEBUG_CALLING(INFO_UI && INFO_GTK_UI,
		    "Menu::announcement_update(player)");

      for (unsigned a = 0; a < ANNOUNCEMENT::NUMBER; a++)
	gtk_signal_handler_block_by_func(GTK_OBJECT(this->announcements_announcement[player.no()][a]),
					 GTK_SIGNAL_FUNC(Menu::announcements_announce_event),
					 NULL);

      gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(this->announcements_announcement[player.no()][player.announcement()]),
				     TRUE);

      for (unsigned a = 0; a < ANNOUNCEMENT::NUMBER; a++)
	gtk_signal_handler_unblock_by_func(GTK_OBJECT(this->announcements_announcement[player.no()][a]),
					   GTK_SIGNAL_FUNC(Menu::announcements_announce_event),
					   NULL);

      for(vector<Player*>::const_iterator p = ::party.game().player_begin();
	  p != ::party.game().player_end();
	  p++)
	if ((*p)->team() == player.team())
	  for (unsigned a = 0;
	       a <= unsigned(player.announcement());
	       a++)
	    gtk_widget_set_sensitive(this->announcements_announcement[(*p)->no()][a], FALSE);

      DEBUG_RETURNING(VOID,
		      INFO_UI && INFO_GTK_UI,
		      "Menu::announcement_update(player)");
    } // void Menu::announcement_update(Player const& player)

  /**
   **
   ** update the language
   **
   ** @param	-
   **
   ** @return	-
   ** 
   ** @author	Diether Knof
   **
   ** @version	0.5.1
   **
   **/
  void
    Menu::announcements_language_update()
    {
      DEBUG_CALLING(INFO_GTK_MENU && INFO_GTK_UPDATE,
		    "Menu::announcements_language_update()");

      if (!this->announcements_announcement.empty())
	for(vector<Player*>::const_iterator p = ::party.game().player_begin();
	    p != ::party.game().player_end();
	    p++)
	  for (unsigned a = 0; a < ANNOUNCEMENT::NUMBER; a++)
	    gtk_label_set_text(GTK_LABEL(GTK_BIN(this->announcements_announcement[(*p)->no()][a])->child),
			       ((Announcement(a) == ANNOUNCEMENT::NO120)
				? ::language((*p)->team())
				: ::language(Announcement(a))
			       ).c_str());

      DEBUG_RETURNING(VOID,
		      INFO_GTK_MENU && INFO_GTK_UPDATE,
		      "Menu::announcements_language_update()");
    } // void Menu::announcements_language_update()

} // namespace GTK_DOKO
#endif // #ifndef GTK
