/*******************************************************************************
*                                                                              *
* Société de Transports de Montréal.                                    	   *
* 2012                                                                         *
*                                                                              *
* Projet Zones Tests                                                           *
*                                                                              *
*                                                                              *
*                                                                              *
*******************************************************************************/
/*
	Description:
	Objet CDV d'Aiguillage.  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 "SwitchCDV.h"

//TEST

CSwitchCDV::CSwitchCDV()
{
	mCDVType = CDV_SWITCH_TYPE;
	mCDVInputMask = 0;
	mCDVITIMask = 0;
	mNormalItiMask = 0;
	mReversedItiMask = 0;
	mIsOccupied = false;
	mIsItiCommanded = false;
	mCDVState = CDV_STATE_FREE;
	mCurSwitchPosition = SWITCH_CDV_NORMAL_POSITION;

}

CSwitchCDV::CSwitchCDV(unsigned int CDVNormalITIMask, unsigned int CDVReversedITIMask, unsigned int CDVInputMask, unsigned int CDVType, QString CDVLabel, unsigned int CDVWay, unsigned int CDVGraphicalPosition)
	:CCDV(0,CDVInputMask,CDVType,CDVLabel,CDVWay,CDVGraphicalPosition)
{
	mNormalItiMask = CDVNormalITIMask;
	mReversedItiMask = CDVReversedITIMask;
	mCDVType = CDV_SWITCH_TYPE;
	mCurSwitchPosition = SWITCH_CDV_NORMAL_POSITION;
}

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

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

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


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

void CSwitchCDV::SetNormalItiMask(unsigned int Mask)
{
	mNormalItiMask = Mask;
}

void CSwitchCDV::SetReverseItiMask(unsigned int Mask)
{
	mReversedItiMask = Mask;
}
