#include "formcheckcaptureselect.h"
#include "ui_formcheckcaptureselect.h"

#include <QPainter>
#include <QPaintEvent>
#include "mainwindow.h"

#include <QFileInfo>
#include <QDirIterator>

FormCheckCaptureSelect::FormCheckCaptureSelect(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::FormCheckCaptureSelect)
{
    ui->setupUi(this);

    m_pDialogSendProgress = new DialogProgress(this);

    m_pDisplayImage = NULL;
    m_pCurrentImage = NULL;
    QWidget* pFormWidget = parent->parentWidget();
    connect(this, SIGNAL(CloseSelectForm()), pFormWidget, SLOT(CloseSelectForm()));

    connect(ui->pushButton_Selection, SIGNAL(released()), this, SLOT(ClickSelect()));
}

FormCheckCaptureSelect::~FormCheckCaptureSelect()
{
    QWidget* pFormWidget = parentWidget()->parentWidget();
    disconnect(this, SIGNAL(CloseSelectForm()), pFormWidget, SLOT(CloseSelectForm()));

    disconnect(ui->pushButton_Selection, SIGNAL(released()), this, SLOT(ClickSelect()));

    if(m_pDialogSendProgress!=NULL)
    {
        delete m_pDialogSendProgress;
    }

    delete ui;
}

void FormCheckCaptureSelect::SetCurrentImage(CAPTURE_IMAGE* pData)
{
    if(m_pCurrentImage!=NULL)
    {
        SAFE_DELETE(m_pCurrentImage->m_pImage);
    }
    m_pCurrentImage = pData;
    if(m_pCurrentImage==NULL)
    {
        ui->pushButton_Selection->hide();
        ui->label_Info->hide();
        return;
    }
    else
    {
        if(m_pCurrentImage->m_pImage==NULL && m_pCurrentImage->m_strImageFilename.length()>0)
        {
            m_pCurrentImage->m_pImage = new QImage(m_pCurrentImage->m_strImageFilename);
        }
    }

    ui->pushButton_Selection->show();
    ui->pushButton_Selection->setChecked(pData->m_bCheck);

    int i=0;
    int nIndex = 0;
    int nTotal = 0;

    CommonData* pCommonData = MainWindow::GetCommonData();
    vector<CAPTURE_IMAGE*>* pListCaptureImage = pCommonData->GetListCaptureImage();
    {
        for(i=0 ; i<pListCaptureImage->size() ; i++)
        {
            CAPTURE_IMAGE* pImageCapture = (*pListCaptureImage)[i];
            if(pImageCapture->m_bDelete==false)
            {
                nIndex++;
            }
            if(m_pCurrentImage==pImageCapture)
            {
                break;
            }
        }


        for(i=0 ; i<pListCaptureImage->size() ; i++)
        {
            CAPTURE_IMAGE* pImageCapture = (*pListCaptureImage)[i];
            //if(pImageCapture->m_bDelete==false && pImageCapture->m_nType==DISPLAY_IMAGE)
            if(pImageCapture->m_bDelete==false)
            {
                nTotal++;
            }
        }
    }
    pCommonData->ReleaseListCaptureImage();

    QString strInfo;
    strInfo = QString("Image: %1/%2").arg(nIndex).arg(nTotal);
    ui->label_Info->show();
    ui->label_Info->setText(strInfo);

    if(nTotal==1)
    {
        ui->pushButton_Prev->hide();
        ui->pushButton_Next->hide();
    }
    else
    {
        if(nIndex==1)
        {
            ui->pushButton_Prev->hide();
            ui->pushButton_Next->show();
        }
        else if(nIndex==nTotal)
        {
            ui->pushButton_Prev->show();
            ui->pushButton_Next->hide();
        }
        else
        {
            ui->pushButton_Prev->show();
            ui->pushButton_Next->show();
        }


    }

    if(pData->m_nType==DISPLAY_VIDEO)
    {
        ui->label_Info_Video->show();
    }
    else if(pData->m_nType==DISPLAY_IMAGE)
    {
        ui->label_Info_Video->hide();
    }
}


void FormCheckCaptureSelect::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    QSize nSize = size();
    int nPaddingLeft = 0;

    CommonData* pCommonData = MainWindow::GetCommonData();
    int nVideoWidth = pCommonData->GetVideoWidth();
    int nVideoHeight = pCommonData->GetVideoHeight();

    float fRatio = (float)nVideoHeight / (float)nVideoWidth;

    QRect rectDisplay = QRect(nPaddingLeft, 0, nSize.width()-nPaddingLeft, nSize.height());
    QRect rectDisplayImage = QRect(0, 0, nSize.width(), fRatio*nSize.width());

    QColor colorBackground;

    painter.setRenderHint(QPainter::SmoothPixmapTransform);

    colorBackground = QColor(0xA6B1C2);
    //painter.fillRect(rectDisplay, colorBackground);

    if(m_pCurrentImage!=NULL)
    {
        if(m_pCurrentImage->m_pImage==NULL && m_pCurrentImage->m_strImageFilename.length()>0)
        {
            m_pCurrentImage->m_pImage = new QImage(m_pCurrentImage->m_strImageFilename);
        }
        painter.drawImage(rectDisplayImage, *m_pCurrentImage->m_pImage);
    }
    else
    {
        painter.fillRect(rectDisplayImage, QColor(0, 0, 0));
    }
    painter.end();
}

void FormCheckCaptureSelect::on_pushButton_Close_clicked()
{
    WRITE_FUNCTION_LOG();
    CloseSelectForm();
}

void FormCheckCaptureSelect::on_pushButton_Prev_clicked()
{
    WRITE_FUNCTION_LOG();
    int i=0;
    CAPTURE_IMAGE* pPrevImage = NULL;
    CAPTURE_IMAGE* pNewCurrentImage = NULL;

    CommonData* pCommonData = MainWindow::GetCommonData();
    vector<CAPTURE_IMAGE*>* pListCaptureImage = pCommonData->GetListCaptureImage();
    {
        for(i=0 ; i<pListCaptureImage->size() ; i++)
        {
            CAPTURE_IMAGE* pImageCapture = (*pListCaptureImage)[i];
            if(pImageCapture->m_bDelete==false)
            {
                if(m_pCurrentImage==pImageCapture)
                {
                    if(pPrevImage!=NULL)
                    {
                        pNewCurrentImage = pPrevImage;
                    }
                    break;
                }
                pPrevImage = pImageCapture;
            }

        }
    }
    pCommonData->ReleaseListCaptureImage();

    if(pNewCurrentImage!=NULL)
    {
        SetCurrentImage(pNewCurrentImage);
    }


    update();
}

void FormCheckCaptureSelect::on_pushButton_Next_clicked()
{
    WRITE_FUNCTION_LOG();
    int i=0;
    CAPTURE_IMAGE* pNextImage = NULL;
    CAPTURE_IMAGE* pNewCurrentImage = NULL;

    int nCurrentIndex = 0;
    int nFindIndex = 0;

    CommonData* pCommonData = MainWindow::GetCommonData();
    vector<CAPTURE_IMAGE*>* pListCaptureImage = pCommonData->GetListCaptureImage();
    {
        for(i=0 ; i<pListCaptureImage->size() ; i++)
        {
            CAPTURE_IMAGE* pImageCapture = (*pListCaptureImage)[i];
            if(pImageCapture->m_bDelete==false)
            {
                if(m_pCurrentImage==pImageCapture)
                {
                    nFindIndex = nCurrentIndex+1;
                    break;
                }
                nCurrentIndex++;
            }
        }

        nCurrentIndex = 0;
        for(i=0 ; i<pListCaptureImage->size() ; i++)
        {
            CAPTURE_IMAGE* pImageCapture = (*pListCaptureImage)[i];
            if(pImageCapture->m_bDelete==false)
            {
                if(nCurrentIndex==nFindIndex)
                {
                    pNewCurrentImage = pImageCapture;
                    break;
                }
                nCurrentIndex++;
            }
        }
    }
    pCommonData->ReleaseListCaptureImage();

    if(pNewCurrentImage!=NULL)
    {
        SetCurrentImage(pNewCurrentImage);
    }


    update();
}


void FormCheckCaptureSelect::ClickSelect()
{
    if(m_pCurrentImage!=NULL)
    {
        if(m_pCurrentImage->m_bCheck==true)
        {
            m_pCurrentImage->m_bCheck = false;
        }
        else
        {
            m_pCurrentImage->m_bCheck = true;
        }
    }
}

void FormCheckCaptureSelect::on_pushButton_Delete_clicked()
{
    WRITE_FUNCTION_LOG();
    int i=0;
    int nIndex = 0;

    vector<CAPTURE_IMAGE*>::iterator it;

    CAPTURE_IMAGE* pPrevImage = NULL;
    CAPTURE_IMAGE* pNewCurrentImage = NULL;
    //SDatabase* pDatabase = NULL;

    CommonData* pCommonData = MainWindow::GetCommonData();
    vector<CAPTURE_IMAGE*>* pListCaptureImage = pCommonData->GetListCaptureImage();
    {
        for(it=pListCaptureImage->begin() ; it!=pListCaptureImage->end() ; ++it)
        {
            CAPTURE_IMAGE* pImageCapture = *it;
            if(m_pCurrentImage==pImageCapture)
            {
                qDebug() << "Delete: Index(" << pImageCapture->m_nIndex << ")";
                if(pImageCapture->m_nType==DISPLAY_IMAGE)
                {
                    //pDatabase = pCommonData->GetDatabase();
                    //pDatabase->ImageDeleteFromAcqusitionTemp(pImageCapture->m_strImageFilename, true);
                    //pCommonData->ReleaseDatabase();
                }
                else if(pImageCapture->m_nType==DISPLAY_VIDEO)
                {
                    QString strFilename = pImageCapture->m_strImageFilename;

                    QFileInfo fileCheck(strFilename);
                    if(fileCheck.exists()==true)
                    {
                        QString strPath = fileCheck.path();
                        QString strFilename = fileCheck.fileName();

                        QStringList filter;
                        if(strFilename.size()>0)
                        {
                            strFilename = strFilename.left(strFilename.size()-4);
                            filter << strFilename + QString("*") + MOVIE_FILE_EXTENSION;
                        }

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

                    QFile fileDelete(strFilename);
                    if(fileDelete.exists()==true)
                    {
                        fileDelete.remove();
                        fileDelete.close();
                    }

                    strFilename.replace(".jpg", MOVIE_FILE_EXTENSION);
                    strFilename.replace(".png", MOVIE_FILE_EXTENSION);
                    strFilename.replace(".bmp", MOVIE_FILE_EXTENSION);

                    //pDatabase = pCommonData->GetDatabase();
                    //pDatabase->ImageDeleteFromAcqusitionTemp(strFilename, true);
                    //pCommonData->ReleaseDatabase();
                }

                pImageCapture->m_bDelete = true;
                //pListCaptureImage->erase(it);
                break;
            }

            if(pImageCapture->m_bDelete==false)
            {
                pPrevImage = pImageCapture;
            }

        }

        if(pPrevImage!=NULL)
        {
            pNewCurrentImage = pPrevImage;
        }
        else
        {
            int nTotalImage = 0;
            int i=0;
            for(i=0 ; i<pListCaptureImage->size() ; i++)
            {
                CAPTURE_IMAGE* pImageCapture = (*pListCaptureImage)[i];
                if(pImageCapture->m_bDelete==false)
                {
                    nTotalImage++;
                }
            }

            if(nTotalImage>0)
            {
                for(i=0 ; i<pListCaptureImage->size() ; i++)
                {
                    CAPTURE_IMAGE* pImageCapture = (*pListCaptureImage)[i];
                    if(pImageCapture->m_bDelete==false)
                    {
                        pPrevImage = pImageCapture;
                        break;
                    }
                }
                //pPrevImage = (*pListCaptureImage)[0];
            }

            pNewCurrentImage = pPrevImage;
        }

        for(it=pListCaptureImage->begin() ; it!=pListCaptureImage->end() ; ++it)
        {
            CAPTURE_IMAGE* pImageCapture = *it;
            nIndex++;

            if(pImageCapture->m_bDelete==false)
            {
                pImageCapture->m_nIndex = nIndex;
            }
            else
            {
                //pImageCapture->m_nIndex = -1;
                pImageCapture->m_nIndex = nIndex;
            }
        }
    }
    pCommonData->ReleaseListCaptureImage();

    //if(pNewCurrentImage!=NULL)
    {
        SetCurrentImage(pNewCurrentImage);
    }


    update();
}

void FormCheckCaptureSelect::on_pushButton_Send_Select_clicked()
{
    WRITE_FUNCTION_LOG();

    bool bExecuteSend = true;

    while(bExecuteSend==true)
    {
        QList<QStringList> listFile;

        CommonData* pCommonData = MainWindow::GetCommonData();

        if(m_pCurrentImage->m_nType==DISPLAY_IMAGE)
        {
            //`FileLocation`, `AcquisitionNumber`, `AcquisitionDate`, `AcquisitionTime`, `ImageDelete`, `ImageSend`

            QString strFile = m_pCurrentImage->m_strImageFilename;
            QString strAcquisitionNumber;
            QString strAcquisitionDate;
            QString strAcquisitionTime;
            QString strImageDelete;
            QString strImageSend;

            strAcquisitionNumber = QString("%1").arg(m_pCurrentImage->m_nAcquisitionNumber);
            strAcquisitionDate = m_pCurrentImage->m_DateTime.toString("yyyyMMdd"); //pImageSelect->m_strDate;
            strAcquisitionTime = m_pCurrentImage->m_DateTime.toString("HHmmss");//pImageSelect->m_strTime;
            if(m_pCurrentImage->m_bDelete==true)
            {
                strImageDelete = "1";
            }
            else
            {
                strImageDelete = "0";
            }

            if(m_pCurrentImage->m_bSend==true)
            {
                strImageSend = "1";
            }
            else
            {
                strImageSend = "0";
            }

            QStringList listData;
            listData.push_back(strFile);
            listData.push_back(strAcquisitionNumber);
            listData.push_back(strAcquisitionDate);
            listData.push_back(strAcquisitionTime);
            listData.push_back(strImageDelete);
            listData.push_back(strImageSend);

            listFile.push_back(listData);
        }

        if(listFile.size()==0)
        {
            return;
        }


        //pCommonData->GetThreadCaptureImage()->SendImage(listFile);
        SThreadSendDICOM* pThreadSendDICOM = pCommonData->GetThreadSendDICOM();
        pThreadSendDICOM->SetCheckSendImageList(listFile);

        if(m_pDialogSendProgress!=NULL)
        {
            delete m_pDialogSendProgress;
        }

        m_pDialogSendProgress = new DialogProgress(this);

        //m_pDialogSendProgress->Init(1);
        m_pDialogSendProgress->Init(0x11);
        m_pDialogSendProgress->exec();

        vector<QString> listImageSendCompleteFile = pThreadSendDICOM->GetListImageSendCompleteFile();
        if(listImageSendCompleteFile.size()>0)
        {
            QString strSendCompleteFile = listImageSendCompleteFile[0];
            if(strSendCompleteFile==m_pCurrentImage->m_strImageFilename)
            {
                m_pCurrentImage->m_bSend = true;
            }
        }

        int nRet = m_pDialogSendProgress->result();

        if(m_pDialogSendProgress->IsError()==false)
        {
            bExecuteSend = false;
        }
        else
        {
            if(nRet==QDialog::Rejected)
            {
                bExecuteSend = false;
            }
        }

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


        if(m_pDialogSendProgress!=NULL)
        {
            delete m_pDialogSendProgress;
            m_pDialogSendProgress = NULL;
        }
    }

}

void FormCheckCaptureSelect::on_pushButton_Send_SelectAll_clicked()
{
    WRITE_FUNCTION_LOG();

    int i=0;

    bool bExecuteSend = true;

    while(bExecuteSend==true)
    {
        QList<QStringList> listFile;
        CommonData* pCommonData = MainWindow::GetCommonData();
        vector<CAPTURE_IMAGE*>* pListCaptureImage = pCommonData->GetListCaptureImage();
        {
            for(i=0 ; i<pListCaptureImage->size() ; i++)
            {
                CAPTURE_IMAGE* pCaptureImage = (*pListCaptureImage)[i];
                if(pCaptureImage->m_bDelete==false && pCaptureImage->m_nType==DISPLAY_IMAGE)
                {
                    if(pCaptureImage->m_bCheck==true)
                    {
                        //`FileLocation`, `AcquisitionNumber`, `AcquisitionDate`, `AcquisitionTime`, `ImageDelete`, `ImageSend`

                        QString strFile = pCaptureImage->m_strImageFilename;
                        QString strAcquisitionNumber;
                        QString strAcquisitionDate;
                        QString strAcquisitionTime;
                        QString strImageDelete;
                        QString strImageSend;

                        strAcquisitionNumber = QString("%1").arg(pCaptureImage->m_nAcquisitionNumber);
                        strAcquisitionDate = pCaptureImage->m_DateTime.toString("yyyyMMdd"); //pImageSelect->m_strDate;
                        strAcquisitionTime = pCaptureImage->m_DateTime.toString("HHmmss");//pImageSelect->m_strTime;
                        if(pCaptureImage->m_bDelete==true)
                        {
                            strImageDelete = "1";
                        }
                        else
                        {
                            strImageDelete = "0";
                        }

                        if(pCaptureImage->m_bSend==true)
                        {
                            strImageSend = "1";
                        }
                        else
                        {
                            strImageSend = "0";
                        }

                        QStringList listData;
                        listData.push_back(strFile);
                        listData.push_back(strAcquisitionNumber);
                        listData.push_back(strAcquisitionDate);
                        listData.push_back(strAcquisitionTime);
                        listData.push_back(strImageDelete);
                        listData.push_back(strImageSend);

                        listFile.push_back(listData);
                    }
                }

            }
        }
        pCommonData->ReleaseListCaptureImage();

        if(listFile.size()==0)
        {
            return;
        }

        //pCommonData->GetThreadCaptureImage()->SendImage(listFile);
        SThreadSendDICOM* pThreadSendDICOM = pCommonData->GetThreadSendDICOM();
        pThreadSendDICOM->SetCheckSendImageList(listFile);

        if(m_pDialogSendProgress!=NULL)
        {
            delete m_pDialogSendProgress;
        }

        m_pDialogSendProgress = new DialogProgress(this);

        //m_pDialogSendProgress->Init(1);
        m_pDialogSendProgress->Init(0x11);
        m_pDialogSendProgress->exec();

        vector<QString> listImageSendCompleteFile = pThreadSendDICOM->GetListImageSendCompleteFile();

        pListCaptureImage = pCommonData->GetListCaptureImage();
        {
            for(i=0 ; i<pListCaptureImage->size() ; i++)
            {
                CAPTURE_IMAGE* pCaptureImage = (*pListCaptureImage)[i];
                int j=0;
                for(j=0 ; j<listImageSendCompleteFile.size() ; j++)
                {
                    QString strImageSendCompleteFile = listImageSendCompleteFile[j];
                    if(pCaptureImage->m_strImageFilename==strImageSendCompleteFile)
                    {
                        pCaptureImage->m_bSend = true;
                    }

                }
            }
        }
        pCommonData->ReleaseListCaptureImage();



        int nRet = m_pDialogSendProgress->result();

        if(m_pDialogSendProgress->IsError()==false)
        {
            bExecuteSend = false;
        }
        else
        {
            if(nRet==QDialog::Rejected)
            {
                bExecuteSend = false;
            }
        }

        if(m_pDialogSendProgress!=NULL)
        {
            delete m_pDialogSendProgress;
            m_pDialogSendProgress = NULL;
        }

        //SDatabase::DeleteListReponse(listFile);
        listFile.clear();
    }


}

void FormCheckCaptureSelect::UpdateMakeDICOMInfo(int nCount, int nTotalCount)
{
    m_pDialogSendProgress->DisplayInfo(nCount, nTotalCount);

    QString strInfo = QString("DICOM Make: %1 / %2").arg(nCount).arg(nTotalCount);
    m_pDialogSendProgress->DisplayInfo(strInfo);
}


void FormCheckCaptureSelect::UpdateSendInfo(int nCount, int nTotalCount)
{
    m_pDialogSendProgress->DisplayInfo(nCount, nTotalCount);

    QString strInfo = QString("%1 / %2").arg(nCount).arg(nTotalCount);
    m_pDialogSendProgress->DisplayInfo(strInfo);
}

void FormCheckCaptureSelect::UpdateSendComplete()
{
    m_pDialogSendProgress->DisplayInfo(-1, 100);

    QString strInfo = QString("Send Complete");
    m_pDialogSendProgress->DisplayInfo(strInfo);

    //QThread::msleep(1000);

    //m_pDialogSendProgress->SendComplete();
    //m_pDialogSendProgress->accept();
}

void FormCheckCaptureSelect::UpdateSendFailed()
{
    if(m_pDialogSendProgress!=NULL)
    {
        m_pDialogSendProgress->DisplayInfo(-1, 100);

        m_pDialogSendProgress->DisplayInfo(-1);
    }

}


void FormCheckCaptureSelect::Init()
{
    m_pCurrentImage = NULL;
}
