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

#include "ui.h"
#include "cards.h"

#include "../../player/player.h"
#include "../../game/game.h"
#include "../../game/game.h"
#include "../../party/rule.h"
#include "../../misc/setting.h"

#include "../../utils/string.h"

namespace UI_GTKMM_NS {

#define TRICKPILE_TRICKS_STEP_BIG_PART \
  static_cast<int>(this->ui->cards->height() / 15.0)
#define TRICKPILE_TRICKS_STEP_SMALL_PART \
    static_cast<int>(this->ui->cards->width() / 30.0)
#define TRICKPILE_CARDS_STEP_BIG_PART \
    static_cast<int>(this->ui->cards->height() * 0)
#define TRICKPILE_CARDS_STEP_SMALL_PART \
    static_cast<int>(this->ui->cards->width() / 20.0)

    /**
     **
     ** Constructor
     **
     ** @param	table	the table
     ** @param	player	the player
     **
     ** @return	-
     ** 
     ** @author	Diether Knof
     **
     ** @version	0.6.1
     **
     **/
    TrickPile::TrickPile(Table* const table, Player const* const player) :
    HTIN(table, player)
    {
      DEBUG_CALLING(INFO_GTKMM_TRICKPILE && INFO_GTKMM_INIT,
		    "TrickPile::TrickPile(table, player)");

      DEBUG_RETURNING(VOID,
		      INFO_GTKMM_TRICKPILE && INFO_GTKMM_INIT,
		      "TrickPile::TrickPile(table, player)");
    } // TrickPile::TrickPile(Table* const table, Player const* const player)

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

    DEBUG_RETURNING(VOID,
		    INFO_GTKMM_TRICKPILE && INFO_GTKMM_INIT,
		    "TrickPile::~TrickPile()");
  } // TrickPile::~TrickPile()

  /**
   **
   ** draws the trickpile
   **
   ** @param	update	whether to update the area
   **
   ** @return	-
   ** 
   ** @author	Diether Knof
   **
   ** @version	0.6.1
   **
   **/
  void
    TrickPile::draw(bool const update)
    {
      DEBUG_CALLING(INFO_GTKMM_TRICKPILE && INFO_GTKMM_DRAW,
		    "TrickPile::draw(update)");

      if ((::game_status >= GAMESTATUS::GAME_INIT)
	  && (::game_status <= GAMESTATUS::GAME_FINISHED)) {
	if (update)
	  this->table().clear(Gdk::Rectangle(this->pos_x(), this->pos_y(),
					     this->width(), this->height()));

	// draw the cards
	this->draw_tricks();
	if (this->player->game().rule()(Rule::SHOW_TRICKPILES_POINTS)
	    && (::setting(Setting::SHOW_TRICKPILES_POINTS)
		|| (::game_status == GAMESTATUS::GAME_FINISHED)))
	  this->draw_points();

	if (update)
	  this->table().update(Gdk::Rectangle(this->pos_x(), this->pos_y(),
					      this->width(), this->height()));

      } // if (GAME_RESERVATION <= game_status < GAME_FINISHED)

      DEBUG_RETURNING(VOID,
		      INFO_GTKMM_TRICKPILE && INFO_GTKMM_DRAW,
		      "TrickPile::draw(update)");
    } // void TrickPile::draw(bool const update = true)

  /**
   **
   ** draws the tricks in the trickpile
   **
   ** @param	-
   **
   ** @return	-
   ** 
   ** @author	Diether Knof
   **
   ** @version	0.6.1
   **
   **/
  void
    TrickPile::draw_tricks()
    {
      DEBUG_CALLING(INFO_GTKMM_TRICKPILE && INFO_GTKMM_DRAW,
		    "TrickPile::draw_tricks()");

      if (::game_status & GAMESTATUS::GAME) {
	for (unsigned t = 0; t < this->player->number_of_tricks(); t++)
	  for (unsigned c = 0; c < this->player->game().playerno(); c++)
	    this->table().pixmap->draw_pixbuf(this->table().gc,
					      this->ui->cards->back(this->pile_rotation()),
					      0, 0,
					      this->start_x()
					      + this->tricks_step_x() * t
					      + this->cards_step_x() * c,
					      this->start_y()
					      + this->tricks_step_y() * t
					      + this->cards_step_y() * c,
					      -1, -1,
					      Gdk::RGB_DITHER_NONE,
					      0, 0);
      } // if (::game_status & GAMESTATUS::GAME)

      DEBUG_RETURNING(VOID,
		      INFO_GTKMM_TRICKPILE && INFO_GTKMM_DRAW,
		      "TrickPile::draw_tricks()");
    } // void TrickPile::draw_tricks()

  /**
   **
   ** draws the points in the trickpile
   **
   ** @param	update	whether to update the area (default: false)
   **
   ** @return	-
   ** 
   ** @author	Diether Knof
   **
   ** @version	0.6.1
   **
   **/
  void
    TrickPile::draw_points(bool const update)
    {
      DEBUG_CALLING(INFO_GTKMM_TRICKPILE && INFO_GTKMM_DRAW,
		    "TrickPile::draw_points(update)");

      if (this->player->number_of_tricks() == 0)
	DEBUG_RETURNING(VOID,
			INFO_GTKMM_TRICKPILE && INFO_GTKMM_DRAW,
			"TrickPile::draw_points(update)");

      this->table().trickpile_points_layout->set_text(DK::Utils::String::to_string(this->player->points_in_trickpile()));

      int layout_width, layout_height;
      this->table().trickpile_points_layout->get_pixel_size(layout_width,
							    layout_height);
      int const x = (this->start_x()
		     + (this->tricks_step_x()
			* (this->player->number_of_tricks() - 1))
		     + (this->cards_step_x()
			* (this->player->game().playerno() - 1))
		     + this->ui->cards->width(this->pile_rotation()) / 2
		     - (layout_width / 2)
		    );
      int const y = (this->start_y()
		     + (this->tricks_step_y()
			* (this->player->number_of_tricks() - 1))
		     + (this->cards_step_y()
			* (this->player->game().playerno() - 1))
		     + this->ui->cards->height(this->pile_rotation()) / 2
		     - (layout_height / 2)
		    );

      this->table().pixmap->draw_layout(this->table().trickpile_points_gc,
					x, y,
					this->table().trickpile_points_layout);

      if (update)
	this->table().update(Gdk::Rectangle(x, y, layout_width, layout_height));

      DEBUG_RETURNING(VOID,
		      INFO_GTKMM_TRICKPILE && INFO_GTKMM_DRAW,
		      "TrickPile::draw_points(update)");
    } // void TrickPile::draw_points(bool const update = false)

  /**
   **
   ** -> result
   **
   ** @param	-
   **
   ** @return	rotation of the cards in the pile
   ** 
   ** @version	0.6.1
   **
   ** @author	Diether Knof
   **
   **/
  Rotation
    TrickPile::pile_rotation() const
    {
      DEBUG_CALLING(INFO_GTKMM_TRICKPILE && INFO_GTKMM_VALUE,
		    "TrickPile::pile_rotation()");

      Rotation r = UP; // return value

      switch(this->position()) {
      case NORTH:
	r = LEFT;
	break;
      case SOUTH:
	r = RIGHT;
	break;
      case WEST:
	r = UP;
	break;
      case EAST:
	r = DOWN;
	break;
      } // switch(this->position())

      DEBUG_RETURNING(r,
		      INFO_GTKMM_TRICKPILE && INFO_GTKMM_VALUE,
		      "TrickPile::pile_rotation()");
    } // Rotation TrickPile::pile_rotation() const

  /**
   **
   ** -> result
   **
   ** @param	-
   **
   ** @return	starting x position of the trickpile
   ** 
   ** @version	0.6.1
   **
   ** @author	Diether Knof
   **
   **/
  int
    TrickPile::pos_x() const
    {
      DEBUG_CALLING(INFO_GTKMM_TRICKPILE && INFO_GTKMM_VALUE,
		    "TrickPile::pos_x()");

      int value = this->start_x();

      switch(this->position()) {
      case NORTH:
      case EAST:
	value -= (this->width()
		  - this->ui->cards->width(this->pile_rotation()));
	break;
      case SOUTH:
      case WEST:
	break;
      } // switch(position)

      DEBUG_RETURNING(value,
		      INFO_GTKMM_TRICKPILE && INFO_GTKMM_VALUE,
		      "TrickPile::pos_x()");
    } // int TrickPile::pos_x()

  /**
   **
   ** -> result
   **
   ** @param	-
   **
   ** @return	starting y position of the trickpile
   ** 
   ** @version	0.6.1
   **
   ** @author	Diether Knof
   **
   **/
  int
    TrickPile::pos_y() const
    {
      DEBUG_CALLING(INFO_GTKMM_TRICKPILE && INFO_GTKMM_VALUE,
		    "TrickPile::pos_y()");

      int value = this->start_y();

      switch(this->position()) {
      case NORTH:
      case WEST:
	break;
      case SOUTH:
      case EAST:
	value -= (this->height()
		  - this->ui->cards->height(this->pile_rotation()));
	break;
      } // switch(position)

      DEBUG_RETURNING(value,
		      INFO_GTKMM_TRICKPILE && INFO_GTKMM_VALUE,
		      "TrickPile::pos_y()");
    } // int TrickPile::pos_y()

  /**
   **
   ** -> result
   **
   ** @param	-
   **
   ** @return	width of the trickpile
   ** 
   ** @version	0.6.1
   **
   ** @author	Diether Knof
   **
   **/
  int
    TrickPile::width() const
    {
      DEBUG_CALLING(INFO_GTKMM_TRICKPILE && INFO_GTKMM_VALUE,
		    "TrickPile::width()");

      int const value = (this->ui->cards->width(this->pile_rotation())
			 + ((this->player->number_of_tricks() - 1)
			    * std::abs(this->tricks_step_x()))
			 + ((this->player->game().playerno() - 1)
			    * std::abs(this->cards_step_x()))
			);

      DEBUG_RETURNING(value,
		      INFO_GTKMM_TRICKPILE && INFO_GTKMM_VALUE,
		      "TrickPile::width()");
    } // int TrickPile::width()

  /**
   **
   ** -> result
   **
   ** @param	-
   **
   ** @return	height of the trickpile
   ** 
   ** @version	0.6.1
   **
   ** @author	Diether Knof
   **
   **/
  int
    TrickPile::height() const
    {
      DEBUG_CALLING(INFO_GTKMM_TRICKPILE && INFO_GTKMM_VALUE,
		    "TrickPile::height()");

      int const value = (this->ui->cards->height(this->pile_rotation())
			 + ((this->player->number_of_tricks() - 1)
			    * std::abs(this->tricks_step_y()))
			 + ((this->player->game().playerno() - 1)
			    * std::abs(this->cards_step_y()))
			);

      DEBUG_RETURNING(value,
		      INFO_GTKMM_TRICKPILE && INFO_GTKMM_VALUE,
		      "TrickPile::height()");
    } // int TrickPile::height()

  /**
   **
   ** -> result
   **
   ** @param	-
   **
   ** @return	starting x position of the trickpile
   ** 
   ** @version	0.6.1
   **
   ** @author	Diether Knof
   **
   **/
  int
    TrickPile::start_x() const
    {
      DEBUG_CALLING(INFO_GTKMM_TRICKPILE && INFO_GTKMM_VALUE,
		    "TrickPile::start_x()");

      int value = 0; // return value

      switch(this->position()) {
      case NORTH:
	value = (this->hand().start_x()
		 - this->ui->cards->width(this->pile_rotation())
		 - this->hand().margin_x());
	break;
      case SOUTH:
	value = (this->hand().start_x()
		 + this->hand().width()
		 + this->hand().margin_x());
	break;
      case WEST:
	value = this->hand().start_x();
	break;
      case EAST:
	value = (this->hand().start_x()
		 + this->hand().width()
		 - this->ui->cards->width(this->pile_rotation()));
	break;
      } // switch(this->position())

      DEBUG_RETURNING(value,
		      INFO_GTKMM_TRICKPILE && INFO_GTKMM_VALUE,
		      "TrickPile::start_x()");
    } // int TrickPile::start_x()

  /**
   **
   ** -> result
   **
   ** @param	-
   **
   ** @return	starting y position of the trickpile
   ** 
   ** @version	0.6.1
   **
   ** @author	Diether Knof
   **
   **/
  int
    TrickPile::start_y() const
    {
      DEBUG_CALLING(INFO_GTKMM_TRICKPILE && INFO_GTKMM_VALUE,
		    "TrickPile::start_y()");

      int value = 0; // return value

      switch(this->position()) {
      case NORTH:
	value = this->hand().start_y();
	break;
      case SOUTH:
	value = (this->hand().start_y()
		 + this->hand().height()
		 - this->ui->cards->height(this->pile_rotation()));
	break;
      case WEST:
	value = (this->hand().start_y()
		 + this->hand().height()
		 + this->hand().margin_y());
	break;
      case EAST:
	value = (this->hand().start_y()
		 - this->hand().margin_y()
		 - this->ui->cards->height(this->pile_rotation()));
	break;
      } // switch(this->position())

      DEBUG_RETURNING(value,
		      INFO_GTKMM_TRICKPILE && INFO_GTKMM_VALUE,
		      "TrickPile::start_y()");
    } // int TrickPile::start_y()

  /**
   **
   ** -> result
   **
   ** @param	-
   **
   ** @return	x step of the tricks
   ** 
   ** @version	0.6.1
   **
   ** @author	Diether Knof
   **
   **/
  int
    TrickPile::tricks_step_x() const
    {
      DEBUG_CALLING(INFO_GTKMM_TRICKPILE && INFO_GTKMM_VALUE,
		    "TrickPile::tricks_step_x()");

      int value = 0;

      if (!this->player->game().rule()(Rule::SHOW_TRICKS_IN_TRICKPILES)) {
	value = 0;
      } else { // if !(!this->player->game().rule()(Rule::SHOW_TRICKS_IN_TRICKPILES))

	switch(this->position()) {
	case NORTH:
	  value = -TRICKPILE_TRICKS_STEP_BIG_PART;
	  break;
	case SOUTH:
	  value = TRICKPILE_TRICKS_STEP_BIG_PART;
	  break;
	case WEST:
	  value = TRICKPILE_TRICKS_STEP_SMALL_PART;
	  break;
	case EAST:
	  value = -TRICKPILE_TRICKS_STEP_SMALL_PART;
	  break;
	} // switch(this->position())
      } // if !(!this->player->game().rule()(Rule::SHOW_TRICKS_IN_TRICKPILES))

      DEBUG_RETURNING(value,
		      INFO_GTKMM_TRICKPILE && INFO_GTKMM_VALUE,
		      "TrickPile::tricks_step_x()");
    } // int TrickPile::tricks_step_x()

  /**
   **
   ** -> result
   **
   ** @param	-
   **
   ** @return	y step of the tricks
   ** 
   ** @version	0.6.1
   **
   ** @author	Diether Knof
   **
   **/
  int
    TrickPile::tricks_step_y() const
    {
      DEBUG_CALLING(INFO_GTKMM_TRICKPILE && INFO_GTKMM_VALUE,
		    "TrickPile::tricks_step_y()");

      int value = 0;

      if (!this->player->game().rule()(Rule::SHOW_TRICKS_IN_TRICKPILES)) {
	value = 0;
      } else {  // if !(!this->player->game().rule()(Rule::SHOW_TRICKS_IN_TRICKPILES))

	switch(this->position()) {
	case NORTH:
	  value = TRICKPILE_TRICKS_STEP_SMALL_PART;
	  break;
	case SOUTH:
	  value = -TRICKPILE_TRICKS_STEP_SMALL_PART;
	  break;
	case WEST:
	  value = TRICKPILE_TRICKS_STEP_BIG_PART;
	  break;
	case EAST:
	  value = -TRICKPILE_TRICKS_STEP_BIG_PART;
	  break;
	} // switch(this->position())
      } // if !(!this->player->game().rule()(Rule::SHOW_TRICKS_IN_TRICKPILES))

      DEBUG_RETURNING(value,
		      INFO_GTKMM_TRICKPILE && INFO_GTKMM_VALUE,
		      "TrickPile::tricks_step_y()");
    } // int TrickPile::tricks_step_y()

  /**
   **
   ** -> result
   **
   ** @param	-
   **
   ** @return	x step of the cards
   ** 
   ** @version	0.6.1
   **
   ** @author	Diether Knof
   **
   **/
  int
    TrickPile::cards_step_x() const
    {
      DEBUG_CALLING(INFO_GTKMM_TRICKPILE && INFO_GTKMM_VALUE,
		    "TrickPile::cards_step_x()");

      int value = 0;

      switch(this->position()) {
      case NORTH:
	value = -TRICKPILE_CARDS_STEP_BIG_PART;
	break;
      case SOUTH:
	value = TRICKPILE_CARDS_STEP_BIG_PART;
	break;
      case WEST:
	value = TRICKPILE_CARDS_STEP_SMALL_PART;
	break;
      case EAST:
	value = -TRICKPILE_CARDS_STEP_SMALL_PART;
	break;
      } // switch(this->position())

      DEBUG_RETURNING(value,
		      INFO_GTKMM_TRICKPILE && INFO_GTKMM_VALUE,
		      "TrickPile::cards_step_x()");
    } // int TrickPile::cards_step_x()

  /**
   **
   ** -> result
   **
   ** @param	-
   **
   ** @return	y step of the cards
   ** 
   ** @version	0.6.1
   **
   ** @author	Diether Knof
   **
   **/
  int
    TrickPile::cards_step_y() const
    {
      DEBUG_CALLING(INFO_GTKMM_TRICKPILE && INFO_GTKMM_VALUE,
		    "TrickPile::cards_step_y()");

      int value = 0;

      switch(this->position()) {
      case NORTH:
	value = TRICKPILE_CARDS_STEP_SMALL_PART;
	break;
      case SOUTH:
	value = -TRICKPILE_CARDS_STEP_SMALL_PART;
	break;
      case WEST:
	value = TRICKPILE_CARDS_STEP_BIG_PART;
	break;
      case EAST:
	value = -TRICKPILE_CARDS_STEP_BIG_PART;
	break;
      } // switch(this->position())

      DEBUG_RETURNING(value,
		      INFO_GTKMM_TRICKPILE && INFO_GTKMM_VALUE,
		      "TrickPile::cards_step_y()");
    } // int TrickPile::cards_step_y()

} // namespace UI_GTKMM_NS

#endif // #ifdef USE_UI_GTKMM
