/**********************************************************************
 *
 *   FreeDoko a Doppelkopf-Game
 *    
 *   Copyright (C) 2001-2005  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 "announcement_info.h"
#include "table.h"
#include "ui.h"
#include "translations.h"
#include "icons.h"

#include "../../player/player.h"
#include "../../misc/setting.h"

#include "widgets/stock_button.h"
#include <gtkmm/stock.h>
#include <gtkmm/box.h>
#include <gtkmm/image.h>
#include <gtkmm/label.h>

namespace UI_GTKMM_NS {

  /**
   **
   ** Constructor
   **
   ** @param	table	the table
   **
   ** @return	-
   ** 
   ** @author	Diether Knof
   **
   ** @version	0.6.6
   **
   **/
  AnnouncementInfo::AnnouncementInfo(Table* const table) :
    GameinfoDialog(table),
  icon(NULL),
  text(NULL),
  reply(NULL),
  player(NULL)
  {
    return ;
  } // AnnouncementInfo::AnnouncementInfo(Table* const table)

  /**
   **
   ** Destruktor
   **
   ** @param	-
   **
   ** @return	-
   ** 
   ** @author	Diether Knof
   **
   ** @version	0.6.1
   **
   **/
  AnnouncementInfo::~AnnouncementInfo()
  {
    return ;
  } // AnnouncementInfo::~AnnouncementInfo()

  /**
   **
   ** init the dialog
   **
   ** @param	-
   **
   ** @return	-
   ** 
   ** @author	Diether Knof
   **
   ** @version	0.6.1
   **
   **/
  void
    AnnouncementInfo::init()
    {
      this->GameinfoDialog::init();

      this->ui->translations->add(*this,
				    ::translator("announcement"));


      this->icon = this->ui->icons->new_managed_image();
      this->text = Gtk::manage(new Gtk::Label("Player announces 'announcement'."));
      this->ui->translations->add(*(this->text),
				    ::translator("announcement"));

      this->close_button->show_all();

      Gtk::VBox* vbox = Gtk::manage(new Gtk::VBox(false, 2 ex));
      vbox->set_border_width(2 ex);

      vbox->add(*(this->icon));
      vbox->add(*(this->text));

      this->get_vbox()->add(*vbox);

      { // show 'announcement reply' button
	this->reply = Gtk::manage(new Gtk::StockButton(Gtk::Stock::OK,
						       "announcement reply"));
	this->ui->translations->add(*(this->reply),
				      ::translator("announcement reply"));

	this->add_action_widget(*(this->reply), Gtk::RESPONSE_ACCEPT);
	this->get_action_area()->reorder_child(*(this->close_button), -1);

	this->reply->signal_clicked().connect(SigC::slot(*this, &GameinfoDialog::close));
	this->reply->signal_clicked().connect(SigC::slot(*this, &AnnouncementInfo::reply_event));
      } // show 'announcement reply' button

      this->show_all_children();

      return ;
    } // void AnnouncementInfo::init()

  /**
   **
   ** event: make a reply
   **
   ** @param	-
   **
   ** @return	-
   ** 
   ** @author	Diether Knof
   **
   ** @version	0.6.4
   **
   **/
  void
    AnnouncementInfo::reply_event()
    {
      DEBUG_ASSERTION(this->player->game().human_player(),
		      "UI_GTKMM_NS::AnnouncementInfo::reply_event():\n"
		      "  Found no human player.");

      this->ui->game().announcement_make(ANNOUNCEMENT::to_reply(this->player->announcement()),
					 *(this->ui->game().human_player()));


      return ;
    } // void AnnouncementInfo::reply_event()

  /**
   **
   ** show the window
   **
   ** @param	player	player who has made the announcement
   **
   ** @return	-
   ** 
   ** @author	Diether Knof
   **
   ** @version	0.6.6
   **
   **/
  void
    AnnouncementInfo::show_information(Player const& player)
    {
      this->player = &player;
      if (!this->is_realized())
	this->realize();

      if (this->player->announcement() == ANNOUNCEMENT::NO120)
	this->ui->translations->change(*this,
				       ::translator(this->player->team()));

      else
	this->ui->translations->change(*this,
				       ::translator(this->player->announcement()
						   ));


      if (this->player->announcement() == ANNOUNCEMENT::NO120)
	this->ui->icons->change_managed(this->icon, this->player->team());
      else
	this->ui->icons->change_managed(this->icon, this->player->announcement());

      if (this->player->announcement() == ANNOUNCEMENT::NO120) {
	this->ui->translations->change(*(this->text),
				       ::translator("%splayer% announces '%tannouncement%'.",
						    this->player->name(),
						    ::translator(this->player->team())
						   ));

      } else {
	this->ui->translations->change(*(this->text),
				       ::translator("%splayer% announces '%tannouncement%'.",
						    this->player->name(),
						    ::translator(this->player->announcement())
						   ));
      }

      {
	Game const& game = this->player->game();
	// a human can announce 
	if (game.rule()(Rule::ANNOUNCEMENT_REPLY)
	    && (game.humanno() == 1)
	    && game.announcement_valid(ANNOUNCEMENT::REPLY,
				       *(game.human_player()))) {
	  this->reply->show();
	  this->ui->translations->change(*(this->reply),
					 ::translator(ANNOUNCEMENT::to_reply(this->player->announcement())
						     ));
	} else { // if !(human can reply)
	  this->reply->hide();
	} // if !(human can reply)
      }

      this->GameinfoDialog::show_information(::setting(Setting::SHOW_ANNOUNCEMENT_WINDOW),
					     ::setting(Setting::CLOSE_ANNOUNCEMENT_WINDOW_AUTOMATICALLY),
					     ::setting(Setting::ANNOUNCEMENT_WINDOW_CLOSE_DELAY));

      this->ui->icons->change_managed(this->icon, Icons::NONE);
      this->player = NULL;

      return ;
    } // void AnnouncementInfo::show_information(Player const& player)

  /**
   **
   ** 'player' has made an announcement
   **
   ** @param	player	the player who has made the announcement
   **
   ** @return	-
   ** 
   ** @author	Diether Knof
   **
   ** @version	0.6.4
   **
   **/
  void
    AnnouncementInfo::announcement_made(Player const& player)
    {
      if (this->reply
	  && this->reply->get_parent()) {
	this->reply->hide();
      }

      return ;
    } // void AnnouncementInfo::announcement_made(Player 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
    AnnouncementInfo::name_changed(Player const& player)
    {
      if (player.is_same(*(this->player))) {
	if (this->player->announcement() == ANNOUNCEMENT::NO120) {
	  this->ui->translations->change(*(this->text),
					 ::translator("%splayer% announces '%tannouncement%'.",
						      this->player->name(),
						      ::translator(::name(this->player->team()))
						     ));

	} else {
	  this->ui->translations->change(*(this->text),
					 ::translator("%splayer% announces '%tannouncement%'.",
						      this->player->name(),
						      ::translator(::name(this->player->announcement()))
						     ));
	}
      } // if (player.is_same(*(this->player)))

      return ;
    } // void AnnouncementInfo::name_changed(Player const& player)

} // namespace UI_GTKMM_NS

#endif // #ifdef USE_UI_GTKMM
