#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>

#include "sthreadsenddicom.h"

#include "mainwindow.h"
#include <QDir>
#include "formcapture.h"

#include "SDCMSendImpl.h"
#include "SDCMImage.h"

#include <QDirIterator>
#include <QProcess>
#include <QDataStream>


SThreadSendDICOM::SThreadSendDICOM():QSThread()
{
    m_nSendPrev = 0;


    m_pCallback = new CSCallbackSendProgress();

    m_bExecAutoSend = false;
}

SThreadSendDICOM::~SThreadSendDICOM()
{
    ExitThread();
}



void SThreadSendDICOM::Init()
{
    m_nCurrentState = 0;

    m_bSendFailed = false;

    m_bExitAutoSend = false;



    //CommonData* pCommonData = MainWindow::GetCommonData();
    //WorklistResponseData rd = pCommonData->GetCurrentWorklistData();
    //pCommonData->ReleaseCurrentWorklistData();

}


void SThreadSendDICOM::InnerRun()
{
    CommonData* pCommonData = MainWindow::GetCommonData();
    if(pCommonData==NULL)
    {
        usleep(1000);
        return;
    }

    if(m_nCurrentState&0x20)
    {
        if(m_bExitAutoSend==true)
        {
            m_LockState.lock();
            m_nCurrentState = (m_nCurrentState & ~0x20);
            m_LockState.unlock();
            m_bExitAutoSend = false;
        }
        else
        {
            if(m_TimeAutoSend.elapsed()>5000)
            {
                m_bExecAutoSend = true;
                m_pCallback->SetExitSend(false);
                AutoSend();
                m_bExecAutoSend = false;
                m_TimeAutoSend.restart();
            }
        }
    }

    if(m_nCurrentState&0x40)
    {
        m_pCallback->SetExitSend(false);
        SendImageList();
    }
    else
    {
        usleep(100*1000);
    }
}



void SThreadSendDICOM::SetAutoSendImage()
{
    m_LockState.lock();
    //m_nCurrentState = (m_nCurrentState | 0x20);
    m_ListAutoSendCompleteFile.clear();
    m_nCurrentState = 0x20;
    m_TimeAutoSend.restart();
    m_LockState.unlock();
}

void SThreadSendDICOM::SetSendImageList(const QList<QStringList> listFile)
{
    int i=0;

    m_LockState.lock();
    {

        /*
        for(i=0 ; i<m_ListSendFile.size() ; i++)
        {
            QStringList* pDataList = m_ListSendFile[i];
            pDataList->clear();
            delete pDataList;
        }
        m_ListSendFile.clear();
        */

        //SDatabase::DeleteListReponse(m_ListSendFile);
        m_ListSendFile.clear();

        for(i=0 ; i<listFile.size() ; i++)
        {
            QStringList listData = listFile[i];
            QStringList newDataList = listData;
            m_ListSendFile.push_back(newDataList);
        }
        m_ListImageSendCompleteFile.clear();
        m_nCurrentState = (m_nCurrentState | 0x40);
    }
    m_LockState.unlock();
}

void SThreadSendDICOM::SetCheckSendImageList(const QList<QStringList> listFile)
{
    m_bExecCheckSendImageList = true;
    if(m_bExecAutoSend==true)
    {
        m_bExitAutoSend = true;

        m_pCallback->SetExitSend(true);

        while(m_bExecAutoSend==true)
        {
            usleep(1000*10);
        }
    }

    m_LockState.lock();
    {
        //SDatabase::DeleteListReponse(m_ListSendFile);
        m_ListSendFile.clear();

        int i=0;
        for(i=0 ; i<listFile.size() ; i++)
        {
            QStringList listData = listFile[i];
            QStringList newDataList = listData;
            m_ListSendFile.push_back(newDataList);
        }
        m_ListImageSendCompleteFile.clear();
        m_nCurrentState = (m_nCurrentState | 0x40);
    }
    m_LockState.unlock();
}



bool SThreadSendDICOM::IsSendFailed()
{
    return m_bSendFailed;
}


void SThreadSendDICOM::SetExitAutoSend()
{
    m_bExitAutoSend = true;
    m_bExecCheckSendImageList = false;

    m_pCallback->SetExitSend(true);
}

void SThreadSendDICOM::SetExitSendImageList()
{
    m_bExitSendImageList = true;

    m_pCallback->SetExitSend(true);
}


void SThreadSendDICOM::AutoSend()
{
    int i=0;
    int j=0;
    CommonData* pCommonData = MainWindow::GetCommonData();

    if(m_bExitAutoSend==true)
    {
        m_LockState.lock();
        m_nCurrentState = (m_nCurrentState & ~0x20);
        m_LockState.unlock();
        m_bExitAutoSend = false;
        return;
    }

    QList<QStringList*> listSendFile;

    vector<int> listAcquisitionNumber;
    vector<QString> listAcquisitionDate;
    vector<QString> listAcquisitionTime;

    vector<CAPTURE_IMAGE*> listSendCaptureImage;
    vector<CAPTURE_IMAGE*>* pListCaptureImage = pCommonData->GetListCaptureImage();
    {
        for(i=0 ; i<pListCaptureImage->size() ; i++)
        {
            CAPTURE_IMAGE* pCaptureImage = (*pListCaptureImage)[i];
            QDateTime dtCheck = QDateTime::currentDateTime();
            dtCheck = dtCheck.addSecs(-10);

            if(pCaptureImage->m_DateTime<dtCheck)
            {
                if(pCaptureImage->m_bSend==false && pCaptureImage->m_nType==DISPLAY_IMAGE && pCaptureImage->m_bDelete==false)
                {
                    QString strFileLocation = pCaptureImage->m_strImageFilename;

                    QFileInfo fi(strFileLocation);
                    if(fi.exists()==true)
                    {
                        CAPTURE_IMAGE* pSendCaptureImage = new CAPTURE_IMAGE;

                        pSendCaptureImage->m_strImageFilename = strFileLocation;
                        pSendCaptureImage->m_DateTime = pCaptureImage->m_DateTime;
                        pSendCaptureImage->m_nAcquisitionNumber = pCaptureImage->m_nAcquisitionNumber;
                        listSendCaptureImage.push_back(pSendCaptureImage);

                    }

                }
            }

        }
    }
    pCommonData->ReleaseListCaptureImage();

    if(listSendCaptureImage.size()==0)
    {
        ClearListAutoSend(listSendCaptureImage);
        usleep(50*1000);
        return;
    }


    WorklistResponseData rd = pCommonData->GetCurrentWorklistData();
    pCommonData->ReleaseCurrentWorklistData();
    //SetWorklistInfo(rd);



    vector<QString> listDCMFile;


    map<QString, QString> mapSendFile;





    vector<QString> listAutoSendFile;
    listAutoSendFile.clear();

    for(i=0 ; i<listSendCaptureImage.size() ; i++)
    {

        CAPTURE_IMAGE* pCaptureImage = listSendCaptureImage[i];

        QDateTime dtCapture = pCaptureImage->m_DateTime;

        QString strFileLocation = pCaptureImage->m_strImageFilename;
        int nAcquisitionNumber = pCaptureImage->m_nAcquisitionNumber;
        QString strAcquisitionDate = dtCapture.toString("yyyyMMdd");
        QString strAcquisitionTime = dtCapture.toString("HHmmss");


        if(nAcquisitionNumber==0 || strAcquisitionDate.size()==0 || strAcquisitionTime.size()==0)
        {
            //SDatabase::DeleteListReponse(listTmpSendFile);
            listSendFile.clear();
            qDebug() << "nAcqusitionNumber: " << nAcquisitionNumber << ", strAcquisitionDate: " << strAcquisitionDate << ", strAcquisitionTime: " << strAcquisitionTime;
            usleep(100);
            ClearListAutoSend(listSendCaptureImage);
            return;
        }

        QFile fileCheck(strFileLocation);
        if(fileCheck.exists()==true)
        {
            qint64 nFileSize =  fileCheck.size();
            //BMP File size is just 6220854
            if(nFileSize>850 && nFileSize<7230854)
            {
                listAutoSendFile.push_back(strFileLocation);
                listAcquisitionNumber.push_back(nAcquisitionNumber);
                listAcquisitionDate.push_back(strAcquisitionDate);
                listAcquisitionTime.push_back(strAcquisitionTime);
            }
        }
    }

    //usleep(500*1000);
    if(m_bExitAutoSend==true)
    {
        m_LockState.lock();
        m_nCurrentState = (m_nCurrentState & ~0x20);

        //SDatabase::DeleteListReponse(listTmpSendFile);
        listSendFile.clear();

        listAutoSendFile.clear();
        listAcquisitionNumber.clear();
        listAcquisitionDate.clear();
        listAcquisitionTime.clear();

        m_bExitAutoSend = false;

        m_LockState.unlock();

        ClearListAutoSend(listSendCaptureImage);
        return;
    }

    //SDatabase::DeleteListReponse(listTmpSendFile);
    listSendFile.clear();




    for(i=0 ; i<listAutoSendFile.size() ; i++)
    {
        QString strFile = listAutoSendFile[i];

        int nAcquisitionNumber = 0;

        QString strAcquisitionDate;
        QString strAcquisitionTime;

        strAcquisitionDate = listAcquisitionDate[i];
        strAcquisitionTime = listAcquisitionTime[i];

        nAcquisitionNumber = listAcquisitionNumber[i];

        rd.m_Image.strAcquisitionDate = strAcquisitionDate.toStdString().c_str();
        rd.m_Image.strAcquisitionTime = strAcquisitionTime.toStdString().c_str();

        rd.m_Image.strContentDate = rd.m_Image.strAcquisitionDate;
        rd.m_Image.strContentTime = rd.m_Image.strAcquisitionTime;

        rd.m_Series.nSeriesNumber = 1;

        rd.m_Image.nImageNumber = nAcquisitionNumber;

        CSDCMImage* pImage = new CSDCMImage;
        qint64 nSeconds = QDateTime::currentSecsSinceEpoch();
        QString strTmpDCMImage = QString("tmp%1_%2.dcm").arg(nAcquisitionNumber).arg(nSeconds);

        QFileInfo fi(strTmpDCMImage);
        if(fi.exists()==true)
        {
            QFile file(strTmpDCMImage);
            file.remove();
        }

        if(pCommonData->GetTransferSyntax()==0)
        {
            pImage->SetTransferSyntax(EXS_LittleEndianExplicit);
        }
        else
        {
            pImage->SetTransferSyntax(EXS_JPEGProcess14SV1);
        }

        if(rd.m_Series.nSeriesNumber==2)
        {
            rd.m_Series.nSeriesNumber = 1;
        }

        int nWriteError = pImage->WriteDCM(strFile, strTmpDCMImage, &rd, 1 /* AUTO SEND SERIES NUMBER ALWYAS 1 */, rd.m_Image.nImageNumber, false, "");

        if(nWriteError<0)
        {
            //WRITE_FUNCTION_LOG_PARAM("Error Write DCM\n");
        }
        else
        {
            //if(pImage->LoadDCMFile(strTmpDCMImage.toStdString().c_str())>=0)
            {
                mapSendFile.insert(map<QString, QString>::value_type(strTmpDCMImage, strFile));
                listDCMFile.push_back(strTmpDCMImage);
                //pSend->AddImageFilename(strTmpDCMImage.toStdString().c_str());
            }
            //else
            {
                //WRITE_FUNCTION_LOG_PARAM("Error Check Load DCM Files\n");
            }

        }

        delete pImage;

        if(m_bExitAutoSend==true)
        {
            break;
        }
    }
    listAcquisitionNumber.clear();
    listAcquisitionDate.clear();
    listAcquisitionTime.clear();

    if(m_bExitAutoSend==true)
    {
        m_LockState.lock();
        m_nCurrentState = (m_nCurrentState & ~0x20);
        m_LockState.unlock();

        listAutoSendFile.clear();

        m_bExitAutoSend = false;

        for(i=0 ; i<listDCMFile.size() ; i++)
        {
            QString strTmpDCMImage = listDCMFile[i];
            QFile::remove(strTmpDCMImage);

        }
        listDCMFile.clear();

        return;
    }


    vector<QString> listSendCompleteFile;
    vector<QString> listSendFail;

    bool bErrorSend = false;



    //CSCallbackSendProgress* pCallback = new CSCallbackSendProgress(true);
    m_pCallback->SetExecBackground(true);
    CSDCMSendImpl* pSend = new CSDCMSendImpl;
    SDCM_SERVER_INFO* pServerInfo = pCommonData->GetServerStorageInfo();
    pSend->SetServerInfo(pServerInfo->m_strServer_AETitle, pServerInfo->m_strClient_AETitle, pServerInfo->m_strServer_IP, pServerInfo->m_nServer_Port);
    pCommonData->ReleaseServerStorageInfo();

    int nTotalSend = listDCMFile.size();

    m_pCallback->Clear();
    m_pCallback->SetTotalCount(nTotalSend);

    if(listDCMFile.size()>0)
    {
        pSend->ClearFileList();
        for(i=0 ; i<listDCMFile.size() ; i++)
        {
            QString strSendDCMFile = listDCMFile[i];
            pSend->AddImageFilename(strSendDCMFile.toStdString().c_str());
        }

        pSend->SetCurrentFilenameList(1);

        int nTransferSyntax = EXS_LittleEndianExplicit;
        if(pCommonData->GetTransferSyntax()==1)
        {
            nTransferSyntax = EXS_JPEGProcess14SV1;
        }
        listSendCompleteFile = pSend->Send(m_pCallback, -1, 10, (E_TransferSyntax) nTransferSyntax);

        if(listDCMFile.size()!=listSendCompleteFile.size())
        {
            m_bSendFailed = true;
        }

        for(i=0 ; i<listAutoSendFile.size() ; i++)
        {
            QString strFileCheck = listAutoSendFile[i];
            bool bSendCheck = false;
            for(j=0 ; j<listSendCompleteFile.size() ; j++)
            {
                QString strSendCheck = listSendCompleteFile[j];
                map<QString, QString>::iterator it = mapSendFile.find(strSendCheck);
                QString strImageFile = it->second;
                if(strImageFile==strFileCheck)
                {
                    bSendCheck = true;
                    break;
                }
            }

            if(bSendCheck==false)
            {
                listSendFail.push_back(strFileCheck);
            }
        }

        if(listSendFail.size()>0)
        {
            for(i=0 ; i<listSendFail.size() ; i++)
            {
                QString strSendFailFile = listSendFail[i];
                WRITE_FUNCTION_LOG_PARAM("Send Failed: " + strSendFailFile);
            }
        }

        if(listSendCompleteFile.size()>0)
        {
            for(i=0 ; i<listSendCompleteFile.size() ; i++)
            {
                QString strSendFile = listSendCompleteFile[i];
                map<QString, QString>::iterator it = mapSendFile.find(strSendFile);
                QString strImageFile = it->second;
                {
                    m_ListAutoSendCompleteFile.push_back(strImageFile);

                    pListCaptureImage = pCommonData->GetListCaptureImage();
                    {
                        for(j=0 ; j<pListCaptureImage->size() ; j++)
                        {
                            CAPTURE_IMAGE* pCaptureImage = (*pListCaptureImage)[j];
                            if(pCaptureImage->m_strImageFilename==strImageFile)
                            {
                                pCaptureImage->m_bSend = true;
                                break;
                            }
                        }
                    }
                    pCommonData->ReleaseListCaptureImage();
                }




            }
        }
        else
        {
            m_bSendFailed = true;
        }

        for(i=0 ; i<listDCMFile.size() ; i++)
        {
            QString strTmpDCMImage = listDCMFile[i];
            QFile::remove(strTmpDCMImage);
        }
        listDCMFile.clear();
    }

    SAFE_DELETE(pSend);

    ClearListAutoSend(listSendCaptureImage);
}


void SThreadSendDICOM::SendImageList()
{
    //m_LockSendList.lock();


    m_pCallback->Clear();

    MainWindow* pMainWindow = MainWindow::GetMainWindow();
    CommonData* pCommonData = MainWindow::GetCommonData();



    int nCurrentWidgetID = pMainWindow->GetCurrentWidgetID();
    //CSCallbackSendProgress* pCallback = new CSCallbackSendProgress(false);
    m_pCallback->SetExecBackground(false);
    CSDCMSendImpl* pSend = new CSDCMSendImpl;
    WorklistResponseData rd;
    SDCM_SERVER_INFO* pServerInfo = pCommonData->GetServerStorageInfo();
    pSend->SetServerInfo(pServerInfo->m_strServer_AETitle, pServerInfo->m_strClient_AETitle, pServerInfo->m_strServer_IP, pServerInfo->m_nServer_Port);
    pCommonData->ReleaseServerStorageInfo();

    vector<QString> listDCMFile;
    int i=0;

    map<QString, QString> mapSendFile;

    {
        QString strPath = "./";
        QStringList filter;
        filter << "*.dcm";

        QDirIterator it(strPath, filter, QDir::AllEntries | QDir::NoSymLinks | QDir::NoDotAndDotDot, QDirIterator::NoIteratorFlags);
        while(it.hasNext()==true)
        {
            QString strDeleteFilename = it.next();
            QFile fileDelete(strDeleteFilename);
            if(fileDelete.exists()==true)
            {
                fileDelete.remove();
                fileDelete.close();
            }
        }
    }

    int nTotalSend = m_ListSendFile.size();


    m_pCallback->SetTotalCount(nTotalSend);

    if(nCurrentWidgetID==SWIDGET_VIEWER)
    {
        rd = pCommonData->GetCurrentHistoryWorklistData();

        vector<ImageSelect*>* pListHistoryImage = pCommonData->GetListHistoryImage();

        CSDCMImage* pImage = new CSDCMImage;

        for(i=0 ; i<nTotalSend && m_bExitSendImageList==false ; i++)
        {
            QStringList listData = m_ListSendFile[i];
            QString strFile = listData.at(0);
            int j=0;
            int nImageIndex = 0;
            ImageSelect* pHistoryImage = NULL;
            for(j=0 ; j<pListHistoryImage->size() ; j++)
            {
                pHistoryImage = (*pListHistoryImage)[j];
                if(strFile==pHistoryImage->m_strFile)
                {
                    nImageIndex = j+1;
                    break;
                }
            }

            qint64 nSeconds = QDateTime::currentSecsSinceEpoch();
            QString strTmpDCMImage = QString("tmp%1_%2.dcm").arg(pHistoryImage->m_nImageNumber).arg(nSeconds);

            rd.m_Image.strAcquisitionDate = pHistoryImage->m_strDate.toStdString().c_str();
            rd.m_Image.strAcquisitionTime = pHistoryImage->m_strTime.toStdString().c_str();

            rd.m_Image.strContentDate = rd.m_Image.strAcquisitionDate;
            rd.m_Image.strContentTime = rd.m_Image.strAcquisitionTime;

            rd.m_Image.nImageNumber = pHistoryImage->m_nImageNumber;
            rd.m_Image.nInstanceNumber = pHistoryImage->m_nImageNumber;
            rd.m_Image.nAcquisitionNumber = pHistoryImage->m_nImageNumber;

            rd.m_Series.nSeriesNumber = pHistoryImage->m_nSeriesNumber;
            rd.m_Study.nPatientIndex = pHistoryImage->m_nIndexPatient;
            rd.m_Series.nStudyIndex = pHistoryImage->m_nIndexStudy;
            rd.m_Image.nSeriesIndex = pHistoryImage->m_nIndexSeries;
            rd.m_Image.nImageIndex = pHistoryImage->m_nIndexImage;

            rd.m_Study.strStudyInstanceUID = pHistoryImage->m_strStudyInstanceUID.toStdString().c_str();
            rd.m_Series.strSeriesInstanceUID = pHistoryImage->m_strSeriesInstanceUID.toStdString().c_str();
            rd.m_Image.strInstanceUID = pHistoryImage->m_strImageInstanceUID.toStdString().c_str();

            rd.m_Image.bDelete = pHistoryImage->m_bDelete;
            rd.m_Image.nFileType = pHistoryImage->m_nType;
            rd.m_Image.strFileLocation = strFile.toStdString().c_str();
            rd.m_Patient.strPatientAge = pHistoryImage->m_strPatientAge.toStdString().c_str();

            //rd.m_Image.nImageNumber = pDatabase->GetAcquisitionNumber(strFile);


            if(pCommonData->GetTransferSyntax()==0)
            {
                pImage->SetTransferSyntax(EXS_LittleEndianExplicit);
            }
            else
            {
                pImage->SetTransferSyntax(EXS_JPEGProcess14SV1);
            }

            pImage->WriteDCM(strFile, strTmpDCMImage, &rd, pHistoryImage->m_nSeriesNumber, pHistoryImage->m_nImageNumber, false, "");
            //pImage->WriteDCM(strFile.toStdString().c_str(), pHistoryImage->m_strFile.toStdString(), pHistoryImage->m_strDate.toStdString(), pHistoryImage->m_strTime.toStdString(), &rd, 1, nImageIndex, false, "");

            pMainWindow->UpdateMakeDICOMInfo(i+1, nTotalSend);


            mapSendFile.insert(map<QString, QString>::value_type(strTmpDCMImage, strFile));

            listDCMFile.push_back(strTmpDCMImage);

            //pSend->AddImageFilename(strTmpDCMImage.toStdString().c_str());

        }

        SAFE_DELETE(pImage);

        pCommonData->ReleaseListHistoryImage();
    }
    else if(nCurrentWidgetID==SWIDGET_CAPTURE)
    {
        rd = pCommonData->GetCurrentWorklistData();
        pCommonData->ReleaseCurrentWorklistData();

        CSDCMImage* pImage = new CSDCMImage;

        for(i=0 ; i<m_ListSendFile.size() && m_bExitSendImageList==false ; i++)
        {
            QStringList listData = m_ListSendFile[i];

            QString strFileLocation = listData[0];
            QString strAcquisitionNumber = listData[1];
            QString strAcquisitionDate = listData[2];
            QString strAcquisitionTime = listData[3];

            int nAcquisitionNumber = strAcquisitionNumber.toInt();

            rd.m_Image.strAcquisitionDate = strAcquisitionDate.toStdString().c_str();
            rd.m_Image.strAcquisitionTime = strAcquisitionTime.toStdString().c_str();

            rd.m_Image.strContentDate = rd.m_Image.strAcquisitionDate;
            rd.m_Image.strContentTime = rd.m_Image.strAcquisitionTime;

            rd.m_Image.nImageNumber = nAcquisitionNumber;
            rd.m_Image.nAcquisitionNumber = nAcquisitionNumber;

            qint64 nSeconds = QDateTime::currentSecsSinceEpoch();
            QString strTmpDCMImage = QString("tmp%1_%2.dcm").arg(rd.m_Image.nAcquisitionNumber).arg(nSeconds);

            if(pCommonData->GetTransferSyntax()==0)
            {
                pImage->SetTransferSyntax(EXS_LittleEndianExplicit);
            }
            else
            {
                pImage->SetTransferSyntax(EXS_JPEGProcess14SV1);
            }

            if(rd.m_Series.nSeriesNumber!=1)
            {
                rd.m_Series.nSeriesNumber = 1;
            }

            //pImage->WriteDCM(strFile.toStdString().c_str(), strTmpDCMImage.toStdString().c_str(), &rd, rd.m_Series.nSeriesNumber, rd.m_Image.nImageNumber, false, "");
            pImage->WriteDCM(strFileLocation, strTmpDCMImage, &rd, 1 /* Series Number Always*/ , rd.m_Image.nImageNumber, false, "");

            usleep(10);


            pMainWindow->UpdateMakeDICOMInfo(i+1, nTotalSend);

            mapSendFile.insert(map<QString, QString>::value_type(strTmpDCMImage, strFileLocation));

            listDCMFile.push_back(strTmpDCMImage);

            //pSend->AddImageFilename(strTmpDCMImage.toStdString().c_str());

        }

        SAFE_DELETE(pImage);



    }

    vector<QString> listSendCompleteFile;

    bool bErrorSend = false;

    if(listDCMFile.size()>0)
    {

        pSend->ClearFileList();
        for(i=0 ; i<listDCMFile.size() ; i++)
        {
            QString strSendDCMFile = listDCMFile[i];
            pSend->AddImageFilename(strSendDCMFile.toStdString().c_str());
        }

        pSend->SetCurrentFilenameList(1);

        int nTransferSyntax = EXS_LittleEndianExplicit;
        if(pCommonData->GetTransferSyntax()==1)
        {
            nTransferSyntax = EXS_JPEGProcess14SV1;
        }
        listSendCompleteFile = pSend->Send(m_pCallback, -1, 10, (E_TransferSyntax) nTransferSyntax);

        if(listDCMFile.size()!=listSendCompleteFile.size())
        {
            int a=0;
        }

        if(listSendCompleteFile.size()>0)
        {
            for(i=0 ; i<listSendCompleteFile.size() ; i++)
            {
                QString strSendFile = listSendCompleteFile[i];
                map<QString, QString>::iterator it = mapSendFile.find(strSendFile);
                QString strImageFile = it->second;


                if(nCurrentWidgetID==SWIDGET_CAPTURE)
                {
                    m_ListImageSendCompleteFile.push_back(strImageFile);
                }
                else if(nCurrentWidgetID==SWIDGET_VIEWER)
                {
                    m_ListImageSendCompleteFile.push_back(strImageFile);
                }



            }

            if(listSendCompleteFile.size()!=listDCMFile.size())
            {
                UpdateSendFailed();
            }
            else
            {
                m_pCallback->ProgressComplete();
            }
        }
        else
        {
            UpdateSendFailed();
        }

        for(i=0 ; i<listDCMFile.size() ; i++)
        {
            QString strTmpDCMImage = listDCMFile[i];
            QFile file(strTmpDCMImage);
            if(file.exists()==true)
            {
                file.remove();
            }
        }
        listDCMFile.clear();
    }

    SAFE_DELETE(pSend);
    //SAFE_DELETE(pCallback);

    int nTryMaxCount = 10;
    int nTryCount = 0;
    while(m_LockState.tryLock(100)==false && nTryCount < nTryMaxCount)
    {
        usleep(10);
        nTryCount++;
    }

    if(nTryCount>=nTryMaxCount)
    {
        qDebug() << "Can not Lock SThreadSendDICOM!!";
    }
    //m_ListSendFile.clear();
    m_nCurrentState = (m_nCurrentState & ~0x40);

    if(m_bExecCheckSendImageList==true)
    {
        m_bExecCheckSendImageList = false;
        m_nCurrentState = (m_nCurrentState | 0x20);
    }
    m_LockState.unlock();

    if(m_bExitSendImageList==true)
    {
        m_bExitSendImageList = false;
    }



}


bool SThreadSendDICOM::IsExitSendImageList()
{
    return m_bExitSendImageList;
}

vector<QString> SThreadSendDICOM::GetListAutoSendCompleteFile()
{
    return m_ListAutoSendCompleteFile;
}

vector<QString> SThreadSendDICOM::GetListImageSendCompleteFile()
{
    return m_ListImageSendCompleteFile;
}

void SThreadSendDICOM::ClearListCompleteFile()
{
    m_ListAutoSendCompleteFile.clear();
    m_ListImageSendCompleteFile.clear();
}

void SThreadSendDICOM::ClearListAutoSend(vector<CAPTURE_IMAGE*>& listAutoSend)
{
    int i=0;
    for(i=0 ; i<listAutoSend.size() ; i++)
    {
        CAPTURE_IMAGE* pCaptureImage = listAutoSend[i];
        if(pCaptureImage!=NULL)
        {
            delete pCaptureImage;
            pCaptureImage = NULL;
        }
    }
    listAutoSend.clear();
}
