/**********************************************************************
 *
 *   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 "last_trick.h"
#include "table.h"
#include "trick_summary.h"

#include "ui.h"
#include "main_window.h"

#include "../../game/game.h"
#include "../../party/rule.h"
#include "../../card/trick.h"
#include "../../player/player.h"

#include <gtkmm/box.h>
#include <gtkmm/button.h>
#include <gtkmm/arrow.h>
#include <gtkmm/alignment.h>
#include <gtkmm/label.h>
#include <gtkmm/main.h>
#include <gtkmm/stock.h>
#include <gdk/gdkkeysyms.h>

namespace UI_GTKMM_NS {

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

  /**
   **
   ** Constructor
   **
   ** @param	table	the table
   ** @param	trick	the trick
   **
   ** @return	-
   ** 
   ** @author	Diether Knof
   **
   ** @version	0.6.1
   **
   **/
  LastTrick::LastTrick(Table* const table, ::Trick const& last_trick) :
    GameinfoDialog(table),
  last_trick(&last_trick),
  trickno(0),
  navigation_container(NULL),
  previous_trick_button(NULL),
  following_trick_button(NULL),
  trick_summary(NULL)
  {
    DEBUG_CALLING(INFO_GTKMM_LAST_TRICK && INFO_GTKMM_INIT,
		  "LastTrick::LastTrick(table, last_trick)");

    DEBUG_RETURNING(VOID,
		    INFO_GTKMM_LAST_TRICK && INFO_GTKMM_INIT,
		    "LastTrick::LastTrick(table, last_trick)");
  } // LastTrick::LastTrick(Table* const table, ::Trick const& last_trick)

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

    delete this->trick_summary;

    DEBUG_RETURNING(VOID,
		    INFO_GTKMM_LAST_TRICK && INFO_GTKMM_INIT,
		    "LastTrick::~LastTrick()");
  } // LastTrick::~LastTrick()

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

      this->GameinfoDialog::init();
      this->set_position(Gtk::WIN_POS_CENTER_ON_PARENT);


      this->ui->add_for_translation(*this,
				    ::translator("Last trick"));


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

      if ((this->last_trick->game().rule()(Rule::SHOW_TRICKS_NUMBER) > 1)
	  && (this->last_trick->game().trick_current_no() >= 2)) {
	this->navigation_container = Gtk::manage(new Gtk::HBox());
	this->previous_trick_button = Gtk::manage(new Gtk::Button());
	this->following_trick_button = Gtk::manage(new Gtk::Button());
	this->previous_trick_button->add(*(Gtk::manage(new Gtk::Image(Gtk::Stock::GO_BACK, Gtk::ICON_SIZE_BUTTON))));
	this->following_trick_button->add(*(Gtk::manage(new Gtk::Image(Gtk::Stock::GO_FORWARD, Gtk::ICON_SIZE_BUTTON))));
	this->navigation_container->pack_start(*(this->previous_trick_button),
					       false, true);
	this->navigation_container->pack_end(*(this->following_trick_button),
					     false, true);
	this->navigation_container->pack_start(*(Gtk::manage(new Gtk::Alignment())),
					       Gtk::PACK_EXPAND_WIDGET);


	this->previous_trick_button->signal_clicked().connect(SigC::slot(*this, &LastTrick::previous_trick));
	this->following_trick_button->signal_clicked().connect(SigC::slot(*this, &LastTrick::following_trick));

	this->get_vbox()->add(*(this->navigation_container));
      } // if ((this->last_trick->game().rule()(Rule::SHOW_TRICKS_NUMBER) > 1) && (this->last_trick->game().trick_current_no() >= 2))

      this->trick_summary = new TrickSummary(this);
      this->trick_summary->set_trick(*(this->last_trick));

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

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

      this->signal_key_press_event().connect(SigC::slot(*this,
							&LastTrick::key_press));

      DEBUG_RETURNING(VOID,
		      INFO_GTKMM_LAST_TRICK && INFO_GTKMM_INIT,
		      "LastTrick::init()");
    } // void LastTrick::init()

  /**
   **
   ** show the last trick
   **
   ** @param	-
   **
   ** @return	-
   ** 
   ** @author	Diether Knof
   **
   ** @version	0.6.1
   **
   **/
  void
    LastTrick::show_last_trick()
    {
      DEBUG_CALLING(INFO_GTKMM_LAST_TRICK && INFO_GTKMM_GAMEPLAY,
		    "LastTrick::show_last_trick()");

      if (this->last_trick->game().trick_current_no() < 1)
	DEBUG_RETURNING(VOID,
			INFO_GTKMM_LAST_TRICK && INFO_GTKMM_GAMEPLAY,
			"LastTrick::show_last_trick()");

      this->realize();
      this->set_trickno(this->last_trick->game().trick_current().isfull()
			? (this->last_trick->game().trick_current_no() - 2)
			: (this->last_trick->game().trick_current_no() - 1));
      this->move_to_last_position();
      this->present();

      while ((::game_status == GAMESTATUS::GAME_SHOW_LAST_TRICK)
	     && (this->is_visible()))
	Gtk::Main::iteration();

      this->save_position();
      this->hide();

      DEBUG_RETURNING(VOID,
		      INFO_GTKMM_LAST_TRICK && INFO_GTKMM_GAMEPLAY,
		      "LastTrick::show_last_trick()");
    } // void LastTrick::show_last_trick()

  /**
   **
   ** set the trickno
   **
   ** @param	trickno	new trickno
   **
   ** @return	-
   ** 
   ** @author	Diether Knof
   **
   ** @version	0.6.1
   **
   **/
  void
    LastTrick::set_trickno(unsigned const trickno)
    {
      DEBUG_CALLING(INFO_GTKMM_LAST_TRICK && INFO_GTKMM_UPDATE,
		    "LastTrick::set_trickno(trickno)");

      this->trickno = trickno;
      this->trick_summary->set_trick(this->last_trick->game().trick(this->trickno));
      if (this->previous_trick_button != NULL)
	this->previous_trick_button->set_sensitive((this->trickno > 0)
						   && (this->trickno
						       + this->last_trick->game().rule()(Rule::SHOW_TRICKS_NUMBER)
						       > this->last_trick->game().trick_current_no())
						  );
      if (this->following_trick_button != NULL)
	this->following_trick_button->set_sensitive(this->trickno
						    < this->last_trick->game().trick_current_no() - 1);

      if (this->trickno == this->last_trick->game().trick_current_no() - 1)
	this->ui->change_translation(*this,
				     ::translator("Last trick"));
      else
	this->ui->change_translation(*this,
				     ::translator("%unumber%. trick", this->trickno + 1));

      DEBUG_RETURNING(VOID,
		      INFO_GTKMM_LAST_TRICK && INFO_GTKMM_UPDATE,
		      "LastTrick::set_trickno(trickno)");
    } // void LastTrick::set_trickno(unsigned const trickno)

  /**
   **
   ** show the previous trickno
   **
   ** @param	-
   **
   ** @return	-
   ** 
   ** @author	Diether Knof
   **
   ** @version	0.6.1
   **
   **/
  void
    LastTrick::previous_trick()
    {
      DEBUG_CALLING(INFO_GTKMM_LAST_TRICK && INFO_GTKMM_UPDATE,
		    "LastTrick::previous_trick()");

      if (this->trickno > 0)
	if (this->trickno
	    + this->last_trick->game().rule()(Rule::SHOW_TRICKS_NUMBER)
	    > this->last_trick->game().trick_current_no())
	  this->set_trickno(this->trickno - 1);

      DEBUG_RETURNING(VOID,
		      INFO_GTKMM_LAST_TRICK && INFO_GTKMM_UPDATE,
		      "LastTrick::previous_trick()");
    } // void LastTrick::previous_trick()

  /**
   **
   ** show the following trickno
   **
   ** @param	-
   **
   ** @return	-
   ** 
   ** @author	Diether Knof
   **
   ** @version	0.6.1
   **
   **/
  void
    LastTrick::following_trick()
    {
      DEBUG_CALLING(INFO_GTKMM_LAST_TRICK && INFO_GTKMM_UPDATE,
		    "LastTrick::following_trick()");

      if (this->trickno
	  < this->last_trick->game().trick_current_no() - 1)
	this->set_trickno(this->trickno + 1);

      DEBUG_RETURNING(VOID,
		      INFO_GTKMM_LAST_TRICK && INFO_GTKMM_UPDATE,
		      "LastTrick::following_trick()");
    } // void LastTrick::following_trick()

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

      this->trick_summary->update();

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

  /**
   **
   ** a key has been pressed
   **
   ** @param	key	the key
   **
   ** @return	from 'ui->key_press(key)'
   **
   ** @version	0.6.1
   **
   ** @author	Diether Knof
   **
   ** @bug	signal does not get here for '->', '<-' and '<Esc>'
   **
   **/
  bool
    LastTrick::key_press(GdkEventKey* key)
    {
      DEBUG_CALLING(INFO_GTKMM_LAST_TRICK && INFO_GTKMM_EVENT,
		    "LastTrick::key_press(key)");

      // whether the key was accepted
      bool accepted = false;

      if ((key->state & GDK_SHIFT_MASK) == 0) {
	switch (key->keyval) {
	case GDK_Left: // show previous trick
	  this->previous_trick();
	  accepted = true;
	  break;
	case GDK_Right: // show following trick
	  this->following_trick();
	  accepted = true;
	  break;
	case GDK_Escape: // close the window
	  this->hide();
	  accepted = true;
	  break;
	} // switch (key->keyval)
      } // if (key->state == 0)

      if (accepted)
	DEBUG_RETURNING(false,
			INFO_GTKMM_LAST_TRICK && INFO_GTKMM_EVENT,
			"LastTrick::key_press(key)");

      DEBUG_RETURNING(this->ui->key_press(key),
		      INFO_GTKMM_LAST_TRICK && INFO_GTKMM_EVENT,
		      "LastTrick::key_press(key)");
    } // bool LastTrick::key_press(GdkEventKey* key)

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

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

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

} // namespace UI_GTKMM_NS

#endif // #ifdef USE_UI_GTKMM
