/**********************************************************************
 *
 *   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 "gameplay.h"

#include "../card/trick.h"
#include "../card/hand.h"
#include "../player/player.h"

/**
 **
 ** constructor
 **
 ** @param	type		action type
 ** @param	playerno	player who has made the action
 ** @param	hand		hand of the action
 **
 ** @return	-
 ** 
 ** @version	0.6.8
 **
 ** @author	Diether Knof
 **
 **/
Gameplay::Action::Action(Type const type,
			 unsigned const playerno,
			 Hand const& hand) :
  type(type),
  player(playerno),
  data()
{
  DEBUG_ASSERTION(((type == POVERTY_SHIFT)
		  || (type == POVERTY_RETURNED)),
		  "Gameplay::Action::Action(type, playerno, hand):\n"
		  "  type = '" << type << "' is neither 'POVERTY_SHIFT' nor 'POVERTY_RETURNED'");

  this->data.cards = new vector<Card>(hand.cards());
} // Action::Action(Type type, unsigned playerno, Hand hand)

/**
 **
 ** constructor
 **
 ** @param	type		action type
 ** @param	playerno	player who has made the action
 ** @param	announcement	announcement made by 'playerno'
 **
 ** @return	-
 ** 
 ** @version	0.6.8
 **
 ** @author	Diether Knof
 **
 **/
Gameplay::Action::Action(Type const type,
			 unsigned const playerno,
			 Announcement const announcement) :
  type(type),
  player(playerno),
  data()
{
  DEBUG_ASSERTION((type == ANNOUNCEMENT),
		  "Gameplay::Action::Action(type, playerno, announcement):\n"
		  "  type = '" << type << "' is not 'ANNOUNCEMENT'");

  this->data.announcement = announcement;
} // Action::Action(Type type, unsigned playerno, Announcement announcement)

/**
 **
 ** constructor
 **
 ** @param	type		action type
 ** @param	playerno	player who has made the action
 ** @param	card		played card
 **
 ** @return	-
 ** 
 ** @version	0.6.8
 **
 ** @author	Diether Knof
 **
 **/
Gameplay::Action::Action(Type const type,
			 unsigned const playerno,
			 Card const& card) :
  type(type),
  player(playerno),
  data()
{
  DEBUG_ASSERTION((type == CARD_PLAYED),
		  "Gameplay::Action::Action(type, playerno, announcement):\n"
		  "  type = '" << type << "' is not 'CARD_PLAYED'");

  this->data.card = new Card(card);
} // Action::Action(Type type, unsigned playerno, Card card)

/**
 **
 ** constructor
 **
 ** @param	type		action type
 ** @param	playerno	player who has made the action
 **
 ** @return	-
 ** 
 ** @version	0.6.8
 **
 ** @author	Diether Knof
 **
 **/
Gameplay::Action::Action(Type const type,
			 unsigned const playerno) :
  type(type),
  player(playerno),
  data()
{
  DEBUG_ASSERTION(((type == SWINES)
		   || (type == HYPERSWINES)),
		  "Gameplay::Action::Action(type, playerno):\n"
		  "  type = '" << type << "' is neither 'SWINES' nor 'HYPERSWINES'");
} // Action::Action(Type type, unsigned playerno)

/**
 **
 ** constructor
 **
 ** @param	type		action type
 ** @param	trick		closed trick
 **
 ** @return	-
 ** 
 ** @version	0.6.8
 **
 ** @author	Diether Knof
 **
 ** @todo	save the trick
 **
 **/
Gameplay::Action::Action(Type const type,
			 Trick const& trick) :
  type(type),
  player(UINT_MAX),
  data()
{
  DEBUG_ASSERTION((type == TRICK_CLOSED),
		  "Gameplay::Action::Action(type, playerno, announcement):\n"
		  "  type = '" << type << "' is not 'TRICK_CLOSED'");

} // Action::Action(Type type, Trick trick)

/**
 **
 ** constructor
 **
 ** @param	type		action type
 ** @param	playerno	player who has made the action
 ** @param	partner		partner player
 **
 ** @return	-
 ** 
 ** @version	0.6.8
 **
 ** @author	Diether Knof
 **
 **/
Gameplay::Action::Action(Type const type,
			 unsigned const playerno,
			 unsigned const partner) :
  type(type),
  player(playerno),
  data()
{
  DEBUG_ASSERTION(((type == MARRIAGE)
		   || (type == GENSCHER)),
		  "Gameplay::Action::Action(type, playerno, partner):\n"
		  "  type = '" << type << "' is neither 'MARRIAGE' nor 'GENSCHER'");

  this->data.partner = partner;
} // Action::Action(Type type, unsigned playerno, unsigned partner)

/**
 **
 ** destructor
 **
 ** @param	-
 **
 ** @return	-
 ** 
 ** @version	0.6.8
 **
 ** @author	Diether Knof
 **
 **/
Gameplay::Action::~Action()
{
  switch(this->type) {
  case POVERTY_SHIFT:
  case POVERTY_RETURNED:
    delete this->data.cards;
    break;
  case ANNOUNCEMENT:
    break;
  case SWINES:
  case HYPERSWINES:
    break;
  case CARD_PLAYED:
    delete this->data.card;
    break;
  case TRICK_CLOSED:
    break;
  case MARRIAGE:
  case GENSCHER:
    break;
  } // switch(type)

  return ;
} // Action(Type type, unsigned playerno, Hand hand)

/**
 **
 ** copy constructor
 **
 ** @param	action	action to copy
 **
 ** @return	-
 ** 
 ** @version	0.6.8
 **
 ** @author	Diether Knof
 **
 **/
Gameplay::Action::Action(Action const& action) :
  type(action.type),
  player(action.player),
  data()
{
  switch(this->type) {
  case POVERTY_SHIFT:
  case POVERTY_RETURNED:
    this->data.cards = new vector<Card>(*action.data.cards);
    break;
  case ANNOUNCEMENT:
    this->data.announcement = action.data.announcement;
    break;
  case SWINES:
  case HYPERSWINES:
    break;
  case CARD_PLAYED:
    this->data.card = new Card(*action.data.card);
    break;
  case TRICK_CLOSED:
    break;
  case MARRIAGE:
  case GENSCHER:
    this->data.partner = action.data.partner;
    break;
  } // switch(type)

  return ;
} // Gameplay::Action::Action(Action action)

/**
 **
 ** copy constructor
 **
 ** @param	action	action to copy
 **
 ** @return	-
 ** 
 ** @version	0.6.8
 **
 ** @author	Diether Knof
 **
 **/
Gameplay::Action::Action&
Gameplay::Action::operator=(Action const& action)
{
  this->type = action.type;
  this->player = action.player;

  switch(this->type) {
  case POVERTY_SHIFT:
  case POVERTY_RETURNED:
    this->data.cards = new vector<Card>(*action.data.cards);
    break;
  case ANNOUNCEMENT:
    this->data.announcement = action.data.announcement;
    break;
  case SWINES:
  case HYPERSWINES:
    break;
  case CARD_PLAYED:
    this->data.card = new Card(*action.data.card);
    break;
  case TRICK_CLOSED:
    break;
  case MARRIAGE:
  case GENSCHER:
    this->data.partner = action.data.partner;
    break;
  } // switch(type)

  return *this;
} // Action& operator=(Action action)

/**
 **
 ** writes the action in the output stream
 **
 ** @param	ostr	output stream
 **
 ** @return	output stream
 ** 
 ** @version	0.6.8
 **
 ** @author	Diether Knof
 **
 **/
ostream&
Gameplay::Action::write(ostream& ostr) const
{
  ostr << this->type;

  switch(this->type) {
  case POVERTY_SHIFT:
  case POVERTY_RETURNED:
    ostr << " = " << this->player;
    for (vector<Card>::const_iterator c = this->data.cards->begin();
	 c != this->data.cards->begin();
	 ++c)
      ostr << " " << *c;
    break;
  case ANNOUNCEMENT:
    ostr << " = " << this->player << " " << this->data.announcement;
    break;
  case SWINES:
  case HYPERSWINES:
    ostr << " = " << this->player;
    break;
  case CARD_PLAYED:
    ostr << " = " << this->player << " " << *this->data.card;
    break;
  case TRICK_CLOSED:
    break;
  case MARRIAGE:
  case GENSCHER:
    ostr << " = " << this->player << " " << this->data.partner;
    break;
  } // switch(type)

  return ostr;
} // ostream& Gameplay::Action::write(ostream& ostr)

/**
 **
 ** -> result
 **
 ** @param	type	action tye
 **
 ** @return	name of the action type
 ** 
 ** @version	0.6.8
 **
 ** @author	Diether Knof
 **
 **/
string
name(Gameplay::Action::Type const type)
{
  switch(type) {
  case Gameplay::Action::POVERTY_SHIFT:
    return "poverty shift";
  case Gameplay::Action::POVERTY_RETURNED:
    return "poverty returned";
  case Gameplay::Action::ANNOUNCEMENT:
    return "announcement";
  case Gameplay::Action::SWINES:
    return "swines";
  case Gameplay::Action::HYPERSWINES:
    return "hyperswines";
  case Gameplay::Action::CARD_PLAYED:
    return "card played";
  case Gameplay::Action::TRICK_CLOSED:
    return "trick closed";
  case Gameplay::Action::MARRIAGE:
    return "marriage";
  case Gameplay::Action::GENSCHER:
    return "genscher";
  } // switch(type)

  DEBUG_ASSERTION(false,
		  "name(Gameplay::Action::Type type):\n"
		  "  unknown type '" << static_cast<int>(type) << "'");

  return "";
} // string name(Gameplay::Action::Type const type)

/**
 **
 ** writes the type in the output stream
 **
 ** @param	ostr	output stream
 ** @param	type	action tye
 **
 ** @return	output stream
 ** 
 ** @version	0.6.8
 **
 ** @author	Diether Knof
 **
 **/
ostream&
operator<<(ostream& ostr, Gameplay::Action::Type const type)
{
  ostr << ::name(type);
  return ostr;
} // ostream& operator<<(ostream& ostr, Gameplay::Action::Type type)

/**
 **
 ** writes the action in the output stream
 **
 ** @param	ostr	output stream
 ** @param	action	action
 **
 ** @return	output stream
 ** 
 ** @version	0.6.8
 **
 ** @author	Diether Knof
 **
 **/
ostream&
operator<<(ostream& ostr, Gameplay::Action const& action)
{
  action.write(ostr);

  return ostr;
} // ostream& operator<<(ostream& ostr, Gameplay::Action action)
