/*******************************************************************************
*                                                                              *
* Société de Transports de Montréal.                                    	   *
* 2012                                                                         *
*                                                                              *
* Projet Zones Tests                                                           *
*                                                                              *
*                                                                              *
*                                                                              *
*******************************************************************************/
/*
	Description:
	Objet CDV.  Contient différents paramètres d'un CDV ainsi que son état actuel.
	Utilisé pour l'interface graphique mais aussi par le state machine pour qui l'état
	de certains CDV est utile.
	
*/

/* ************************************************************************** */
/* Revision:
### YYYMMDD JFM
	Verision d'origine.
	
### YYYYMMDD Description du besoin ou du bug
	Description du changement.
 */

/* ************************************************************************** */

#include "CDV.h"

//TEST

CCDV::CCDV()
{
	mCDVType = CDV_NORMAL_TYPE;
	mCDVInputMask = 0;
	mCDVITIMask = 0;
	mIsOccupied = false;
	mIsItiCommanded = false;
	mCDVState = CDV_STATE_FREE;
	mCDVGraphicalPosition = 0;
	mCDVWay = 1;
    mCDVIsDeck = false;
}

CCDV::CCDV(unsigned int CDVITIMask, unsigned int CDVInputMask, unsigned int CDVType, QString CDVLabel, unsigned int CDVWay, unsigned int CDVGraphicalPosition)
{
	mCDVType = CDVType;
	mCDVInputMask = CDVInputMask;
	mCDVITIMask = CDVITIMask;
	mCDVGraphicalPosition = CDVGraphicalPosition;
	mCDVWay = CDVWay;
	mCDVLabel = CDVLabel;

	mIsOccupied = false;
	mIsItiCommanded = false;
	mCDVState = CDV_STATE_FREE;
    mCDVIsDeck = false;
}

CCDV::~CCDV()
{

}

unsigned int CCDV::SetInputMask(unsigned int Mask)
{
	mCDVInputMask = Mask;

	return RET_OK;
}

unsigned int CCDV::SetLabel(QString Label)
{
	mCDVLabel = Label;
	return RET_OK;
}

unsigned int CCDV::ComputeCDVState(unsigned int InputsBuf, unsigned int ZT1ItiMask, unsigned int ZT2ItiMask)
{
	mIsOccupied = false;
	mIsItiCommanded = false;
	mCDVState = CDV_STATE_FREE;

	if( (((ZT1ItiMask & InputsBuf) != 0) && ((ZT1ItiMask & mCDVITIMask) != 0)) ||	//If there is a ZT1 itinerary commanded AND this CDV is part of itinerary
		(((ZT2ItiMask & InputsBuf) != 0) && ((ZT2ItiMask & mCDVITIMask) != 0)) )	//OR if there is a ZT2 itinerary commanded AND this CDV is part of itinerary
	{
		mIsItiCommanded = true;
		mCDVState = CDV_STATE_ITI_CMD;
	}

	if((mCDVInputMask & InputsBuf) == 0)	//Check if CDV is occupied
	{
		mIsOccupied = true;
		mCDVState = CDV_STATE_OCCUPIED;
	}
	return RET_OK;
}

void CCDV::SetCDVDeck(bool IsDeck)
{
    mCDVIsDeck = IsDeck;
}
