/***************************************************************************
 *   Copyright (C) 2005 by Tavarez Arnaud Bakoula                          *
 *   tbakoula@yahoo.fr                                                     *
 *                                                                         *
 *   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 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.             *
 ***************************************************************************/
#include "notification.h"

/*!
 *  \class Notification 
 *  \brief The Notification class stores the pieces that we submit.
 *
 *  It stores marks with specified states and in particular places.
 *  The stored elements are abstract representation of a piece. They are
 *  managed with a std::vector, pMarkList.
 */

/*!
 *  \brief Default constructor
 *
 *  It constructs a empty notification.
 */
Notification::Notification()
{
    dim   = 0;
    count = 0;
    pMarksList = 0;       // initialisation du tableau de pMarksList Ã  NULL
}

/*!
 *  Constructs a notification that contains \a nb elements which are pieces
 *  of type Mark.
 */
Notification::Notification( uint nb )
    : dim( nb )
{
    count = 0;  count = new int[3];
    pMarksList = new std::vector<Mark>();
    for ( uint i=0; i < dim; i++ )
      pMarksList->push_back( Mark(i) );
}

/*!
 *  \brief Copy constructor.
 *
 *  Creates a notification from another.
 */
Notification::Notification( const Notification& ntf )
{
    if ( ntf.dim )
    {
      dim = ntf.dim;
      pMarksList = 0;  count = 0;
      pMarksList = new std::vector<Mark>();
      count = new int[3];

      std::vector<Mark>::iterator it = ntf.pMarksList->begin();
      for ( ; it != ntf.pMarksList->end(); ++it )
      {
        pMarksList->push_back( (*it) );
      }
    }
    else
    {
      dim = 0; count = 0;
      pMarksList = 0;
    }
}

/*!
 *  \brief Destructor.
 *
 *  Destructs the notification and restores the allocated ressources
 */
Notification::~Notification()
{
    if ( pMarksList )  delete pMarksList;
    pMarksList = 0;
    if ( count )       delete [] count;
    count = 0;
}

/*!
 *  Affectation operator.
 */
Notification&
Notification::operator=( const Notification& ntf )
{
    if ( this != &ntf )
    {
      dim = ntf.dim;
      delete pMarksList;  pMarksList = 0;
      delete [] count;    count = 0;
      pMarksList = new std::vector<Mark>();
      count = new int[3];

      std::vector<Mark>::iterator it = ntf.pMarksList->begin();
      for ( ; it != ntf.pMarksList->end(); ++it )
      {
        pMarksList->push_back( (*it) );
      }
    }
    return *this;
}

/*!
 *  This function determines the number of occurences of each state
 *  in the notification.
 */
void
Notification::doCount()
{
    /* Comptage des pPionsList d'une mÃªme couleur */
    if ( pMarksList==0 )
      return;

    std::vector<Mark>::iterator it = pMarksList->begin();
    for ( ; it != pMarksList->end(); ++it )
      count[ static_cast<int>( (*it).getState() ) ]++;
}

/*!
 *  Resets the contents of the state occurences array.
 */
void
Notification::resetCount()
{
    if ( count==0 )
      return;

    for ( int i=0; i < 3; i++ )
      count[i] = 0;
}

/*!
 *  Adds \a nb pieces to the notification.
 */
void
Notification::addMarks( uint nb )
{
    if ( pMarksList )
    {
      for ( uint i=0; i < nb; i++ )
        pMarksList->push_back( Mark( dim + i ) );
      dim += nb;
    }
}

/*!
 *  Removes \a nb pieces from the notification.
 */
void
Notification::removeMarks( uint nb )
{
    if ( pMarksList && !pMarksList->empty() )
    {
      for ( uint i=0; i < nb; i++ )
        pMarksList->pop_back();
      dim -= nb;
    }
}

/*!
 *  Resets the content of the notification. So the state of all the
 *  Mark objects are put to the value State::MISPLACED.
 */
void
Notification::reset()
{
    if ( pMarksList==0 )
      return;

    std::vector<Mark>::iterator it = pMarksList->begin();
    for ( ; it != pMarksList->end(); it++ )
      (*it).setState( MISPLACED );
    resetCount();
}
