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

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

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

#ifndef ZONETEST_H
#define ZONETEST_H
#include "MainPanel.h"
#include "Station.h"
#ifndef WINDOWS_OS
#include "Seaiolibinterface.h"
#include "ExternalIOMgr.h"
#include "Comedilibinterface.h"
#endif

#include <QTimer>
#include <QObject>
#include <QSocketNotifier>
#include <QLocalSocket>

#include "WelcomePage.h"
#include "LazerProbesMgr.h"
#include "ZTStateMachine.h"
#include "ZTSimulator.h"
#include "IOManager.h"
#include "PCIIOMgr.h"
#include "ZTData.h"
#include "EventMgr.h"
#include "LogMgr.h"
#include "ZTSettings.h"
#include "USBDriveInterface.h"
#include "ExtIOThread.h"
#include "DI710Driver.h"
#include "WatchdogCtrl.h"
#include "DiscreteTKTransport.h"
#include "ModbusRepository.h"
#include "ModbusCCMgr.h"
#include "ModbusSEIMgr.h"
#include "NetworkCfgMgr.h"
#include "NetDriveMgr.h"
#include "SFTPServerManager.h"
#include "PIHistorianManager.h"

#include "TCPProtocol.h"

enum eAppExitReason
{
    EXIT_REASON_USER_REQUEST = 0,
    EXIT_REASON_POWER_BUTTON,
    EXIT_REASON_SYSTEM_REBOOT
};

class CZoneTest : public QObject
{
    Q_OBJECT
public:
    CZoneTest();
	~CZoneTest();

    unsigned int Run();
    unsigned int Start();

	bool mFullScreen;
	bool mSimulateLazerProbes;
	bool mIgnoreStationPhysicalKey;
	bool mDisablePassword;
    bool mUseWatchdog;  //internal CPU watchdog
    bool mDoNotReboot;

    CWelcomePage *mWelcomePagePtr;


	void ZTFunctionsOptionSelected();
	void ZTMaintenanceOptionSeleced();
	void OptionsMenuPageSelected();
	void LogsPageSelected();
	unsigned int DetectionFunctionsConfigChanged(CZTDetectionFunctionConfig *NewConfig);


	void LogViewRequest(CLogElement* element);
	QString GetStationShortName();
	QString GetStationTextualName();

	void LogViewCloseRequest();
	void OpenEngineeringPageRequest();
	void CloseEngineeringPageRequest();
	void TrainFilesChanged();
	void SaveAutoExportCSVSettings(bool AutoExportZT1CSV, bool AutoExportZT2CSV);
	bool StartPGCalibrationRequest(int NbPassages);
	bool StopPGCalibrationRequest();
	int GetPGTreshold();
	unsigned int SetPGTreshold(int NewTreshold);
    void HighResLogging(bool HighResON);
	void ProgramExitRequest();
    void KeepAllMPM10Logs(bool Keep);
    void KeepAllZT1Logs(bool keep);
    void KeepAllZT2Logs(bool keep);
    void SetLogFilesNumber(int NbLogFiles);

	bool EnterMaintenanceModeRequest();
	void CloseMaintenancePageRequest();
	void ExitMaintenanceModeRequest();
    unsigned int ClearMaintenanceCurTKRequest();
	unsigned int SendTKToPCC(int DetectionID, int Rank);
    void SetWatchdogEnabled(bool Enabled);

	void OpenGeneralSettingsPage();
	void CloseGeneralSettingsPage();
    void UseNetworkTime(bool UseNetworkTime);

    void OpenModbusSettingsPage();
    void CloseModbusSettingsPage();
    void ModbusSettingsChangedReboot(bool UseModbus, QHostAddress CCHostAdd);

    void OpenSEISettingsPage();
    void CloseSEISettingsPage();
    void ApplySEISettings(QHostAddress SEIHostAdd);

	void ShowZTLogViewerPage();
	void HideZTLogViewerPage();

	void ResetTriggerCount();
    void ResetNbPassages();

    void OpenModbusDataPageRequest();
    void CloseModbusDataPageRequest();

    void OpenHistorienDataPageRequest();
    void CloseHistorienDataPageRequest();

    void LogZTSettingsRequest(bool LogStationName = false);

    void ApplicationQuit(int Reason);

    unsigned int PauseInternalWatchdog();
    unsigned int ResumeInternalWatchdog();

    bool IsZT2PresentInStation();

    int SetSystemDateTime(QDateTime *DateTime);



    //Networking requests
    CTCPZTStatus *GetTCPStatusRequest();//HAVE TO BE DELETED BY CALLER !!!
    QString *GetZTLogTextRequest();//HAVE TO BE DELETED BY CALLER !!!
    QFileInfoList GetTrainLogsFileListRequest();
    unsigned int DeleteZTLogRequest();
    unsigned int SetZTFunctionsConfig(const CTCPZTFunctionsStatus &FunctionsConfig);

    //ZTStateMachine requests
    int SetZT1InhibitionState(bool InhibitionON);
    int SetZT2InhibitionState(bool InhibitionON);
    int NewZT1Passage(CZTPassageInfo ZT1PassageInfo);
    int NewZT2Passage(CZTPassageInfo ZT2PassageInfo);



private:
    unsigned int InitZT();
    unsigned int InitStation();
	void EnterDeadlockState(void);
	void CheckAndCreateDirectories();


    Panel panel;

    CStation *mZTStation;    //The instance of the station in which the ZT is installed.
	CIOManager *mIOManager;
	CPCIIOMgr *mPCIIO;
	CExtIOThread *mExtIOWorkerThread;
	QThread *mExtIOThread;
	CAbstractLazerProbeMgr *mLazerProbesManager;
	CZTStateMachine *mZTStateMachine;
	CZTSimulator *mZTSimulator;
	CEventMgr *mEventMgr;
	CLogMgr *mLogMgr;
    CZTSettingsFileMgr mZTSettingsFileMgr;
    QElapsedTimer mWatchdogTimer;
    COutputModule *mOutputModule;
	CUSBDriveInterface *mUSBDriveInterface;
    QDateTime mZTStartDateTime;
	CAnalogInputModule *mAnalogModule;
    CWatchdogCtrl *mInternalWatchdog;
    bool mWatchdogEnabled;
    CTKTransportInterface *mTKTransportInterface;
    CModbusRepository *mCCModbusRepository, *mSEIModbusRepository, *mPIHistorianRepository;
    CModbusCCMgr *mModbusCCMgr;
    CModbusSEIMgr *mModbusSEIMgr;
    CNetworkCfgMgr mNetworkCfgMgr;
    CNetDriveMgr mNetworkDriveMgr;
    CSFTPServerManager mSFTPManager;
    CPIHistorianManager *mPIHistorianManager;

    QLocalSocket mACPISocket;
 //   CWatchdogCtrl mWatchdogCtrl;





    //Zone test main state machine
    typedef enum eZTStates
    {
        ZT_INIT_STATE,
        ZT_RUN_STATE,
        ZT_DEADLOCK_STATE,
		ZT_EXIT_APPLICATION_STATE,
        ZT_POWER_BUTTON_EXIT_STATE,
        ZT_SYSTEM_REBOOT_EXIT_STATE,
        ZT_MAX_STATE
    }eZTSMStates_t;

    typedef enum eZTSMEvents
    {
        ZT_SM_TICK_EVENT,
        ZT_SM_MAX_EVENT
    }eZTSMEvents_t;

    eZTSMStates_t mZtState;
    unsigned int ExecStateMachine(eZTSMEvents_t);
    QTimer *mZTStateMachineTimer;

    //CZTDetectionFunctionConfig *mZTDetectionConfig;
    CZTSettingsData *mZTSettings;

protected:
	bool eventFilter(QObject *obj, QEvent *event);

private:
	bool mSimulationON;

public slots:
    void SMTimerExpired();
    void ACPISocketConnected();
    void ACPISocketEvent();
    void ModbusDateTimeUpdate(QDateTime *NewDateTime);


     //   CSeaIOLibInterface mSeaIOLibInterface;
};

#endif // ZONETEST_H
