#include "qopenglbufferthumbnail.h"
using namespace SOPENGL;

QOpenGLBufferThumbnail::QOpenGLBufferThumbnail(QOpenGLContext* pContext, QSurfaceFormat surfaceformat, QObject *parent) :
    QOpenGLBufferBase(pContext, surfaceformat, parent)
{
    m_pButtonNext = NULL;
    m_pButtonPrev = NULL;

    m_pThumbnailTextureList = new QList<QOpenGLTexture*>;
    m_pThumbnailFileList = new QList<QString>;

    m_bPrevClicked = false;
    m_bNextClicked = false;




}

QOpenGLBufferThumbnail::~QOpenGLBufferThumbnail()
{
    ResetThumbnailTexture();
    if(m_pThumbnailTextureList!=NULL)
    {
        delete m_pThumbnailTextureList;
        m_pThumbnailTextureList = NULL;
    }

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

void QOpenGLBufferThumbnail::ResetThumbnailTexture()
{
    m_pThumbnailFileList->clear();

    int nTexture = 0;
    nTexture = m_pThumbnailTextureList->size();

    QList<QOpenGLTexture*>::iterator it;
    for(it=m_pThumbnailTextureList->begin() ; it!=m_pThumbnailTextureList->end() ; ++it)
    {
        QOpenGLTexture* pTexture = *it;
        if(pTexture!=NULL)
        {
            pTexture->destroy();
            delete pTexture;
        }
    }

    m_pThumbnailTextureList->clear();
}

void QOpenGLBufferThumbnail::initializeGL()
{
    m_rectDisplay = QRect(0, 1080-128, 1920-128, 128);
    QOpenGLBufferBase::initializeGL();

    m_pButtonPrev = new QOpenGLTextureButton;
    m_pButtonNext = new QOpenGLTextureButton;

    m_pThumbnailHover = new QOpenGLTextureButton;

    m_pButtonPrev->LoadImage(":/images/images/left.png", ":/images/images/left_click.png");
    m_pButtonNext->LoadImage(":/images/images/right.png", ":/images/images/right_click.png");
    m_pThumbnailHover->LoadImage(":/images/images/thumbnail_hover.png", ":/images/images/thumbnail_select.png");



    m_rectPrev = QRect(0, m_rectDisplay.top(), 52, 128);
    m_rectNext = QRect(m_rectDisplay.right()-52, m_rectDisplay.top(), 52, 128);
    m_rectPrevSmall = QRect(m_rectPrev.left()/2, m_rectPrev.top()/2, m_rectPrev.width()/2, m_rectPrev.height()/2);
    m_rectNextSmall = QRect(m_rectNext.left()/2, m_rectNext.top()/2, m_rectNext.width()/2, m_rectNext.height()/2);

    int i=0;

    int nGabWidth = 0;
    nGabWidth = (m_rectDisplay.width()-52*2)/8;
    for(i=0 ; i<8 ; i++)
    {
        m_rectThumbnail[i] = QRect(52+i*nGabWidth, m_rectDisplay.top(), nGabWidth, m_rectDisplay.height());
        m_rectThumbnailSmall[i] = QRect(m_rectThumbnail[i].left()/2, m_rectThumbnail[i].top()/2, m_rectThumbnail[i].width()/2, m_rectThumbnail[i].height()/2);
        m_bThumbnailHover[i] = false;
    }



    for(i=0 ; i<8 ; i++)
    {
        if (!m_VBOTest[i].isCreated()) {

            MakeRectNormalWithOffset(m_rectThumbnail[i], QSize(m_rectDisplay.width(), m_rectDisplay.height()), QSize(m_rectDisplay.left(), m_rectDisplay.top()));

            m_VBOTest[i].create();
            m_VBOTest[i].bind();

            m_VBOTest[i].allocate(6 * 5 * sizeof(GLfloat));
            m_VBOTest[i].write(0, m_fNormalRect, 6*3*sizeof(GLfloat));
            m_VBOTest[i].write(6*3*sizeof(GLfloat), m_fNormalRectTexture, 6*2*sizeof(GLfloat));
            //m_VBOVideo.write(6*5*sizeof(GLfloat), afNormals, 6*3*sizeof(GLfloat));

            m_VBOTest[i].release();
        }
    }


    if (!m_VBOPrev.isCreated()) {
        MakeRectNormalWithOffset(m_rectPrev, QSize(m_rectDisplay.width(), m_rectDisplay.height()), QSize(m_rectDisplay.left(), m_rectDisplay.top()));


        m_VBOPrev.create();
        m_VBOPrev.bind();

        m_VBOPrev.allocate(6 * 5 * sizeof(GLfloat));
        m_VBOPrev.write(0, m_fNormalRect, 6*3*sizeof(GLfloat));
        m_VBOPrev.write(6*3*sizeof(GLfloat), m_fNormalRectTexture, 6*2*sizeof(GLfloat));
        //m_VBOVideo.write(6*5*sizeof(GLfloat), afNormals, 6*3*sizeof(GLfloat));

        m_VBOPrev.release();
    }

    if (!m_VBONext.isCreated()) {
        MakeRectNormalWithOffset(m_rectNext, QSize(m_rectDisplay.width(), m_rectDisplay.height()), QSize(m_rectDisplay.left(), m_rectDisplay.top()));


        m_VBONext.create();
        m_VBONext.bind();

        m_VBONext.allocate(6 * 5 * sizeof(GLfloat));
        m_VBONext.write(0, m_fNormalRect, 6*3*sizeof(GLfloat));
        m_VBONext.write(6*3*sizeof(GLfloat), m_fNormalRectTexture, 6*2*sizeof(GLfloat));
        //m_VBOVideo.write(6*5*sizeof(GLfloat), afNormals, 6*3*sizeof(GLfloat));

        m_VBONext.release();
    }
}

void QOpenGLBufferThumbnail::InitState()
{
    m_bPrevClicked = false;
    m_bNextClicked = false;

    int i=0;
    for(i=0 ; i<8 ; i++)
    {
        m_bThumbnailHover[i] = false;
    }
}

void QOpenGLBufferThumbnail::CheckRegion(int nX, int nY)
{
    if(m_rectPrev.contains(nX, nY)==true)
    {
        m_bPrevClicked = true;
    }
    else
    {
        m_bPrevClicked = false;
    }

    if(m_rectNext.contains(nX, nY)==true)
    {
        m_bNextClicked = true;
    }
    else
    {
        m_bNextClicked = false;
    }

    int i=0;
    for(i=0 ; i<8 ; i++)
    {
        if(m_rectThumbnail[i].contains(nX, nY)==true)
        {
            m_bThumbnailHover[i] = true;
        }
        else
        {
            m_bThumbnailHover[i] = false;
        }
    }
}

void QOpenGLBufferThumbnail::CheckRegionSmall(int nX, int nY)
{
    if(m_rectPrevSmall.contains(nX, nY)==true)
    {
        m_bPrevClicked = true;
    }
    else
    {
        m_bPrevClicked = false;
    }

    if(m_rectNextSmall.contains(nX, nY)==true)
    {
        m_bNextClicked = true;
    }
    else
    {
        m_bNextClicked = false;
    }

    int i=0;
    for(i=0 ; i<8 ; i++)
    {
        if(m_rectThumbnailSmall[i].contains(nX, nY)==true)
        {
            m_bThumbnailHover[i] = true;
        }
        else
        {
            m_bThumbnailHover[i] = false;
        }
    }

}

void QOpenGLBufferThumbnail::Paint()
{
    m_pShaderThumbnail->Bind();
    m_pShaderThumbnail->SetUniformValue();

    int nSec = m_StartTime.elapsed();
    float fY = (float)nSec/1000;
    if(nSec>500)
    {
        fY = 0.5;
    }
    m_pShaderThumbnail->Translate(0.0f, -0.5+fY, 0.0f);

    m_pShaderThumbnail->EnableAttributeArray();




    m_VBOThumbnail.bind();
    // In the buffer we first have 36 vertices (3 floats for each), then 36 texture
    // coordinates (2 floats for each), then 36 normals (3 floats for each).
    m_pShaderThumbnail->SetAttributeBuffer();
    m_VBOThumbnail.release();



    int nTextureID = m_pFBO->texture();
    glBindTexture(GL_TEXTURE_2D, nTextureID);
    glDrawArrays(GL_TRIANGLES, 0, 6);
    glBindTexture(GL_TEXTURE_2D, 0);






    m_pShaderThumbnail->DisableAttributeArray();

    m_pShaderThumbnail->Release();
}

void QOpenGLBufferThumbnail::PaintBuffer()
{
    QOpenGLTexture* pTexture = NULL;

    m_pContext->makeCurrent(m_pOffScreen);
    glViewport(0, 0, m_pFBO->width(), m_pFBO->height());

    //glFrontFace(GL_CW);
    //glCullFace(GL_FRONT);
    //glEnable(GL_CULL_FACE);
    glDisable(GL_DEPTH_TEST);
    glEnable(GL_BLEND);

    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

    m_pFBO->bind();

    glClearColor(1.0f, 1.0f, 1.0f, 0.3f);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);


    m_pShaderThumbnail->Bind();
    m_pShaderThumbnail->SetUniformValue();

    m_pShaderThumbnail->EnableAttributeArray();





    if(m_pThumbnailTextureList->size()>0)
    {
        int nThumbnailCount = m_pThumbnailTextureList->size();

        int i=0;
        for(i=0 ; i<nThumbnailCount && i<8 ; i++)
        {
            m_VBOTest[i].bind();
            m_pShaderThumbnail->SetAttributeBuffer();
            //glDrawArrays(GL_TRIANGLES, 0, 6);
            m_VBOTest[i].release();

            pTexture = m_pThumbnailTextureList->at(i);

            pTexture->bind();
            glDrawArrays(GL_TRIANGLES, 0, 6);
            pTexture->release();

            if(m_bThumbnailHover[i]==true)
            {
                pTexture = m_pThumbnailHover->GetTextureNormal();

                pTexture->bind();
                glDrawArrays(GL_TRIANGLES, 0, 6);
                pTexture->release();
            }

        }



    }



    m_VBOPrev.bind();
    m_pShaderThumbnail->SetAttributeBuffer();
    //glDrawArrays(GL_TRIANGLES, 0, 6);
    m_VBOPrev.release();

    if(m_bPrevClicked==true)
    {
        pTexture = m_pButtonPrev->GetTextureClicked();
    }
    else
    {
        pTexture = m_pButtonPrev->GetTextureNormal();
    }

    pTexture->bind();
    glDrawArrays(GL_TRIANGLES, 0, 6);
    pTexture->release();



    m_VBONext.bind();
    m_pShaderThumbnail->SetAttributeBuffer();
    //glDrawArrays(GL_TRIANGLES, 0, 6);
    m_VBONext.release();

    if(m_bNextClicked==true)
    {
        pTexture = m_pButtonNext->GetTextureClicked();
    }
    else
    {
        pTexture = m_pButtonNext->GetTextureNormal();
    }
    pTexture->bind();
    glDrawArrays(GL_TRIANGLES, 0, 6);
    pTexture->release();





    m_pShaderThumbnail->DisableAttributeArray();

    m_pShaderThumbnail->Release();

    m_pFBO->release();

    glFlush();

}



void QOpenGLBufferThumbnail::AddThumbnail(QString strFilenameThumbnail)
{
    m_pContext->makeCurrent(m_pOffScreen);
    QImage imageThumbnail(strFilenameThumbnail);
    QOpenGLTexture* pTexture = new QOpenGLTexture(imageThumbnail);
    m_pThumbnailTextureList->push_front(pTexture);
    m_pThumbnailFileList->push_front(strFilenameThumbnail);
    m_pContext->makeCurrent(NULL);
}

void QOpenGLBufferThumbnail::StartAnimation()
{
    m_StartTime.restart();
}

