/**********************************************************************
 *
 *   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"

#include "gameplay.h"

#include "../party/party.h"
#include "../party/rule.h"
#include "../game/game.h"
#include "../game/gamepointstable.h"

namespace OS_NS {

  /*****************************************************************************
   *
   ** Gameplay::Gameplay(ostream* ostr = &cout)
   *
   ** Parameters:	ostr - output stream
   *
   ** Result:	-
   ** 
   ** Version:	0.4.3
   *
   ** Description:	constructor
   *
   ****************************************************************************/
  Gameplay::Gameplay(ostream* ostr) :
    OS(OS_TYPE::GAMEPLAY),
  ostr(ostr)
  {
    DEBUG_CALLING(INFO_OS_GAMEPLAY,
		  "Gameplay::Gameplay()");

    DEBUG_RETURNING(VOID,
		    INFO_OS_GAMEPLAY,
		    "Gameplay::Gameplay()");
  } // Gameplay::Gameplay()

  /*****************************************************************************
   *
   ** Gameplay::~Gameplay()
   *
   ** Parameters:	-
   *
   ** Result:	-
   ** 
   ** Version:	0.4.3
   *
   ** Description:	Destructor
   *
   ****************************************************************************/
  Gameplay::~Gameplay()
  {
    DEBUG_CALLING(INFO_OS_GAMEPLAY,
		  "Gameplay::~Gameplay()");

    DEBUG_RETURNING(VOID,
		    INFO_OS_GAMEPLAY,
		    "Gameplay::~Gameplay()");
  } // Gameplay::~Gameplay()


  /*****************************************************************************
   *
   ** void Gameplay::party_open()
   *
   ** Parameters:	-
   *
   ** Result:	-
   ** 
   ** Version:	0.4.3
   *
   ** Description:	call the function of 'gui'
   *
   ****************************************************************************/
  void
    Gameplay::party_open()
    {
      DEBUG_CALLING(INFO_OS_GAMEPLAY,
		    "Gameplay::party_open()");

      *(this->ostr) << "party open\n";

      DEBUG_RETURNING(VOID,
		      INFO_OS_GAMEPLAY,
		      "Gameplay::party_open()");
    } // void Gameplay::party_open()


  /*****************************************************************************
   *
   ** void Gameplay::party_start()
   *
   ** Parameters:	-
   *
   ** Result:	-
   ** 
   ** Version:	0.4.3
   *
   ** Description:	call the function of 'gui'
   *
   ****************************************************************************/
  void
    Gameplay::party_start()
    { 
      DEBUG_CALLING(INFO_OS_GAMEPLAY,
		    "Gameplay::party_start()");

      *(this->ostr) << "party start\n";
      *(this->ostr) << ::party.rule();

      DEBUG_RETURNING(VOID,
		      INFO_OS_GAMEPLAY,
		      "Gameplay::party_start()");
    } // void Gameplay::party_start()

  /*****************************************************************************
   *
   ** void Gameplay::party_finish()
   *
   ** Parameters:	-
   *
   ** Result:	-
   ** 
   ** Version:	0.4.3
   *
   ** Description:	call the function of 'gui'
   *
   ****************************************************************************/
  void
    Gameplay::party_finish()
    {
      DEBUG_CALLING(INFO_OS_GAMEPLAY,
		    "Gameplay::party_finish()");

      *(this->ostr) << "party finish\n";

      DEBUG_RETURNING(VOID,
		      INFO_OS_GAMEPLAY,
		      "Gameplay::party_finish()");
    } // void Gameplay::party_end()

  /*****************************************************************************
   *
   ** void Gameplay::party_close()
   *
   ** Parameters:	-
   *
   ** Result:	-
   ** 
   ** Version:	0.4.3
   *
   ** Description:	call the function of 'gui'
   *
   ****************************************************************************/
  void
    Gameplay::party_close()
    {
      DEBUG_CALLING(INFO_OS_GAMEPLAY,
		    "Gameplay::party_close()");

      *(this->ostr) << "party close\n";

      DEBUG_RETURNING(VOID,
		      INFO_OS_GAMEPLAY,
		      "Gameplay::party_close()");
    } // void Gameplay::party_close()

  /*****************************************************************************
   *
   ** void Gameplay::game_open()
   *
   ** Parameters:	-
   *
   ** Result:	-
   ** 
   ** Version:	0.4.3
   *
   ** Description:	open the game
   *
   ****************************************************************************/
  void
    Gameplay::game_open()
    {
      DEBUG_CALLING(INFO_OS_GAMEPLAY,
		    "Gameplay::game_open()");

      *(this->ostr) << "game open: " << ::party.gameno() << "\n";
      *(this->ostr) << "seed: " << ::party.game().seed() << "\n";

      DEBUG_RETURNING(VOID,
		      INFO_OS_GAMEPLAY,
		      "Gameplay::game_open()");
    } // void Gameplay::game_open()

  /*****************************************************************************
   *
   ** void Gameplay::game_start()
   *
   ** Parameters:	-
   *
   ** Result:	-
   ** 
   ** Version:	0.4.3
   *
   ** Description:	The game is started.
   *		The announcements of the other players are shown.
   *
   ****************************************************************************/
  void
    Gameplay::game_start()
    {
      DEBUG_CALLING(INFO_OS_GAMEPLAY,
		    "Gameplay::game_start()");

      *(this->ostr) << "game start: " << ::party.game().type() << "\n";

      if (is_solo(::party.game().type()))
	*(this->ostr) << "Soloplayer: "
	  << ::party.game().soloplayer().no()
	  << "\n";

      DEBUG_RETURNING(VOID,
		      INFO_OS_GAMEPLAY,
		      "Gameplay::game_start()");
    } // void Gameplay::game_start()


  /*****************************************************************************
   *
   ** void Gameplay::game_finish()
   *
   ** Parameters:	-
   *
   ** Result:	-
   ** 
   ** Version:	0.4.3
   *
   ** Description:	The game is ended.
   *		The winner and the points of the game are shown.
   *
   ****************************************************************************/
  void
    Gameplay::game_finish()
    { 
      DEBUG_CALLING(INFO_OS_GAMEPLAY,
		    "Gameplay::game_finish()");

      *(this->ostr) << "game finish\n";

      GamePoints const& game_points
	= (static_cast<Party const&>(::party)).gamepointstable().gamepoints(::party.gameno());

      *(this->ostr) << "\ngamepoints:\n" << game_points;

      if (game_points.pointlist().size() > 0) {
	*(this->ostr) << "specialpoints:\n";
	*(this->ostr) << "team\tfor\tvalue\tdescription:\n";
	for (Specialpointsvector::const_iterator
	     special_point = game_points.pointlist().begin();
	     special_point != game_points.pointlist().end();
	     special_point++) {
	  *(this->ostr) << special_point->team
	    << "\t" << name(special_point->counting_team);
	  // extra rule
	  if ((special_point->type == SPECIALPOINT::NO120_SAID)
	      && (::party.rule()(Rule::ANNOUNCEMENT_RE_DOUBLES)))
	    *(this->ostr) << "\t" << "* 2";
	  else
	    *(this->ostr) << "\t"
	      << ((special_point->team == game_points.winnerteam())
		  ? "+ "
		  : "- ")
	      << Value_of_Specialpoint(special_point->type);
	  *(this->ostr) << "\t" << name(special_point->type)
	    << "\n";
	} // for (special_point)
	*(this->ostr) << "\t\t= " << game_points.points() << "\n";
      } // if (game_points.pointlist().size() > 0)

      DEBUG_RETURNING(VOID,
		      INFO_OS_GAMEPLAY,
		      "Gameplay::game_finish()");
    } // void Gameplay::game_finish()

  /*****************************************************************************
   *
   ** void Gameplay::game_close()
   *
   ** Parameters:	-
   *
   ** Result:	-
   ** 
   ** Version:	0.4.3
   *
   ** Description:	Removes the entry 'rules' of the menu.
   *
   ****************************************************************************/
  void
    Gameplay::game_close()
    {
      DEBUG_CALLING(INFO_OS_GAMEPLAY,
		    "Gameplay::game_close()");

      *(this->ostr) << "game close\n";

      DEBUG_RETURNING(VOID,
		      INFO_OS_GAMEPLAY,
		      "Gameplay::game_close()");
    } // void Gameplay::game_close()


  /*****************************************************************************
   *
   ** void Gameplay::card_played(Card const& card, Player const& player)
   *
   ** Parameters:	card   - the played card
   *		player - the player, who has played a card
   *
   ** Result:	-
   ** 
   ** Version:	0.4.3
   *
   ** Description:	The player 'player' has played the card 'card'.
   *		Remove it from the hand.
   *		(later: animation?)
   *
   ****************************************************************************/
  void
    Gameplay::card_played(Card const& card, Player const& player)
    { 
      DEBUG_CALLING(INFO_OS_GAMEPLAY,
		    "Gameplay::card_played(card, player)");

      *(this->ostr) << ::party.game().trick_current().actcardno() << ". "
	<< player.name() << ": "
	<< card << "\n";

      DEBUG_RETURNING(VOID,
		      INFO_OS_GAMEPLAY,
		      "Gameplay::card_played(card, player)");
    } // void Gameplay::card_played(card, player)

  /**
   **
   ** the announcement 'announcement' has been made by player 'player'
   **
   ** @param	announcement	the announcement
   ** @param	player		the player, who has made the announcement
   **
   ** @return	-
   **
   ** @version	0.4.5
   **
   ** @author	Diether Knof
   **
   **/
  void
    Gameplay::announcement_made(Announcement const& announcement,
				Player const& player)
    {
      DEBUG_CALLING(INFO_OS_GAMEPLAY,
		    "Gameplay::announcement_made(announcement, player)");

      *(this->ostr) << "announcement: "
	<< "player " << player.no() << ": "
	<< announcement
	<< "\n";

      DEBUG_RETURNING(VOID,
		      INFO_OS_GAMEPLAY,
		      "Gameplay::announcement_made(announcement, player)");
    } // void Gameplay::announcement_made(Announcement const& announcement, Player const& player)

  /**
   **
   ** the player has swines
   **
   ** @param	player		the player with the swines
   **
   ** @return	-
   **
   ** @version	0.5.4
   **
   ** @author	Diether Knof
   **
   **/
  void
    Gameplay::swines_announced(Player const& player)
    {
      DEBUG_CALLING(INFO_OS_GAMEPLAY,
		    "Gameplay::swines_announced(player)");

      *(this->ostr) << "swines announced: "
	<< "player " << player.no()
	<< "\n";

      DEBUG_RETURNING(VOID,
		      INFO_OS_GAMEPLAY,
		      "Gameplay::swines_announced(player)");
    } // void Gameplay::swines_announced(Player const& player)

  /**
   **
   ** the player has hyperswines
   **
   ** @param	player		the player with the swines
   **
   ** @return	-
   **
   ** @version	0.5.4
   **
   ** @author	Diether Knof
   **
   **/
  void
    Gameplay::hyperswines_announced(Player const& player)
    {
      DEBUG_CALLING(INFO_OS_GAMEPLAY,
		    "Gameplay::hyperswines_announced(player)");

      *(this->ostr) << "hyperswines announced: "
	<< "player " << player.no()
	<< "\n";

      DEBUG_RETURNING(VOID,
		      INFO_OS_GAMEPLAY,
		      "Gameplay::hyperswines_announced(player)");
    } // void Gameplay::hyperswines_announced(Player const& player)

  /**
   **
   ** 'player' shifts 'cardno' cards
   **
   ** @param	player	the player who shifts the cards
   ** @param	cardno	the number of cards that are shifted
   **
   ** @return	-
   **
   ** @version	0.4.5
   **
   ** @author	Diether Knof
   **
   **/
  void
    Gameplay::poverty_shift(Player const& player, unsigned const cardno)
    { 
      DEBUG_CALLING(INFO_OS_GAMEPLAY,
		    "Gameplay::poverty_shift(player, cardno)");

      *(this->ostr) << "poverty: "
	<< "player " << player.no()
	<< " shifts " << cardno << " cards.\n";

      DEBUG_RETURNING(VOID,
		      INFO_OS_GAMEPLAY,
		      "Gameplay::poverty_shift(player, cardno)");
    } // void Gameplay::poverty_shift(Player const& player, unsigned const cardno)

  /**
   **
   ** all players have denied to take the cards
   **
   ** @param	-
   **
   ** @return	-
   **
   ** @version	0.4.5
   **
   ** @author	Diether Knof
   **
   **/
  void
    Gameplay::poverty_take_denied_by_all()
    { 
      DEBUG_CALLING(INFO_OS_GAMEPLAY,
		    "Gameplay::poverty_take_denied_by_all()");

      *(this->ostr) << "poverty: "
	<< "denied by all players\n";

      DEBUG_RETURNING(VOID,
		      INFO_OS_GAMEPLAY,
		      "Gameplay::poverty_take_denied_by_all()");
    } // void Gameplay::poverty_take_denied_by_all()

  /**
   **
   ** 'player' accepts to take the shifted cards
   ** and returns 'cardno' cards with 'trumpno' trumps
   **
   ** @param	player	the player who has denied to take the cards
   ** @param	cardno	number of cards that are given back
   ** @param	trumpno	number of trumps of the cards
   **
   ** @return	-
   **
   ** @version	0.4.5
   **
   ** @author	Diether Knof
   **
   **/
  void
    Gameplay::poverty_take_accepted(Player const& player,
				    unsigned const cardno,
				    unsigned const trumpno)
    { 
      DEBUG_CALLING(INFO_OS_GAMEPLAY,
		    "Gameplay::poverty_take_accepted(player, cardno, trumpno)");

      *(this->ostr) << "poverty: "
	<< "player " << player.no() << " accepts poverty and returns "
	<< cardno << " cards with " << trumpno << " trumps.\n";

      DEBUG_RETURNING(VOID,
		      INFO_OS_GAMEPLAY,
		      "Gameplay::poverty_take_accepted(player, cardno, trumpno)");
    } // void Gameplay::poverty_take_accepted(Player const& player, unsigned const cardno, unsigned const trumpno)

/**
 **
 ** marriage: Information of the new team
 **
 ** @param	bridegroom	the player with the marriage
 ** @param	bride		the bride
 **
 ** @return	-
 **
 ** @author	Diether Knof
 **
 ** @version	0.6.1
 **
 **/
  void
    Gameplay::marriage(Player const& bridegroom,
		       Player const& bride)
    {
      DEBUG_CALLING(INFO_OS_GAMEPLAY,
		    "Gameplay::marriage(bridegroom, bride)");

      *(this->ostr) << "Marriage: "
	<< "player " << bridegroom.no()
	<< " with player " << bride.no()
	<< '\n';

      DEBUG_RETURNING(VOID,
		      INFO_OS_GAMEPLAY,
		      "Gameplay::marriage(bridegroom, bride)");
    } // void Gameplay::marriage(Player const& bridegroom, Player const& bride)

  /**
   **
   ** genscher: Information of the new team
   **
   ** @param	player	genscher player
   ** @param	partner	partner of the gensher player
   **
   ** @return	-
   **
   ** @author	Diether Knof
   **
   ** @version	0.5.4
   **
   **/
  void
    Gameplay::genscher(Player const& player,
		       Player const& partner)
    {
      DEBUG_CALLING(INFO_OS_GAMEPLAY,
		    "Gameplay::genscher(player, partner)");

      *(this->ostr) << "Genscher: "
	<< "player " << player.no()
	<< " with player " << partner.no()
	<< '\n';

      DEBUG_RETURNING(VOID,
		      INFO_OS_GAMEPLAY,
		      "Gameplay::genscher(player, partner)");
    } // void Gameplay::genscher(Player const& player, Player const& partner)


  /*****************************************************************************
   *
   ** void Gameplay::trick_open()
   *
   ** Parameters:	-
   *
   ** Result:	-
   ** 
   ** Version:	0.4.3
   *
   ** Description:	The trick is opened
   *
   ****************************************************************************/
  void
    Gameplay::trick_open()
    {
      DEBUG_CALLING(INFO_OS_GAMEPLAY,
		    "Gameplay::trick_open()");

      *(this->ostr) << "\n"
	<< "Trick: " << ::party.game().trick_current_no() << "\n";

      DEBUG_RETURNING(VOID,
		      INFO_OS_GAMEPLAY,
		      "Gameplay::trick_open()");
    } // void Gameplay::trick_open()

  /*****************************************************************************
   *
   ** void Gameplay::trick_full(Trick const& trick)
   *
   ** Parameters:	trick - the trick
   *
   ** Result:	-
   ** 
   ** Version:	0.4.3
   *
   ** Description:	The trick is full.
   *		Wait for a mouse click and afterwards update the last trick.
   *
   ****************************************************************************/
  void
    Gameplay::trick_full(Trick const& trick)
    {
      DEBUG_CALLING(INFO_OS_GAMEPLAY,
		    "Gameplay::trick_full(trick)");

      *(this->ostr) << "Winner: "
	<< trick.winnerplayer().name()
	<< "\n";

      DEBUG_RETURNING(VOID,
		      INFO_OS_GAMEPLAY,
		      "Gameplay::trick_full(trick)");
    } // void Gameplay::trick_full(trick)

} // namespace OS_NS
