/**********************************************************************
 *
 *   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 "full_trick.h"
#include "trick_summary.h"
#include "ui.h"
#include "cards.h"

#include "../../card/trick.h"
#include "../../misc/setting.h"

#include <gtkmm/image.h>
#include <gtkmm/main.h>

namespace UI_GTKMM_NS {

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

  /**
   **
   ** Constructor
   **
   ** @param	table	the table
   ** @param	trick	the trick
   **
   ** @return	-
   ** 
   ** @author	Diether Knof
   **
   ** @version	0.6.1
   **
   **/
  FullTrick::FullTrick(Table* const table, Trick const& trick) :
    GameinfoDialog(table),
  trick_summary(NULL),
  winnercard(),
  trick(&trick)
  {
    DEBUG_CALLING(INFO_GTKMM_FULL_TRICK && INFO_GTKMM_INIT,
		  "FullTrick::FullTrick(table, trick)");

    DEBUG_RETURNING(VOID,
		    INFO_GTKMM_FULL_TRICK && INFO_GTKMM_INIT,
		    "FullTrick::FullTrick(table, trick)");
  } // FullTrick::FullTrick(Table* const table, Trick const& trick)

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

    delete this->trick_summary;

    DEBUG_RETURNING(VOID,
		    INFO_GTKMM_FULL_TRICK && INFO_GTKMM_INIT,
		    "FullTrick::~FullTrick()");
  } // FullTrick::~FullTrick()

  /**
   **
   ** create all subelements
   **
   ** @param	-
   **
   ** @return	-
   ** 
   ** @author	Diether Knof
   **
   ** @version	0.6.1
   **
   **/
  void
    FullTrick::init()
    {
      DEBUG_CALLING(INFO_GTKMM_FULL_TRICK && INFO_GTKMM_INIT,
		    "FullTrick::init()");

      this->GameinfoDialog::init();
      this->ui->add_for_translation(*this,
				    ::translator("Full trick"));

      this->get_vbox()->set_spacing(10);

      this->trick_summary = new TrickSummary(this, false);
      this->trick_summary->set_trick(*(this->trick));

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

      this->winnercard = Gtk::manage(new Gtk::Image());
      this->cards_update();
      this->trick_summary->pack_start(*(this->winnercard));

      this->show_all_children();
      this->trick_summary->update();

      DEBUG_RETURNING(VOID,
		      INFO_GTKMM_FULL_TRICK && INFO_GTKMM_INIT,
		      "FullTrick::init()");
    } // void FullTrick::init()

  /**
   **
   ** show the window
   **
   ** @param	-
   **
   ** @return	-
   ** 
   ** @author	Diether Knof
   **
   ** @version	0.6.1
   **
   **/
  void
    FullTrick::show_information()
    {
      DEBUG_CALLING(INFO_GTKMM_MARRIAGE_INFO && INFO_GTKMM_GAMEPLAY,
		    "FullTrick::show_information()");

      bool show = ::setting(Setting::SHOW_FULL_TRICK_WINDOW);
      if (!show
	  && ::setting(Setting::SHOW_FULL_TRICK_WINDOW_IF_SPECIAL_POINTS)) {
	// test whether the teams of the player of and for are different
	for (Specialpointsvector::const_iterator
	     sp = this->trick->specialpoints().begin();
	     sp != this->trick->specialpoints().end();
	     sp++) {
	  if ( (sp->player_of_no == UINT_MAX)
	       || ( (this->trick->game().teaminfo_for_humans(this->trick->game().player(sp->player_get_no)) != TEAM::RE)
		    && (this->trick->game().teaminfo_for_humans(this->trick->game().player(sp->player_get_no)) != TEAM::CONTRA))
	       || (this->trick->game().teaminfo_for_humans(this->trick->game().player(sp->player_get_no))
		   != this->trick->game().teaminfo_for_humans(this->trick->game().player(sp->player_of_no)))) {
	    show = true;
	    break;
	  } // if (trick shall be shown)
	} // for (sp)
      } // if (!show)

      if (show) {
	//this->move_to_last_position();
	this->present();
      }

      SigC::Connection timeout_connection;
      if (::setting(Setting::CLOSE_FULL_TRICK_AUTOMATICALLY))
	timeout_connection
	  = Glib::signal_timeout().connect(SigC::Slot0<bool>(SigC::slot(*this, &GameinfoDialog::on_timeout)),
					   ::setting(Setting::FULL_TRICK_CLOSE_DELAY));

      ::game_status = GAMESTATUS::GAME_SHOW_FULL_TRICK;

      while (::game_status == GAMESTATUS::GAME_SHOW_FULL_TRICK)
	Gtk::Main::iteration();

      if (::setting(Setting::CLOSE_FULL_TRICK_AUTOMATICALLY))
	timeout_connection.disconnect();

      if (show) {
	//this->save_position();
	this->hide();
      }

      DEBUG_RETURNING(VOID,
		      INFO_GTKMM_MARRIAGE_INFO && INFO_GTKMM_GAMEPLAY,
		      "FullTrick::show_information()");
    } // void FullTrick::show_information()

  /**
   **
   ** close the full trick
   **
   ** @param	-
   **
   ** @return	-
   ** 
   ** @author	Diether Knof
   **
   ** @version	0.6.1
   **
   **/
  void
    FullTrick::close()
    {
      DEBUG_CALLING(INFO_GTKMM_MARRIAGE_INFO && INFO_GTKMM_GAMEPLAY,
		    "FullTrick::close()");

      ::game_status = GAMESTATUS::GAME_PLAY;
      this->GameinfoDialog::close();

      DEBUG_RETURNING(VOID,
		      INFO_GTKMM_MARRIAGE_INFO && INFO_GTKMM_GAMEPLAY,
		      "FullTrick::close()");
    } // void FullTrick::close()

  /**
   **
   ** updates the winnercard
   **
   ** @param	-
   **
   ** @return	-
   **
   ** @version	0.6.1
   **
   ** @author	Diether Knof
   **
   **/
  void
    FullTrick::cards_update()
    {
      DEBUG_CALLING(INFO_GTKMM_FULL_TRICK && INFO_GTKMM_UPDATE,
		    "FullTrick::cards_update()");

      if (!this->is_realized())
	DEBUG_RETURNING(VOID,
			INFO_GTKMM_FULL_TRICK && INFO_GTKMM_UPDATE,
			"FullTrick::cards_update()");

      this->winnercard->set(this->ui->cards->card(this->trick->winnercard()));
      this->winnercard->set_padding(this->ui->cards->width() / 5,
				    this->ui->cards->height() / 5);

      DEBUG_RETURNING(VOID,
		      INFO_GTKMM_FULL_TRICK && INFO_GTKMM_UPDATE,
		      "FullTrick::cards_update()");
    } // void FullTrick::cards_update()

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

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

      this->trick_summary->update();

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

  /**
   **
   ** save the position of the window
   **
   ** @param	-
   **
   ** @return	-
   **
   ** @version	0.6.4
   **
   ** @author	Diether Knof
   **
   **/
  void
    FullTrick::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 FullTrick::save_position()

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

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

} // namespace UI_GTKMM_NS

#endif // #ifdef USE_UI_GTKMM
