#ifndef COMMONDATA_H
#define COMMONDATA_H

#include "common.h"
#include "sdcmworklist.h"
#include "sdcmfindscudatacallback.h"
#include "qrtspthread.h"
#include "sthreadimagesave.h"
#include "sthreadsenddicom.h"
#include "sthreadworklistsearch.h"
#include "stableheader.h"
#include "sdatabase.h"
using namespace SUTIL;

#include "sambaclient.h"

#include "acquisition_info.h"

#include <QMutex>
#include <QDateTime>


#define WRITE_FUNCTION_LOG() { QString strLog; strLog = QString("[%1] %2:%3,%4\n").arg(QTime::currentTime().toString("HH:mm:ss")).arg(__FILE__).arg(__LINE__).arg(__FUNCTION__); MainWindow::GetCommonData()->WriteLog(strLog);}
#define WRITE_FUNCTION_LOG_PARAM(X) { QString strLog; strLog = QString("[%1] %2:%3,%4(%5)\n").arg(QTime::currentTime().toString("HH:mm:ss")).arg(__FILE__).arg(__LINE__).arg(__FUNCTION__).arg(X); MainWindow::GetCommonData()->WriteLog(strLog);}

class CommonData
{
public:
    CommonData();
    virtual ~CommonData();

    QString GetNewFilename(int nType);

    static void RestartNetworkManager();



    bool IsActiveDetect();
    void SetActiveDetect(bool bActive);

    void Exit();

    void SetSearchItemHistory(SEARCH_ITEM si);
    void SetHistorySearchStudyIndex(int nStudyIndex);

    SEARCH_ITEM* GetSearchItemHistory();
    int GetHistorySearchStudyIndex();

    void ClearHistorySearchItem();
    void ClearHistorySearchStudyIndex();

    //void ReOpenDatabase();
    //SDatabase* GetDatabase();
    //void ReleaseDatabase();

    ACQUISITION_INFO* GetAcquisitionInfo();
    void ReleaseAcquisitionInfo();

    QDate GetDateSearchWorklistStart();
    QDate GetDateSearchWorklistEnd();

    void SetDateSearchWorklistStart(QDate date);
    void SetDateSearchWorklistEnd(QDate date);

    QString GetStorageDirectory();


    QDate GetDateSearchHistoryStart();
    QDate GetDateSearchHistoryEnd();

    void SetDateSearchHistoryStart(QDate date);
    void SetDateSearchHistoryEnd(QDate date);

    SEARCH_ITEM* GetSearchItemWorklist();

    SDCMFindSCUDataCallback* GetFindSCUDataCallback();
    vector<WorklistResponseData>* GetWorklistData();
    void ReleaseWorklistData();
    vector<CSDCM*>* GetWorklistDump();

    void SetRTSPThread(QRTSPThread* pThread);
    QRTSPThread* GetThreadRTSP();

    void SetThreadCaptureImage(SThreadImageSave* pThread);
    void SetThreadSendDICOM(SThreadSendDICOM* pThread);
    void SetThreadWorklistSearch(SThreadWorklistSearch* pThread);
    SThreadImageSave* GetThreadCaptureImage();
    SThreadSendDICOM* GetThreadSendDICOM();
    SThreadWorklistSearch* GetThreadWorklistSearch();


    vector<ImageSelect*>* GetListHistoryImage();
    void ReleaseListHistoryImage();
    void ClearHistoryImage();

    vector<CAPTURE_IMAGE*>* GetListCaptureImage();
    void ReleaseListCaptureImage();
    void ClearCaptureImage();
    CAPTURE_IMAGE* AddCaptureImage(uint8_t* pData, int nWidth, int nHeight, int nCaptureType);
    bool ConvertCaptureImage(CAPTURE_IMAGE* pCaptureImage, int nWidth, int nHeight);
    void ConvertCaptureImageRGBA(CAPTURE_IMAGE* pCaptureImage, int nWidth, int nHeight);

    uint8_t* ConvertColor(uint8_t* pData, int nWidth, int nHeight);

    void ConvertColor2(uint8_t* pSrcData, uint8_t* pDestData, int nOffsetX, int nOffsetY, int nWidth, int nHeight);

    void AddCaptureImageWithHistory();
    void AddCaptureImageWithHistory_v1();

    vector<HISTORY_STUDY*>* GetHistorySearchResult();
    void ClearHistorySearchResult();
    HISTORY_STUDY* GetCurrentHistoryStudy();
    void ReleaseCurrentHistoryStudy();
    void SetCurrentHistoryStudy(int nIndex);

    void InitWorklistIndex();
    void UpdateWorklistIndex();

    void InitHistoryIndex();
    void UpdateHistoryIndex();


    bool IsEncodingVideo();
    void SetEncodingVideo(bool bEncoding);

    STableHeader* GetWorklistTableHeader();
    STableHeader* GetHistoryTableHeader();

    int GetDisplayType();
    void SetDisplayType(int nType);

    SDCMWorklist* GetWorklistClass();

    SDCM_SERVER_INFO* GetServerWorklistInfo();
    void ReleaseServerWorklistInfo();

    SDCM_SERVER_INFO* GetServerStorageInfo();
    void ReleaseServerStorageInfo();


    void UpdateSetting();
    void UpdateAdvancedSetting();
    void InitSetting();

    QString GetModality();
    QString GetInstitutionName();
    QString GetStationName();
    int GetTransferSyntax();

    void SetModality(QString strModality);
    void SetInstitutionName(QString strInstitutionName);
    void SetStationName(QString strStationName);
    void SetTransferSyntax(int nTransferSyntax);

    bool GetEnableLog();
    QString GetLocalImageFolder();
    QString GetLogFolder();
    QString GetLocalHistoryFolder();

    void SetEnableLog(int bEnable);
    void SetLocalImageFolder(QString strLocalImageFolder);
    void SetLogFolder(QString strLogFolder);
    void SetLocalHistoryFolder(QString strLocalHistoryFolder);

    void Beep(bool bSound);

    WorklistResponseData GetCurrentHistoryWorklistData();

    WorklistResponseData GetCurrentWorklistData();
    void ReleaseCurrentWorklistData();

    qint64 ComputeStorageSize();

    int GetStorageSize();
    int GetStorageFreeSize();

    vector<QString>* GetExportList();

    QString* GetExternalDiskPath();

    QString* GetImageExtension();

    bool IsCapturing();

    int GetHandSwitchValue();

    bool IsAutoCloseSendFinish();
    void SetAutoCloseSendComplete(bool bEnable);

    QString GetBeepGPIONumber();
    QString GetHandswitchGPIONumber();

    void SetUseNetworkBackupVideo(bool bUse);
    bool IsUseNetworkBackupVideo();

    NetworkShareInfo* GetNetworkShareInfo();

    bool IsAutoExitCaptureSendDICOM();
    bool IsAutoCaptureSendDICOM();
    bool IsAutoVideoCapture();
    bool IsAutoLogin();

    void SetAutoExitCaptureSendDICOM(bool bEnable);
    void SetAutoCaptureSendDICOM(bool bEnable);
    void SetAutoVideoCapture(bool bEnable);
    void SetAutoLogin(bool bEnable);

    SambaClient* GetSambaClient();

    void CreateSambaClient();

    QString GetDICOMCharacterSet();
    void SetDICOMCharacterSet(QString strCharacterSet);

    void WriteLog(QString strLog);
    QStringList* GetListLog();
    void ReleaseListLog();

    void WriteLogFile(QString strLog);

    void SetVideoMode(int nMode);
    int GetVideoMode();

    void SetCropMode(int bCropMode);
    int GetCropMode();

    void SetUseFixWidthLevel(int bFixWidthLevel);
    int GetUseFixWidthLevel();

    void SetScreenSaverTime(int nMinutes);
    int GetScreenSaverTime();

    void SetLogLevel(int nLevel);
    int GetLogLevel();

    void SetUseUSBBackup(int nUSBBackup);
    int IsUseUSBBackup();

    int GetVideoWidth();
    int GetVideoHeight();

    void MakeBitmapHeader(int nWidth, int nHeight);
    bool WriteBitmapFile(QString strFile, QImage* pImage);
    bool WriteBitmapFile(QString strFile, uint8_t* pData, int nWidth, int nHeight);

    bool WriteJPEG(QString strFile, uint8_t* pData, int nWidth, int nHeight, int nQualilty);


    void SetConnectHDMI(bool bConnect);
    bool IsConnectHDMI();

    void GetSearchDateStartEnd(int nSearchDurationType, QDate& nDateStart, QDate& nDateEnd);

    void SetUseWebDisplay(bool bUse);
    bool IsUseWebDisplay();

protected:

    QStringList m_ListLog;

    bool m_bConnectHDMI;

    int m_nScreenSaverTime;

    uint8_t* m_pBitmapData;

    BITMAPFILEHADER m_bf;
    BITMAPINFOHEADER m_bi;

    int m_nVideoWidth;
    int m_nVideoHeight;

    QFile* m_pLogFile;

    QString m_strDICOMCharacterSet;
    SambaClient* m_pSambaClient;
    NetworkShareInfo m_NetworkShareInfo;
    bool m_bActiveDetect;
    QString m_strGPIOBeep;
    QString m_strGPIOHandSwitch;
    bool m_bIsCapturing;
    QString m_strImageExtension;
    QString m_strExternalDiskPath;
    int m_nGPIO_Buzzer;
    int m_nGPIO_HandSwitch;

    bool m_bAutoExitCaptureSendDICOM;
    bool m_bAutoCaptureSendDICOM;
    bool m_bAutoVideoCapture;
    bool m_bAutoLogin;

    int m_nVideoMode;

    int m_nCropMode;

    int m_nUseFixWidthLevel;

    int m_nUseLogLevel;

    bool m_bAutoCloseSendFinish;

    bool m_bEnableLog;
    int m_nUseUSBBackup;
    QString m_strLocalImageFolder;
    QString m_strLogFolder;
    QString m_strLocalHistoryFolder;
    QString m_strModality;
    QString m_strInstitutionName;
    QString m_strStationName;
    int m_nTransferSyntax;
    SDCM_SERVER_INFO m_ServerWorklistInfo;
    SDCM_SERVER_INFO m_ServerStorageInfo;
    int m_nDisplayType;
    STableHeader* m_pWorklistTableHeader;
    STableHeader* m_pHistoryTableHeader;
    //SDatabase* m_pDatabase;
    ACQUISITION_INFO m_AcquisitionInfo;

    SThreadImageSave* m_pThreadCaptureImage;
    SThreadSendDICOM* m_pThreadSendDICOM;
    SThreadWorklistSearch* m_pThreadWorklistSearch;
    QRTSPThread* m_pRTSPThread;

    QString m_strStorageDirectory;

    SearchDate m_SearchDateWorklist;
    SearchDate m_SearchDateHistory;

    SEARCH_ITEM m_SearchItemWorklist;
    SEARCH_ITEM m_SearchItemHistory;

    int m_nSearchItemHistoryStudyIndex;

    SDCMWorklist* m_pDCMWorklist;
    SDCMFindSCUDataCallback* m_pFindSCUDataCallback;
    vector<WorklistResponseData>* m_pWorklistData;
    vector<CSDCM*>* m_pDCMDumpList;
    WorklistResponseData* m_pCurrentWorklist;

    vector<CAPTURE_IMAGE*>* m_pListCaptureImage;
    RGB_Data* m_pImageCapture;

    vector<ImageSelect*>* m_pListHistoryImage;


    vector<HISTORY_STUDY*>* m_pListHistorySearch;
    HISTORY_STUDY* m_pCurrentHistoryStudy;

    bool m_bVideoEncoding;

    bool m_bUseWebDisplay;


    qint64 m_nStorageSize;

    qint64 m_nStorageFreeSize;

    vector<QString> m_ListExport;

    QMutex m_LockAddCapture;
    QMutex m_LockAddHistory;
    QMutex m_LockLog;
    QMutex m_LockAcquisitionInfo;
    QMutex m_LockCurrentHistoryStudy;
    QMutex m_LockWorklistData;
    QMutex m_LockDatabase;

    QMutex m_LockServerWorklistInfo;
    QMutex m_LockServerStorageInfo;



};

#endif // COMMONDATA_H
