/*******************************************************************************
*                                                                              *
* Société de Transports de Montréal.                                    	   *
* 2012 - 2013                                                                        *
*                                                                              *
* 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 LOGMGR_H
#define LOGMGR_H

#include "ZTData.h"
#include "GlobalDefine.h"
#include <QList>
#include <QDir>
#include <QTimer>
#include <QReadWriteLock>
#include "DirParserThread.h"

class CLogElement
{
public:
    unsigned int mZTLogType;
	virtual ~CLogElement();
};
//QDataStream &operator<<(QDataStream &out, const CLogElement &source);
//QDataStream &operator>>(QDataStream &in, CLogElement &dest);

class CZT1LogElement : public CLogElement
{
public:
    CZT1LogElement(){mZTLogType=ZT1_LOG_TYPE;}
    ~CZT1LogElement();
    QDateTime mPassageDateTime;
	QVector<CZT1LogData*> mZTLogData;
	QVector<CZTDetectionData*> mZTDetections;
	quint32 mTrainType;
	quint32 mNbElements;
    quint64 mThreadDataStartTime, mThreadDataEndTime;
	QString mLogFileName;
	qreal mMeanSpeed;
	CZT1FlagsData mFlags;
	QString mStationName;
    bool mFileProtected;
    int mFileVersion;
};
//QDataStream &operator<<(QDataStream &out, const CZT1LogElement &source);
//QDataStream &operator>>(QDataStream &in, CZT1LogElement &dest);

class CZT2LogElement : public CLogElement
{
public:
    CZT2LogElement(){mZTLogType = ZT2_LOG_TYPE;}
    ~CZT2LogElement();
    QDateTime mPassageDateTime;
	QVector<CZT2LogData*> mZTLogData;
	QVector<CZTDetectionData*> mZTDetections;
	QString mLogFileName;
	quint32 mNbElements;
	QString mStationName;
    CZT2FlagsData mFlags;
    bool mFileProtected;
};

class COutilZT;

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

    COutilZT *mProgramHandle;

	unsigned int GetLogsCount();
	QList<CLogElement*> *GetLogsList();
    unsigned int ParseLogs(bool RebuildDatabase = false, bool KeepData = false);
	unsigned int ParseNewLog(QString FileName, bool KeepData = false);
    unsigned int ParseImportedLogs(QStringList *NewLogFiles);
	CLogElement* LoadLogData(CLogElement * Element);
	unsigned int FreeLogData(CLogElement * Element);
    unsigned int DeleteLog(int LogIndex);
    void SetLogDataDir(QString Dir){mLogDataDir = Dir;}
    bool ParseNextLog();
    unsigned int SaveDatabaseFile();
    unsigned int RebuildDatabaseFile();
    unsigned int ProtectLogElementFile(bool IsProtected, CLogElement *Element);
    QTimer *mDatabaseParsingTimer;
    QDir GetBaseLogDataDir(){return QDir(mLogDataDir);}


private:
    CDirParserThread *mDirParserWorkerThread;
    QThread *mDirParserThread;
	QList<CLogElement*> mPassagesList;
	unsigned int DestroyLogList();
    int ParseDir(QDir dir, bool KeepData = false);


    QFileInfoList mLogsFilelist;
    unsigned int mLogsFileIndex;
    QString mLogDataDir;

    bool mParsingFinished;
    unsigned int mDatabaseFileCounter;
    bool mSaveDBFile;

public slots:
    void NewLogParsed(QString,bool);
    void NewLogParsed(CLogElement*);
    void DirParsingFinished(int);
    void ParsingTimerExpired();
    void EmptyDirParsed();


    void ThreadQuit();
    void ThreadTerminated();

};




#endif // LOGMGR_H

