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

*/

/* ************************************************************************** */
/* Revision:
### 20131021 JFM
    Verision d'origine.

### YYYMMDD Description du besoin ou du bug
    Description du changement.
 */

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


#ifndef DATABASEMGRPAGE_H
#define DATABASEMGRPAGE_H

#include "GlobalDefine.h"
#include <QGraphicsWidget>
#include "Guipage.h"
#include <QString>
#include "PushButton.h"
#include "TextButtonWidget.h"
#include "ZTData.h"
#include <QCheckBox>
#include "LogMgr.h"
#include <QProgressBar>
#include <QThread>
#include <QReadWriteLock>
#include "ZTLogFilesMgr.h"
#include <QSpinBox>
#include <QDateTime>
#include <QProcess>
//#include "DatabaseArchiver.h"

class COutilZT;

class CDatabaseImporterThread : public QObject
{
    Q_OBJECT
public:

    enum eDatabaseThreadTask
    {
        DATABASE_THREAD_NO_TASK,
        DATABASE_THREAD_IMPORT_LOGS_TASK,
        DATABASE_THREAD_ARCHIVE_DB_TASK
    };

    CDatabaseImporterThread();

    void SetDatabasePath(QString path){mDatabasePath = path;}
    void SetLogMgrHandle(CLogMgr *Handle){mLogMgrHandle = Handle;}
    void SetImporParams(QStringList SelectedDirs, bool FilterDuplicates, bool OnlyErrorLogs);
    void SetDatabaseFilters(QDate FilterDate);
    void KillThread();
    bool SetThreadTask(int task);

public slots:

    void ExecuteThread();
    void New7zipStdOutput();
 //   void Process7zipFinished();

private:

    int ParseDir(QDir dir);
    int CopyDir(QDir Dir, QDir DestDir,bool NotifyWhenCopied = false, bool Recursive = false);
    bool ClearDir(QDir Dir);
    bool DeleteFile(QString FilePath);
    void ImportLogs();
    int ArchiveDB();

//    bool mProcess7zipRunning


    bool mKillThread;
    QReadWriteLock mMutex;

    QString mDatabasePath;
    CLogMgr *mLogMgrHandle;


    QStringList mSelectedDirs;
    bool mFilterDuplicates;
    bool mOnlyErrorLogs;

    bool IsThreadKilled();
    int ProgressValue;
    int mThreadTask;
    QDate mDatabaseArchiveFilterDate;
    QProcess *m7zipProcess;


signals:
    void DatabaseImportFinished(bool,QStringList*);
    void SetProgressBarRange(int,int);
    void SetProgressBarVal(int);
    void ResetProgressBar();
    void SetProgressText(QString);
    void DatabaseArchiveFinished(bool, int);


};

class CDatabaseMgrPage : public CGuiPage
{
Q_OBJECT
public:
    CDatabaseMgrPage(QGraphicsWidget *Parent = 0,bool EngMode = false);
    ~CDatabaseMgrPage();



    COutilZT *mProgramHandle;
    CLogMgr *mLogMgrHandle;
    CZTLogFilesMgr *mZTLogFilesManager;

    void UpdateDBStats();
    void EnableEngineeringMode();
    void EnableLogImport();

    virtual void showEvent(QShowEvent *event);
    virtual void resizeEvent(QGraphicsSceneResizeEvent *event);
	virtual void mousePressEvent(QGraphicsSceneMouseEvent *event);
	virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);


private:

    QGraphicsRectItem *mBackgroundRect;
    CTextButtonWidget *mCancelButton, *mImportDataButton, *mCancelImportButton;
    QCheckBox *mImportErrorDataOnlyChkBx, *mFilterDuplicateChkBx, *mDeleteSourceDataChkBx, *mImportInPlace;
    QProgressBar *mProgressBar;
    QGraphicsTextItem *mProgressStatusText;

    CTextButtonWidget *mDeleteDBDuplicatesBtn, *mDeleteNonErrorLogs, *mDeleteZTLogFiles, *mRebuildDatabaseFile, *mArchiveDatabase;
    QProgressBar *mDBMaintenanceProgressBar;
    QGraphicsTextItem *mDBMaintenanceProgressStatusText;
    QGraphicsTextItem *mDBFileCountText;
    QSpinBox *mArchiveKeepDays;


    CDatabaseImporterThread *mDatabaseImporterWorkerThread;
    QThread *mDatabaseImporterThread;


    bool ImportData(bool OnlyError, bool FilterDuplicates);
    bool CleanDBDuplicates();
    bool CleanDBNormalPasses();
    bool CleanSingleZTLogFiles();
    bool DeleteFile(QString FilePath);
    bool ArchiveDB();
    bool RemoveDir(QDir Dir);


//    int ParseDir(QDir dir);
//    bool CopyDir(QDir Dir, QDir DestDir);
//    bool ClearDir(QDir Dir);
//    bool DeleteFile(QString FilePath);




public slots:
	void ButtonClicked(CTextButtonWidget *);
    void DatabaseImportFinished(bool,QStringList*);
    void DatabaseArchiveFinished(bool,int);
    void SetProgressText(QString);

};



#endif // DATABASEMGRPAGE_H
