/*******************************************************************************
*                                                                              *
* Société de Transports de Montréal.                                    	   *
* 2012                                                                         *
*                                                                              *
* Projet Zones Tests                                                           *
*                                                                              *
*                                                                              *
*                                                                              *
*******************************************************************************/
/*
	Description:
	Description du fichier si nécessaire.
	
*/

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

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

#ifndef CZTSIMULATOR_H
#define CZTSIMULATOR_H
#include <QGraphicsWidget>
#include "SimulatorIOManager.h"
#include "GlobalDefine.h"
#include "Station.h"
#include "SimulatorLazerProbesMgr.h"
#include "SimulatorIOManager.h"
#include "SimulatorPCIIO.h"
#include "CDVItem.h"
#include "ToggleTextButtonWidget.h"
#include "PushButton.h"
#include <QTimer>
#include "SimulationScenario.h"
#include <QElapsedTimer>
#include "LedWidget.h"
#include "SimulatorThread.h"
#include <QCheckBox>
#include "USBDriveInterface.h"
#include "IOManager.h"



#define SIM_LAZER_PROBES_UPDATE_TIMEOUT 1 //milliseconds
#define USE_REAL_ANALOG_EXTERNAL_MODULE

class CZTPage;


class CZTSimulator : public QGraphicsWidget
{
	Q_OBJECT
public:
	CZTSimulator(CSimulatorIOManager *mIOMgrPtr);
	~CZTSimulator();




	void Show();
	unsigned int Init(CZTPage *MainPagePtr, CStation *Station, CIOManager *IOMgrPtr,CPCIIOMgr *PCIIO,CAbstractLazerProbeMgr *LazerProbesMgr,CUSBDriveInterface *UsbDriveInterface, CAnalogInputModule *RealAnalogModule=0);
	void UpdateSimulatorDisplay();
//    void UpdateOutputsDisplay(quint32 Outputs);
	void StartSim();
	void SetPGTreshold(qint32 Treshold);

private:
	CSimulatorIOManager *mIOMgrPtr;
	CSimulatorPCIIO *mPCIIO;
	CSimulatorLazerProbesMgr *mLazerProbesMgr;
	CSimulatorLazerProbe *mInternalLazerProbe, *mExternalLazerProbe;
	CZTPage *mMainPagePtr;
	CStation *mStationPtr;
	CSimulatorInputModule *mInputModule;
	CSImulatorOutputModule *mOutputModule;
	CSimulatorMixedModule *mMixedModule;

	CSimulatorThread *mSimWorkerThread;
	QThread *mSimThread;
	CLogElement *mSimLogElement;
	CUSBDriveInterface *mUSBDriveInterfacePtr;

	GenericInputMasks_t *mStationInputMasks;
	GenericOutputMasks_t *mStationOutputMasks;

	//Simulator GUI items
	QList<CCDVItem*> mSimCDVItemsList;
    CToggleTextButtonWidget *mITIZT1ToggleBtn, *mITIZT2ToggleBtn, *mITIZT1AltToggleBtn;
	CToggleTextButtonWidget *mS1SimBtn, *mS2SimBtn, *mS1S2SimBtn, *mFNSimBtn, *mPPISimBtn, *mPPESimBtn;
    CToggleTextButtonWidget *mZT2S1SimBtn, *mZT2PPISimBtn,*mZT2PPESimBtn;
	CPushButton *mSimulationStartBtn, *mSimFileOpenBtn, *mSimDirOpenButton;
    CToggleTextButtonWidget *mAN1SimBtn, *mAN2SimBtn;
	QCheckBox *mLoopSimCheckbox;
    QCheckBox *mProbesAliveCheckBox;

	//USB stick presence emulation
	QCheckBox *mUSBKeyEmuEnableCheckBox;
	CToggleTextButtonWidget *mUSBKeyPresenceToggleBtn;

	//General simulator stuff
	QElapsedTimer *mSimulationElapsedTimer;
	QTimer mFileSimLoopDelayTimer;

	//Simulation scenario stuff
	CSimulationScenario *mScenario;

	//Lazer probes simulation
	QTimer *mLazerProbesUpdateTimer;
	unsigned int mPGTreshold;
	unsigned int mPGIValue, mPGEValue;

    //Outputs display
    CLedWidget *LedWidget[GENERIC_OUTPUT_NB_ID];


	void DestroyLogElement();


public slots:
	void CDVItemRightClicked(CCDVItem *ClickedItem);
	void ItineraryToggleSwitchActivate(CToggleTextButtonWidget*);
	void ItineraryToggleSwitchDeActivate(CToggleTextButtonWidget*);
	void ZT1SensorBtnActivate(CToggleTextButtonWidget*);
	void ZT1SensorBtnDeActivate(CToggleTextButtonWidget*);
    void ZT2SensorBtnActivate(CToggleTextButtonWidget*);
    void ZT2SensorBtnDeActivate(CToggleTextButtonWidget*);
	void SimulationScenarioButtonPressed(CPushButton*);
	void SimulationScenarioExecStep(CSimulationStep*);
	void SimulationScenarioFinished();
	void LazerProbesSimTimerExpired();
	void FileSimFinished();
	void FileLoopDelayExpired();
	void USBEmulationCheckboxChanged(int state);
	void USBEmulationPresenceToggled(CToggleTextButtonWidget*);
	void UpdateOutputsDisplay(quint32);
    void SimulDefectLazerCheckboxChanged(int state);


};

#endif // CZTSIMULATOR_H
