#include "formcaptureimageselect.h"
#include "ui_formcaptureimageselect.h"

#include <QPainter>
#include <QPaintEvent>
#include <QResizeEvent>
#include <QWheelEvent>

#include "mainwindow.h"

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

    m_pCaptureImage = NULL;

    m_pImage = NULL;

    m_pParentWidget = parent;

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

    connect(this, SIGNAL(ThumbnailClicked(void*)), m_pParentWidget, SLOT(ThumbnailClicked(void*)));
}

FormCaptureImageSelect::~FormCaptureImageSelect()
{
    disconnect(this, SIGNAL(ThumbnailClicked(void*)), m_pParentWidget, SLOT(ThumbnailClicked(void*)));

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

    SAFE_DELETE(m_pImage);

    delete ui;
}

void FormCaptureImageSelect::Init()
{
    m_pCaptureImage = NULL;
}

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

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

    int nWidth = nSize.width() - nPaddingLeft;
    int nHeight = (int)(nWidth * (float)((float)nVideoHeight/(float)nVideoWidth) + 0.5f);

    QRect rectDisplay = QRect(nPaddingLeft, 0, nWidth, nHeight);

    QColor colorBackground;

    colorBackground = Qt::black;
    painter.setRenderHint(QPainter::SmoothPixmapTransform);
    painter.fillRect(rectDisplay, colorBackground);

    if(m_pCaptureImage!=NULL)
    {
        if(m_pCaptureImage->m_pImageSmall!=NULL)
        {
            QImage::Format format = m_pCaptureImage->m_pImageSmall->format();
            if(format!=QImage::Format_RGB888)
            {
                *m_pCaptureImage->m_pImageSmall = m_pCaptureImage->m_pImageSmall->convertToFormat(QImage::Format_RGB888);
            }
            painter.drawImage(rectDisplay, *m_pCaptureImage->m_pImageSmall);
        }
        else if(m_pCaptureImage->m_pImage!=NULL)
        {
            painter.drawImage(rectDisplay, *m_pCaptureImage->m_pImage);
        }
        else if(m_pCaptureImage->m_pImageData!=NULL)
        {
            int nWidth = m_pCaptureImage->GetWidth();
            int nHeight = m_pCaptureImage->GetHeight();
            QImage::Format fmt = QImage::Format_RGB888;
            uint8_t* pImageDataSrc = m_pCaptureImage->m_pImageData;

            if(m_pImage==NULL)
            {
                m_pImage = new QImage(nWidth, nHeight, fmt);
                uchar* pDataImage = m_pImage->bits();
                memcpy(pDataImage, pImageDataSrc, nWidth*nHeight*3);
            }
            else
            {
                if(nWidth>0 && nHeight>0)
                {
                    if(m_pImage->width()!=nWidth || m_pImage->height()!=nHeight)
                    {
                        delete m_pImage;
                        m_pImage = NULL;
                        //m_pImage = new QImage(pImageDataSrc, nWidth, nHeight, fmt);
                        m_pImage = new QImage(nWidth, nHeight, fmt);
                    }

                    if(pImageDataSrc!=NULL)
                    {
                        uchar* pDataImage = m_pImage->bits();
                        memcpy(pDataImage, pImageDataSrc, nWidth*nHeight*3);
                    }
                    else
                    {
                        m_pImage->fill(QColor(0, 0, 0));
                    }
                }
                else
                {
                    if(m_pImage!=NULL)
                    {
                        m_pImage->fill(QColor(0, 0, 0));
                    }
                }

            }

            painter.drawImage(rectDisplay, *m_pImage);
        }

        if(m_pCaptureImage->m_nType==DISPLAY_VIDEO)
        {
            QRect rectDisplayVideo;
            int nWidthDisplayVideo = rectDisplay.width() / 20;
            int nHeightDisplayVideo = rectDisplay.height() / 20;
            rectDisplayVideo = QRect(rectDisplay.left()+nWidthDisplayVideo*17, nHeightDisplayVideo, nWidthDisplayVideo*2, nHeightDisplayVideo);

            painter.fillRect(rectDisplayVideo, QColor(255, 0, 0));

            int nFontSize = nHeightDisplayVideo/2;
            if(nFontSize<10)
            {
                nFontSize = 10;
            }

            QFont font("Roboto");
            font.setPixelSize(nFontSize);
            font.setBold(true);
            painter.setFont(font);
            painter.drawText(rectDisplayVideo, Qt::AlignCenter,  "VIDEO");
        }
    }


}

void FormCaptureImageSelect::resizeEvent(QResizeEvent *event)
{

}

void FormCaptureImageSelect::mousePressEvent(QMouseEvent *event)
{

}

void FormCaptureImageSelect::wheelEvent(QWheelEvent *event)
{
    int nDelta = event->delta();
    if(nDelta<0)
    {
        SingleDown();
    }
    else if(nDelta>0)
    {
        SingleUp();
    }
}

void FormCaptureImageSelect::mouseReleaseEvent(QMouseEvent *event)
{
    if(m_pCaptureImage==NULL)
    {
        return;
    }

    if(m_pCaptureImage->m_bCheck==true)
    {
        //m_pCaptureImage->m_bCheck = false;
    }
    else
    {
        //m_pCaptureImage->m_bCheck = true;
    }

    //ui->pushButton_Select->setChecked(m_pCaptureImage->m_bCheck);

    ThumbnailClicked(m_pCaptureImage);
}

void FormCaptureImageSelect::ClickSelect()
{
    if(m_pCaptureImage==NULL)
    {
        return;
    }

    if(m_pCaptureImage->m_bCheck==true)
    {
        //m_pCaptureImage->m_bCheck = false;
    }
    else
    {
        //m_pCaptureImage->m_bCheck = true;
    }

    //ui->pushButton_Select->setChecked(m_pCaptureImage->m_bCheck);

}

void FormCaptureImageSelect::SetCaptureImage(CAPTURE_IMAGE* pCaptureImage)
{
    m_pCaptureImage = pCaptureImage;

    QString strIndex;
    bool bCheck = false;

    if(m_pCaptureImage!=NULL)
    {
        strIndex = QString::number(m_pCaptureImage->m_nIndex);
        bCheck = m_pCaptureImage->m_bCheck;
    }

    ui->pushButton_Select->setText(strIndex);
    ui->pushButton_Select->setChecked(bCheck);

}
