#include "formimageselect.h"
#include "ui_formimageselect.h"

#include <QPainter>
#include <QPaintEvent>
#include <QMouseEvent>
#include <QPixelFormat>
#include <QtMath>

#include "mainwindow.h"
//#include <QImageData>

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

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

    //m_pImage = new QImage("/home/birdhead/test1.png");

    m_pImageSelect = NULL;
    m_pImage = NULL;

    //m_pImage = new QImage(1920, 1080, QImage::Format_RGB888);


    m_nIndexViewer = -1;

    Init();
}

FormImageSelect::~FormImageSelect()
{
    //SAFE_DELETE(m_pImage);
    delete ui;
}

void FormImageSelect::resizeEvent(QResizeEvent *event)
{
    WRITE_FUNCTION_LOG();

    UpdateImageSelect();
    Init();
}


void FormImageSelect::ClickSelect()
{
    WRITE_FUNCTION_LOG();

}

void FormImageSelect::paintEvent(QPaintEvent *event)
{


    QPainter painter(this);
    QSize nSize = size();
    int nPaddingLeft = 0;

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

    QColor colorBackground;

    painter.setRenderHint(QPainter::SmoothPixmapTransform);

    colorBackground = Qt::white;
    if(m_pImage==NULL)
    {
        painter.fillRect(rectDisplay, Qt::black);
        return;
    }




    painter.fillRect(rectDisplay, colorBackground);

    if(m_pImage!=NULL)
    {
        painter.fillRect(QRect(0, 0, nSize.width(), nSize.height()), Qt::black);
        /*
        if(m_pImage->width() < m_pImage->height())
        {

            nPaddingLeft += (nSize.width()-nSize.height())/2;
        }
        else
        {
            nPaddingLeft = 0;
        }
        m_rectDisplay = QRect(nPaddingLeft, 0, nSize.width()-nPaddingLeft*2, nSize.height());
        m_rectDisplayImage = m_rectDisplay;
        */
    }

    if(m_bUseZoom==true)
    {
        int nOffset = m_PointClicked.y() - m_PointCurrent.y();

        int nRectX = m_rectDisplay.x();
        int nRectY = m_rectDisplay.y();
        int nRectWidth = m_rectDisplay.width();
        int nRectHeight = m_rectDisplay.height();

        float fRatioTmp = (float)nSize.width() / (float)nSize.height();

        m_rectDisplayImage = QRect(nRectX+nOffset*fRatioTmp, nRectY+nOffset, nRectWidth-nOffset*2*fRatioTmp, nRectHeight-nOffset*2);
    }
    else if(m_bUseMove==true)
    {
        int nOffsetX = m_PointCurrent.x() - m_PointClicked.x();
        int nOffsetY = m_PointCurrent.y() - m_PointClicked.y();

        qDebug() << nOffsetX << ", " << nOffsetY << endl;

        int nRectX = m_rectDisplay.x();
        int nRectY = m_rectDisplay.y();
        int nRectWidth = m_rectDisplay.width();
        int nRectHeight = m_rectDisplay.height();

        float fRatioTmp = (float)nSize.width() / (float)nSize.height();

        m_rectDisplayImage = QRect(nRectX+nOffsetX, nRectY+nOffsetY, nRectWidth, nRectHeight);
    }

    painter.drawImage(m_rectDisplayImage, *m_pImage);


    QFont font("Roboto");
    font.setPixelSize(18);

    painter.setFont(font);

    painter.setPen(Qt::black);
    //QString strInfo;
    //strInfo = QString("Index: %1").arg(QString::number(m_nIndexViewer));
    //painter.drawText(rectDisplay, Qt::AlignLeft | Qt::AlignVCenter,  strInfo);

    if(m_pImageSelect!=NULL)
    {
        if(m_pImageSelect->m_nType==DISPLAY_VIDEO)
        {
            QRect rectDisplayVideo;
            int nWidth = rectDisplay.width() / 20;
            int nHeight = rectDisplay.height() / 20;
            rectDisplayVideo = QRect(nWidth*17, nHeight, nWidth*2, nHeight);

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

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

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




}

void FormImageSelect::SetIndexViewer(int nIndex)
{
    m_nIndexViewer = nIndex;
}


void FormImageSelect::SetImageSelect(ImageSelect* pImageSelect)
{
    //WRITE_FUNCTION_LOG();
    m_pImageSelect = pImageSelect;

    DisplayCheck();

    if(m_pImageSelect==NULL)
    {
        if(m_pImage!=NULL)
        {
            m_pImage->fill(QColor(0, 0, 0));
        }

        //m_pImage = NULL;
    }
    else
    {


        UpdateImageSelect(true);
    }


}


void FormImageSelect::UpdateImageSelect(bool bForce)
{
    //WRITE_FUNCTION_LOG();
    QSize nSize = size();

    bool bResize = false;
    /*
    if(m_pImage==NULL)
    {
        bResize = true;
    }
    else
    {
        if(m_pImage->size()!=nSize)
        {
            bResize = true;
        }
    }
    */

    if(m_pImageSelect!=NULL)
    {
        int nWidth = m_pImageSelect->GetWidth();
        int nHeight = m_pImageSelect->GetHeight();
        //QImage::Format fmt = (QImage::Format)m_pImageSelect->GetFormat();
        QImage::Format fmt = QImage::Format_RGB888;
        uint8_t* pImageDataSrc = m_pImageSelect->m_pImageData;

        if(m_pImage==NULL)
        {
            //m_pImage = new QImage(pImageDataSrc, nWidth, nHeight, fmt);
            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));
                }
            }




        }
        //m_pImage = m_pImageSelect->m_pImage;
    }
    else
    {
        if(m_pImage!=NULL)
        {
            m_pImage->fill(QColor(0, 0, 0));
        }
        //m_pImage = NULL;
    }

    return;
}

void FormImageSelect::on_pushButton_Select_clicked()
{
    WRITE_FUNCTION_LOG();
    if(m_pImageSelect==NULL)
    {
        return;
    }
    if(m_pImageSelect->m_bSelect==true)
    {
        m_pImageSelect->m_bSelect = false;
    }
    else if(m_pImageSelect->m_bSelect==false)
    {
        m_pImageSelect->m_bSelect = true;
    }

    DisplayCheck();
}

void FormImageSelect::DisplayCheck()
{
    //WRITE_FUNCTION_LOG();
    if(m_pImageSelect==NULL)
    {
        ui->pushButton_Select->setChecked(false);
        return;
    }

    ui->pushButton_Select->setChecked(m_pImageSelect->m_bSelect);
}

void FormImageSelect::mousePressEvent(QMouseEvent *event)
{
    WRITE_FUNCTION_LOG();
    qDebug() << "Pressed";

    //if(event->buttons()==Qt::RightButton)
    {
        m_PointClicked = event->pos();
        //grabMouse();
    }
}

void FormImageSelect::mouseReleaseEvent(QMouseEvent *event)
{
    WRITE_FUNCTION_LOG();
    //if(event->buttons()==Qt::RightButton || event->button()==Qt::RightButton)
    if(m_bUseMove==true || m_bUseZoom==true)
    {
        m_rectDisplay = m_rectDisplayImage;
        return;
    }
    //return;
    qDebug() << "Released";
    if(m_pImageSelect==NULL)
    {
        m_PointCurrent = event->pos();
        update();

        return;
    }

    if(m_pImageSelect->m_bSelect==true)
    {
        m_pImageSelect->m_bSelect = false;
    }
    else if(m_pImageSelect->m_bSelect==false)
    {
        m_pImageSelect->m_bSelect = true;
    }

    DisplayCheck();
}

void FormImageSelect::mouseMoveEvent(QMouseEvent *event)
{
    //if(event->buttons()==Qt::RightButton)
    {
        if(m_bUseZoom==true || m_bUseMove==true)
        {
            m_PointCurrent = event->pos();
            update();
        }

    }
}

void FormImageSelect::mouseDoubleClickEvent(QMouseEvent *event)
{
    qDebug() << "Double Clicked";

}

bool FormImageSelect::IsSelect()
{
    if(m_pImage==NULL)
    {
        return false;
    }

    return ui->pushButton_Select->isChecked();
}

void FormImageSelect::DisplayRotate()
{
    QImage image;

    QPoint center = m_pImage->rect().center();
    QMatrix matrix;
    matrix.translate(center.x(), center.y());
    matrix.rotate(90);

    image = m_pImage->transformed(matrix);

    delete m_pImage;
    m_pImage = new QImage(image);


    int nPaddingLeft = 0;
    QSize nSize = size();
    if(m_pImage->width() < m_pImage->height())
    {
        nPaddingLeft += (nSize.width()-nSize.height())/2;
    }
    else
    {
        nPaddingLeft = 0;
    }
    m_rectDisplay = QRect(nPaddingLeft, 0, nSize.width()-nPaddingLeft*2, nSize.height());

    update();
}

void FormImageSelect::DisplayFlipHorizontal()
{
    QImage image;
    image = m_pImage->mirrored(true, false);

    delete m_pImage;
    m_pImage = new QImage(image);

    update();
}

void FormImageSelect::DisplayFlipVertical()
{
    QImage image;
    image = m_pImage->mirrored(false, true);

    delete m_pImage;
    m_pImage = new QImage(image);

    update();
}



void FormImageSelect::Init()
{
    WRITE_FUNCTION_LOG();

    m_bUseZoom = false;
    m_bUseMove = false;
    m_PointClicked = QPoint(-1, -1);

    QSize nSize = size();
    m_rectDisplay = QRect(0, 0, nSize.width(), nSize.height());
    m_rectDisplayImage = m_rectDisplay;
}

void FormImageSelect::EnableZoom(bool bEnable)
{

    //Init();
    m_bUseZoom = bEnable;
    m_bUseMove = false;
}

bool FormImageSelect::IsEnableZoom()
{

    return m_bUseZoom;
}

void FormImageSelect::EnableMove(bool bEnable)
{
    //Init();
    m_bUseMove = bEnable;
    m_bUseZoom = false;
}

bool FormImageSelect::IsEnableMove()
{
    return m_bUseMove;
}



