#ifndef STHREADIMAGESAVE_H
#define STHREADIMAGESAVE_H

#include <QThread>
#include <QMutex>
#include <QDebug>
#include <QMutexLocker>

#include "qsthread.h"

#include "common.h"

#include "sdcmworklist.h"

#ifdef _SYOLOV5
#include "syolov5.h"
#endif

#include <QImage>

#include <QTimer>

#include "sdatabase.h"
using namespace SUTIL;

#include "scurl.h"




class SThreadImageSave : public QSThread
{
    Q_OBJECT
public:
    explicit SThreadImageSave();

    virtual ~SThreadImageSave();

    virtual void InnerRun();

    virtual void Init();

    virtual void OpenDatabase();

    CAPTURE_IMAGE* SetImageCapture(uint8_t* pImage, int nWidth, int nHeight);
    CAPTURE_IMAGE* SetVideoCapture(uint8_t* pImage, int nWidth, int nHeight);



    void SendImage(vector<QString> listFile);

    void LoadHistoryImage();

    //vector<WorklistResponseData>* GetWorklistData();
    vector<CSDCM*>* GetWorklistDump();



    void MissCapture();
    void ClearMissCapture();

    void SaveCaptureImage();
    void SaveCaptureImageToFile();
    void ExitCapture();

    void StopLoadImage();

    void AddCaptureSaveToFile(CAPTURE_IMAGE* pCaptureImage);
    void ClearCaptureSaveToFile();

    void SetImageDetect(uint8_t* pImage, int nWidth, int nHeight);

    void ExportUSB();
    void ExportUSB_mp4();
    void ExportNetworkDirectory();

    void CleanStorage();


    void CheckAndCreateDir(QString strDir);

    void CancelLoad();

    void LoadAcquisitionFromHistory();

    QString GetFindImageWithMovieFile(const QString strMovieFile);

    void SetListImageWithStudy(const QList<QStringList> list);
    void SetListImageWithStudyASC(const QList<QStringList> list);

    void SetSchduledDeleteStudyDateList(const QStringList& list);

    void USBBackup();

    bool IsExecuteBackup();
    void SetExecuteBackup(bool bBackup);

    void SetExecuteDetect(bool bDetect);

    void MountUSBBackup();
    void UmountUSBBackup();

    void USBBackupInner();

signals:
    void ViewDialogMissCapture();
    void UpdateSendFailed();
    void captureImage(CAPTURE_IMAGE* pCaptureImage);
    void captureVideo(CAPTURE_IMAGE* pCaptureImage);

    void SetSendProgress(int nProgress, int nTotal);

    void SetImageLoadInfo(int nLoad, int nTotal,  ImageSelect* pImageSelect);

    void SetProgressExportFile(int nProgress, int nTotal);
    void SetProgressExportPercent(quint64 nProgress, quint64 nTotal);
    void SetProgressSync(bool bComplete);

    void ImageLoadComplete();

    void CompleteAcquisitionFromHistory();

    void LoadInfoAcquisitionFromHistory(int nCurrent, int nTotal);

    void SetExportComplete();
    void SetExportInit();

    void CompleteCleanStorage();







public slots:
    void StartExport(int nType);
    void StopExport();

    void MissCaptureBeep();

    void SetProgressExportPercentSlot(quint64 nCurrent, quint64 nTotal);

protected:
    bool m_bDeferredInitMainWidget;
    SCURL* m_pCURL;

    bool m_bErrorSamba;
    QMutex m_LockSendList;
    QTimer m_TimerMissBeep;
    QMutex m_LockImageSaveToFile;
    QMutex m_LockExport;
    int m_nMissCount;
    bool m_bMissCapture;
    bool m_bCancelLoad;
    bool m_bExport;
    int m_nExportType;
    vector<QString> m_ListSendFile;
    vector<QString> m_ListSendFileWait;
    int m_nDetectOffsetX;
    int m_nDetectOffsetY;
    int m_nDetectWidth;
    int m_nDetectHeight;
    bool m_bDetectReady;
    uint8_t* m_pImageDetect;
    uint8_t* m_pImageDetectSrc;
    QMutex m_LockDetect;
    QImage* m_pImageTest;
#ifdef _SYOLOV5
    SYolov5* m_pDetectObject;
#endif
    bool m_bLoadImage;

    vector<WorklistResponseData>* m_pWorklistData;
    vector<CSDCM*>* m_pDCMDumpList;
    WorklistResponseData* m_pCurrentWorklist;

    vector<CAPTURE_IMAGE*> m_ListCaptureSaveToFile;
    bool m_bSaveImage;

    //SDatabase* m_pDatabase;

    QList<QStringList> m_ListImageWithStudy;
    QList<QStringList> m_ListScheduledDeleteStudyDateList;

    bool m_bExecuteUSBBackup;

};

#endif // STHREADIMAGESAVE_H
