/*******************************************************************************
*                                                                              *
* Société de Transports de Montréal.                                    	   *
* 2012 - 2013                                                                        *
*                                                                              *
* Projet Zones Tests                                                           *
*                                                                              *
*                                                                              *
*                                                                              *
*******************************************************************************/
/*
	Description:
	Cette classe gère la liste des fonctions désactivées.  Pour chaque fonction
	de détection désactivée, un objet "CEvent" est créé et ajouté à la liste.
	Cette classe est utilisée par l'interface graphique est la machine à états
	d'analyse d'erreurs afin de savoir quels déclenchements ignorer.
	
*/

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

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

#include "EventMgr.h"
#include "ZTPage.h"



CEventMgr::CEventMgr()
{

}

unsigned int CEventMgr::UpdateEvents(CZTDetectionFunctionConfig *DetectionFuncCfg)
{

	if(!DetectionFuncCfg->mZTDetectionConfig[DETECTION_FCT_FN].AnalysisActive ||
	   !DetectionFuncCfg->mZTDetectionConfig[DETECTION_FCT_FN].TKActive)
		AddEvent(EVENT_FN_HS);
	else
		RemoveEvent(EVENT_FN_HS);

	if(!DetectionFuncCfg->mZTDetectionConfig[DETECTION_FCT_PG].AnalysisActive ||
	   !DetectionFuncCfg->mZTDetectionConfig[DETECTION_FCT_PG].TKActive)
		AddEvent(EVENT_PG_HS);
	else
		RemoveEvent(EVENT_PG_HS);

	if(!DetectionFuncCfg->mZTDetectionConfig[DETECTION_FCT_PP].AnalysisActive ||
	   !DetectionFuncCfg->mZTDetectionConfig[DETECTION_FCT_PP].TKActive)
		AddEvent(EVENT_PP_HS);
	else
		RemoveEvent(EVENT_PP_HS);

	if(!DetectionFuncCfg->mZTDetectionConfig[DETECTION_FCT_PP2].AnalysisActive ||
	   !DetectionFuncCfg->mZTDetectionConfig[DETECTION_FCT_PP2].TKActive)
		AddEvent(EVENT_PP2_HS);
	else
		RemoveEvent(EVENT_PP2_HS);

	mMainPagePtr->UpdateEventsList();

	return RET_OK;
}

bool CEventMgr::AddEvent(unsigned int EventID)
{
	//Check if already on the list.  If so, do nothing.
	for(int i = 0; i < mEventsList.size(); i++)
	{
		if(mEventsList.at(i)->mEventType == EventID)
			return false;
	}

	QString Label;
	bool IsAnimated = false;
    int Color = CEventItem::EVENT_ITEM_COLOR_RED;
	switch(EventID)
	{
		case EVENT_FN_HS:
		{
			Label = "Frotteur Négatif HS";
			IsAnimated = true;
			break;
		}
		case EVENT_PG_HS:
		{
			Label = "Pneu Guidage HS";
			IsAnimated = true;
			break;
		}
		case EVENT_PP_HS:
		{
			Label = "Pneu Porteur HS";
			IsAnimated = true;
			break;
		}
		case EVENT_PP2_HS:
		{
			Label = "Pneu Porteur ZT2 HS";
			IsAnimated = true;
			break;
		}
		case EVENT_TK1_HS:
		{
			Label = "TK1 HS";
			break;
		}
		 case EVENT_CA_PG:
		{
			Label = "Calibration Pneu Guidage";
			IsAnimated = true;
			break;
		}
		case EVENT_PEQ1:
		{
			Label = "Panne Équipement ZT1";
			break;
		}
		case EVENT_MAINTENANCE:
		{
			Label = "Entretien ZT";
			IsAnimated = true;
			break;
		}
        case EVENT_ZT1_INHIBITION:
        {
            Label = "Passage ZT1 inhibé";
            IsAnimated = true;
            Color = CEventItem::EVENT_ITEM_COLOR_GREEN;
            break;
        }
        case EVENT_ZT2_INHIBITION:
        {
            Label = "Passage ZT2 inhibé";
            IsAnimated = true;
            Color = CEventItem::EVENT_ITEM_COLOR_GREEN;
            break;
        }
	}

    CEvent *NewEvent = new CEvent(EventID,Label,IsAnimated,Color);
	mEventsList.append(NewEvent);

	return true;

}

bool CEventMgr::RemoveEvent(unsigned int EventID)
{
	//Find item in the list and remove it if present
	for(int i = 0; i < mEventsList.size(); i++)
	{
		if(mEventsList.at(i)->mEventType == EventID)
		{
			delete mEventsList.at(i);
			mEventsList.removeAt(i);
		}
	}

	return true;
}

unsigned int CEventMgr::AddSingleEvent(unsigned int EventID)
{
	if(AddEvent(EventID) == true)
	{
		mMainPagePtr->UpdateEventsList();
		return RET_OK;
	}

	return RET_ERROR;
}

unsigned int CEventMgr::RemoveSingleEvent(unsigned int EventID)
{
	if(RemoveEvent(EventID) == true)
	{
		mMainPagePtr->UpdateEventsList();
		return RET_OK;
	}

	return RET_ERROR;
}
