#include "qopenglbuffermenu.h"
using namespace SOPENGL;


QOpenGLBufferMenu::QOpenGLBufferMenu(QOpenGLContext *pContext, QSurfaceFormat surfaceformat, QObject *parent)
    :QOpenGLBufferBase(pContext, surfaceformat, parent)
{
    m_bRecording = false;
    m_bCapturing = false;
    m_bThumbnailView = false;
    m_pButtonRecord = NULL;
    m_pButtonCapture = NULL;
    m_pButtonThumbnailView = NULL;
}

QOpenGLBufferMenu::~QOpenGLBufferMenu()
{

}

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

    m_pButtonRecord = new QOpenGLTextureButton;
    m_pButtonCapture = new QOpenGLTextureButton;
    m_pButtonThumbnailView = new QOpenGLTextureButton;

    m_pButtonRecord->LoadImage(":/images/images/opengl_record_normal.png", ":/images/images/opengl_record_clicked.png");
    m_pButtonCapture->LoadImage(":/images/images/opengl_capture_normal.png", ":/images/images/opengl_capture_clicked.png");
    m_pButtonThumbnailView->LoadImage(":/images/images/opengl_thumbnail_hide.png", ":/images/images/opengl_thumbnail_view.png");

    m_rectRecord = QRect(m_rectDisplay.left(), m_rectDisplay.top()+m_rectDisplay.height()/2-128-20, 128, 128);
    m_rectRecordSmall = QRect(m_rectRecord.left()/2, m_rectRecord.top()/2, m_rectRecord.width()/2, m_rectRecord.height()/2);

    m_rectCapture = QRect(m_rectDisplay.left(), m_rectDisplay.top()+m_rectDisplay.height()/2, 128, 128);
    m_rectCaptureSmall = QRect(m_rectCapture.left()/2, m_rectCapture.top()/2, m_rectCapture.width()/2, m_rectCapture.height()/2);

    m_rectThumbnailView = QRect(m_rectDisplay.left(), m_rectDisplay.bottom()-128, 128, 128);
    m_rectThumbnailViewSmall = QRect(m_rectThumbnailView.left()/2, m_rectThumbnailView.top()/2, m_rectThumbnailView.width()/2, m_rectThumbnailView.height()/2);


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

        m_VBORecord.create();
        m_VBORecord.bind();

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

        m_VBORecord.release();
    }


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

        m_VBOCapture.create();
        m_VBOCapture.bind();

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

        m_VBOCapture.release();
    }


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

        m_VBOThumbnailView.create();
        m_VBOThumbnailView.bind();

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

        m_VBOThumbnailView.release();
    }
}

void QOpenGLBufferMenu::Paint()
{
    m_pShaderThumbnail->Bind();
    m_pShaderThumbnail->SetUniformValue();
    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 QOpenGLBufferMenu::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();

    m_VBOCapture.bind();
    m_pShaderThumbnail->SetAttributeBuffer();
    m_VBOCapture.release();

    if(m_bCapturing==false)
    {
        pTexture = m_pButtonCapture->GetTextureNormal();
    }
    else
    {
        pTexture = m_pButtonCapture->GetTextureClicked();
    }

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





    m_VBORecord.bind();
    m_pShaderThumbnail->SetAttributeBuffer();
    m_VBORecord.release();

    if(m_bRecording==false)
    {
        pTexture = m_pButtonRecord->GetTextureNormal();
    }
    else
    {
        pTexture = m_pButtonRecord->GetTextureClicked();
    }

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




    m_VBOThumbnailView.bind();
    m_pShaderThumbnail->SetAttributeBuffer();
    m_VBOThumbnailView.release();

    if(m_bThumbnailView==false)
    {
        pTexture = m_pButtonThumbnailView->GetTextureNormal();
    }
    else
    {
        pTexture = m_pButtonThumbnailView->GetTextureClicked();
    }

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




    m_pShaderThumbnail->DisableAttributeArray();

    m_pShaderThumbnail->Release();
}

void QOpenGLBufferMenu::CheckRegion(int nX, int nY)
{
    if(m_rectRecord.contains(nX, nY)==true)
    {
        m_bRecording = !m_bRecording;
        SendRecord(m_bRecording);
    }

    if(m_rectCapture.contains(nX, nY)==true)
    {
        m_bCapturing = !m_bCapturing;
        if(m_bCapturing==true)
        {
            SendSaveBitmap();
        }
    }

    if(m_rectThumbnailView.contains(nX, nY)==true)
    {
        m_bThumbnailView = !m_bThumbnailView;
        SendViewThumbnail(m_bThumbnailView);
    }
}

void QOpenGLBufferMenu::CheckRegionSmall(int nX, int nY)
{
    if(m_rectRecordSmall.contains(nX, nY)==true)
    {
        m_bRecording = !m_bRecording;
        SendRecord(m_bRecording);
    }

    if(m_rectCaptureSmall.contains(nX, nY)==true)
    {
        m_bCapturing = !m_bCapturing;

        if(m_bCapturing==true)
        {
            SendSaveBitmap();
        }
    }

    if(m_rectThumbnailViewSmall.contains(nX, nY)==true)
    {
        m_bThumbnailView = !m_bThumbnailView;
        SendViewThumbnail(m_bThumbnailView);
    }
}

void QOpenGLBufferMenu::SetCapture(bool bCapture)
{
    m_bCapturing = bCapture;
}
