#include "qvideowidget_uyvy.h"
#include "qvideoshader.h"
#include "mainwindow.h"

#include <QSurfaceFormat>

QVideoWidget_UYVY::QVideoWidget_UYVY(QWidget* parent, Qt::WindowFlags f):QVideoWidget(parent, f)
{
    CommonData* pCommonData = MainWindow::GetCommonData();
    int nVideoWidth = pCommonData->GetVideoWidth();
    int nVideoHeight = pCommonData->GetVideoHeight();

    m_nTextureWidth = nVideoWidth;
    m_nTextureHeight = nVideoHeight;
}

QVideoWidget_UYVY::~QVideoWidget_UYVY()
{

}

void QVideoWidget_UYVY::initializeGL()
{
    QOpenGLContext * pCurrent = QOpenGLContext::currentContext();


    QSurfaceFormat format;
    format.setVersion(3, 0);
    format.setDepthBufferSize(24);

    QSurfaceFormat::setDefaultFormat(format);


    initializeOpenGLFunctions();


    QString strGLSLVersion = reinterpret_cast<const char*>(glGetString(GL_SHADING_LANGUAGE_VERSION));
    qDebug("glslVer: %s\n",strGLSLVersion);

    int nGLSLVersion = 30;
    if(strGLSLVersion.contains("3.1")==true)
    {
        nGLSLVersion = 31;
    }
    else if(strGLSLVersion.contains("3.2")==true)
    {
        nGLSLVersion = 32;
    }

    setUpdateBehavior(QOpenGLWidget::NoPartialUpdate);
    //setUpdateBehavior(QOpenGLWidget::PartialUpdate);

    GLuint nFrameBufferObject = defaultFramebufferObject();

    int i=0;

    for(i=0 ; i<18 ; i++)
    {
        m_fNormalRect[i] = -1;
    }

    GLfloat afTexCoord[] = {
        0.0f,1.0f, 1.0f,1.0f, 0.0f,0.0f,
                    1.0f,0.0f, 0.0f,0.0f, 1.0f,1.0f
    };

    memcpy(m_fNormalRectTexture, afTexCoord, sizeof(GLfloat)*2*6);

    m_nTmpDisplayType = 0;


    CommonData* pCommonData = MainWindow::GetCommonData();


    int nWidth = pCommonData->GetVideoWidth();
    int nHeight = pCommonData->GetVideoHeight();



    //nWidth = m_nTextureWidth;
    //nHeight = m_nTextureHeight;



    m_pVideoShader = new QVideoShader;
    m_pVideoShader->CreateTexture(nWidth*2, nHeight, QOpenGLTexture::Target2D, QOpenGLTexture::R8_UNorm);

    char *pSrcVertex = NULL;

    if(nGLSLVersion==30)
    {
        pSrcVertex =
                "#version 300 es\n"
                "attribute highp vec4 vertex;\n"
                "attribute highp vec4 texCoord;\n"
                "uniform mediump mat4 matrix;\n"
                "varying highp vec4 texc;\n"
                "void main(void)\n"
                "{\n"
                "    gl_Position = matrix * vertex;\n"
                "    texc = texCoord;\n"
                "}\n";
    }
    else
    {
        pSrcVertex =
                "#version 300 es\n"
                "in highp vec4 vertex;\n"
                "in highp vec4 texCoord;\n"
                "uniform mediump mat4 matrix;\n"
                "out highp vec4 texc;\n"
                "void main(void)\n"
                "{\n"
                "    gl_Position = matrix * vertex;\n"
                "    texc = texCoord;\n"
                "}\n";
    }


    /*
    char *pSrcFragmentCaptureEnvent =
            "#version 300 es\n"
            "in highp vec4 texc;\n"
            "uniform sampler2D tex;\n"
            "out lowp vec4 FragColor;\n"
            "uniform lowp int nImageWidth;\n"
            "uniform lowp int nImageHeight;\n"
            "void main(void)\n"
            "{\n"
            "   int nX=0;\n"
            "   int nY=0;\n"
            "   highp float fX = 0.0;\n"
            "   highp float fY = 0.0;\n"
            "   int nInternalIndex = 0;\n"
            "   highp float fImageWidth = float(nImageWidth);\n"
            "   highp float fImageHeight = float(nImageHeight);\n"
            "   fX = texc.s*fImageWidth;\n"
            "   fY = texc.t*fImageHeight;\n"
            "   nX = int(fX);\n"
            "   nY = int(fY);\n"
            "   int nStartX = int((int(nX/4)) * 4);\n"
            "   if(nStartX%2==1) return;\n"
            "   int nIndexY = nStartX + 1 + int(mod(float(nX/2), 2.0)) * 2;\n"
            "   int nIndexU = nStartX;\n"
            "   int nIndexV = nStartX+2;\n"
            //"   if(nX<4) { FragColor = vec4(0.0, 0.0, 0.0, 1.0); return; }\n"
            "   fX = float((nX/2)*2+1)/(fImageWidth);\n"
            "   fY = float(nY)/(fImageHeight);\n"
            "   highp vec4 colorY = texture2D(tex, vec2(float(nIndexY)/(fImageWidth)+0.5/fImageWidth,fY+0.5/fImageHeight));\n"
            "   highp vec4 colorU = texture2D(tex, vec2(float(nIndexU)/(fImageWidth)+0.5/fImageWidth,fY+0.5/fImageHeight));\n"
            "   highp vec4 colorV = texture2D(tex, vec2(float(nIndexV)/(fImageWidth)+0.5/fImageWidth,fY+0.5/fImageHeight));\n"
            "   highp float fColorY = colorY.r;\n"
            "   highp float fColorU = colorU.r;\n"
            "   highp float fColorV = colorV.r;\n"
            //"   FragColor = vec4(fColorY, fColorU, fColorV, 1.0); return;\n"


            "   highp float fR = fColorY;\n"
            "   highp float fG = fColorY;\n"
            "   highp float fB = fColorY;\n"

            "   fColorY=1.1643*(fColorY-0.0625);"
            "   fColorU=fColorU-0.5;"
            "   fColorV=fColorV-0.5;"

            "   fR=clamp(fColorY+1.5958*fColorV, 0.0, 1.0);"
            "   fG=clamp(fColorY-0.39173*fColorU-0.81290*fColorV, 0.0, 1.0);"
            "   fB=clamp(fColorY+2.017*fColorU, 0.0, 1.0);"

            "   FragColor = vec4(fR, fG, fB, 1.0);\n"
            "}\n";
*/

    char *pSrcFragmentCaptureEnvent =
            "#version 300 es\n"
            "in highp vec4 texc;\n"
            "uniform sampler2D tex;\n"
            "out lowp vec4 FragColor;\n"
            "uniform lowp int nImageWidth;\n"
            "uniform lowp int nImageHeight;\n"
            "void main(void)\n"
            "{\n"
            "   int nX=0;\n"
            "   int nY=0;\n"
            "   highp float fX = 0.0;\n"
            "   highp float fY = 0.0;\n"
            //"   highp float fTextureWidth = 1920.0;\n"
            //"   highp float fTextureHeight = 1080.0;\n"
            "   highp float fImageWidth = float(nImageWidth);\n"
            "   highp float fImageHeight = float(nImageHeight);\n"
            "   highp float fX1 = texc.s * 1280.0;\n"
            "   highp float fY1 = texc.t * 720.0;\n"

            "   fX = texc.s*fImageWidth;\n"
            "   fY = texc.t*fImageHeight;\n"
            "   nX = int(fX);\n"
            "   nY = int(fY);\n"

            //"   int nX1 = int(fX1);\n"
            //"   int nY1 = int(fY1);\n"
            //"   highp float fNewX = float(nX1) * 1.5;\n"
            //"   highp float fNewY = float(nY1) * 1.5;\n"
            //"   highp float fGabX = fNewX - float(int(fNewX));\n"
            //"   highp float fGabY = fNewY - float(int(fNewY));\n"
            //"   if(fGabY==0.5)\n"
            //"   {\n"
            //"       FragColor = vec4(0, 0, 0, 1.0);\n"
            //"       return;\n"
            //"   }\n"
            //"   else\n"
            //"   {\n"
            //"       FragColor = vec4(1.0, 1.0, 1.0, 1.0);\n"
            //"       return;\n"
            //"     }\n"
            "   int nStartX = 0;\n"
            "   int nIndexY = 0;\n"
            "   int nIndexU = 0;\n"
            "   int nIndexV = 0;\n"
            "   highp vec4 colorY = vec4(0.0, 0.0, 0.0, 0.0);\n"
            "   highp vec4 colorU = vec4(0.0, 0.0, 0.0, 0.0);\n"
            "   highp vec4 colorV = vec4(0.0, 0.0, 0.0, 0.0);\n"
            "   highp float fColorY = 0.0;\n"
            "   highp float fColorU = 0.0;\n"
            "   highp float fColorV = 0.0;\n"
            "   highp float fR1 = 0.0;\n"
            "   highp float fG1 = 0.0;\n"
            "   highp float fB1 = 0.0;\n"
            "   highp float fR2 = 0.0;\n"
            "   highp float fG2 = 0.0;\n"
            "   highp float fB2 = 0.0;\n"
            "   highp float fR3 = 0.0;\n"
            "   highp float fG3 = 0.0;\n"
            "   highp float fB3 = 0.0;\n"
            "   highp float fR4 = 0.0;\n"
            "   highp float fG4 = 0.0;\n"
            "   highp float fB4 = 0.0;\n"

            "   nStartX = int((int(nX/4)) * 4);\n"
            "   nIndexY = nStartX + 1 + int(mod(float(nX/2), 2.0)) * 2;\n"
            "   nIndexU = nStartX;\n"
            "   nIndexV = nStartX+2;\n"
            "   fX = float((nX/2)*2+1)/(fImageWidth);\n"
            "   fY = float(nY)/(fImageHeight);\n"
            "   colorY = texture2D(tex, vec2(float(nIndexY)/(fImageWidth)+0.5/fImageWidth,fY));\n"
            "   colorU = texture2D(tex, vec2(float(nIndexU)/(fImageWidth)+0.5/fImageWidth,fY));\n"
            "   colorV = texture2D(tex, vec2(float(nIndexV)/(fImageWidth)+0.5/fImageWidth,fY));\n"
            "   fColorY = colorY.r;\n"
            "   fColorU = colorU.r;\n"
            "   fColorV = colorV.r;\n"
            "   fColorY=1.1643*(fColorY-0.0625);"
            "   fColorU=fColorU-0.5;"
            "   fColorV=fColorV-0.5;"
            "   fR1=clamp(fColorY+1.5958*fColorV, 0.0, 1.0);"
            "   fG1=clamp(fColorY-0.39173*fColorU-0.81290*fColorV, 0.0, 1.0);"
            "   fB1=clamp(fColorY+2.017*fColorU, 0.0, 1.0);"


            "   nStartX = int((int((nX+1)/4)) * 4);\n"
            "   nIndexY = nStartX + 1 + int(mod(float((nX+1)/2), 2.0)) * 2;\n"
            "   nIndexU = nStartX;\n"
            "   nIndexV = nStartX+2;\n"
            "   fX = float((nX/2)*2+1)/(fImageWidth);\n"
            "   fY = float(nY)/(fImageHeight);\n"
            "   colorY = texture2D(tex, vec2(float(nIndexY)/(fImageWidth)+0.5/fImageWidth,fY+0.5/fImageHeight));\n"
            "   colorU = texture2D(tex, vec2(float(nIndexU)/(fImageWidth)+0.5/fImageWidth,fY+0.5/fImageHeight));\n"
            "   colorV = texture2D(tex, vec2(float(nIndexV)/(fImageWidth)+0.5/fImageWidth,fY+0.5/fImageHeight));\n"
            "   fColorY = colorY.r;\n"
            "   fColorU = colorU.r;\n"
            "   fColorV = colorV.r;\n"
            "   fColorY = colorY.r;\n"
            "   fColorU = colorU.r;\n"
            "   fColorV = colorV.r;\n"
            "   fColorY=1.1643*(fColorY-0.0625);"
            "   fColorU=fColorU-0.5;"
            "   fColorV=fColorV-0.5;"
            "   fR2=clamp(fColorY+1.5958*fColorV, 0.0, 1.0);"
            "   fG2=clamp(fColorY-0.39173*fColorU-0.81290*fColorV, 0.0, 1.0);"
            "   fB2=clamp(fColorY+2.017*fColorU, 0.0, 1.0);"


            "   nStartX = int((int(nX/4)) * 4);\n"
            "   nIndexY = nStartX + 1 + int(mod(float(nX/2), 2.0)) * 2;\n"
            "   nIndexU = nStartX;\n"
            "   nIndexV = nStartX+2;\n"
            "   fX = float((nX/2)*2+1)/(fImageWidth);\n"
            "   fY = float(nY+1)/(fImageHeight);\n"
            "   colorY = texture2D(tex, vec2(float(nIndexY)/(fImageWidth)+0.5/fImageWidth,fY));\n"
            "   colorU = texture2D(tex, vec2(float(nIndexU)/(fImageWidth)+0.5/fImageWidth,fY));\n"
            "   colorV = texture2D(tex, vec2(float(nIndexV)/(fImageWidth)+0.5/fImageWidth,fY));\n"
            "   fColorY = colorY.r;\n"
            "   fColorU = colorU.r;\n"
            "   fColorV = colorV.r;\n"
            "   fColorY=1.1643*(fColorY-0.0625);"
            "   fColorU=fColorU-0.5;"
            "   fColorV=fColorV-0.5;"
            "   fR3=clamp(fColorY+1.5958*fColorV, 0.0, 1.0);"
            "   fG3=clamp(fColorY-0.39173*fColorU-0.81290*fColorV, 0.0, 1.0);"
            "   fB3=clamp(fColorY+2.017*fColorU, 0.0, 1.0);"

            "   nStartX = int((int((nX+1)/4)) * 4);\n"
            "   nIndexY = nStartX + 1 + int(mod(float((nX+1)/2), 2.0)) * 2;\n"
            "   nIndexU = nStartX;\n"
            "   nIndexV = nStartX+2;\n"
            "   fX = float((nX/2)*2+1)/(fImageWidth);\n"
            "   fY = float(nY+1)/(fImageHeight);\n"
            "   colorY = texture2D(tex, vec2(float(nIndexY)/(fImageWidth)+0.5/fImageWidth,fY));\n"
            "   colorU = texture2D(tex, vec2(float(nIndexU)/(fImageWidth)+0.5/fImageWidth,fY));\n"
            "   colorV = texture2D(tex, vec2(float(nIndexV)/(fImageWidth)+0.5/fImageWidth,fY));\n"
            "   fColorY = colorY.r;\n"
            "   fColorU = colorU.r;\n"
            "   fColorV = colorV.r;\n"
            "   fColorY=1.1643*(fColorY-0.0625);"
            "   fColorU=fColorU-0.5;"
            "   fColorV=fColorV-0.5;"
            "   fR4=clamp(fColorY+1.5958*fColorV, 0.0, 1.0);"
            "   fG4=clamp(fColorY-0.39173*fColorU-0.81290*fColorV, 0.0, 1.0);"
            "   fB4=clamp(fColorY+2.017*fColorU, 0.0, 1.0);"


            //"   highp float fR = fR1;\n"
            //"   highp float fG = fG1;\n"
            //"   highp float fB = fB1;\n"
            //"   fR = fR1*(1.0-fGabX)*(1.0-fGabY)+fR2*(fGabX)*(1.0-fGabY)+fR3*(1.0-fGabX)*(fGabY)+fR4*(fGabX)*(fGabY);\n"
            //"   fG = fG1*(1.0-fGabX)*(1.0-fGabY)+fG2*(fGabX)*(1.0-fGabY)+fG3*(1.0-fGabX)*(fGabY)+fG4*(fGabX)*(fGabY);\n"
            //"   fB = fB1*(1.0-fGabX)*(1.0-fGabY)+fB2*(fGabX)*(1.0-fGabY)+fB3*(1.0-fGabX)*(fGabY)+fB4*(fGabX)*(fGabY);\n"

            "   highp float fR = fR1*0.25+fR2*0.25+fR3*0.25+fR4*0.25;\n"
            "   highp float fG = fG1*0.25+fG2*0.25+fG3*0.25+fG4*0.25;\n"
            "   highp float fB = fB1*0.25+fB2*0.25+fB3*0.25+fB4*0.25;\n"

            //"   if(fGabY==0.0 && fGabX==0.0)\n"
            //"   {\n"
            //"       fR = fR1*0.25+fR2*0.25+fR3*0.25+fR4*0.25;\n"
            //"       fG = fG1*0.25+fG2*0.25+fG3*0.25+fG4*0.25;\n"
            //"       fB = fB1*0.25+fB2*0.25+fB3*0.25+fB4*0.25;\n"
            //"   }\n"



            "   FragColor = vec4(fR, fG, fB, 1.0);\n"
            "}\n";



    m_pVideoShader->CreateShader((const char*)pSrcVertex, (const char*)pSrcFragmentCaptureEnvent);
    m_pVideoShader->CreateGeometry();





    if (!m_VBOVideo.isCreated()) {
        //float fRatioWidth = 1572.0f/1920.0f;
        float fRatioWidth = 1.0;
        GLfloat afVertices[] = {
            -1, -fRatioWidth, -1,
            1,-fRatioWidth,-1,

            -1,fRatioWidth,-1,
            1, fRatioWidth,-1,

            -1,fRatioWidth,-1,
            1,-fRatioWidth,-1
        };

        GLfloat afTexCoord[] = {
            0.0f,1.0f, 1.0f,1.0f, 0.0f,0.0f,
                        1.0f,0.0f, 0.0f,0.0f, 1.0f,1.0f
        };

        GLfloat afNormals[] = {

            0,0,1, 0,0,1, 0,0,1,
            0,0,1, 0,0,1, 0,0,1
        };

        m_VBOVideo.create();
        m_VBOVideo.bind();

        m_VBOVideo.allocate(6 * 5 * sizeof(GLfloat));
        m_VBOVideo.write(0, afVertices, 6*3*sizeof(GLfloat));
        m_VBOVideo.write(6*3*sizeof(GLfloat), afTexCoord, 6*2*sizeof(GLfloat));
        //m_VBOVideo.write(6*5*sizeof(GLfloat), afNormals, 6*3*sizeof(GLfloat));

        m_VBOVideo.release();
    }

    GLfloat afVertices[] = {
        -1,1, -1,      -0.767,1,-1,       -1,-1,-1,
         -0.767, -1,-1,  -1,-1,-1,        -0.767,1,-1
    };

    QFile fileTest("test.data");
    if(fileTest.exists()==true)
    {
        if(fileTest.open(QFile::ReadOnly)==true)
        {
            int nBytes = fileTest.size();
            uint8_t* pDataTest = new uint8_t[nBytes];
            fileTest.read((char*)pDataTest, nBytes);

            if(nBytes>0)
            {
                memcpy(m_QueueData[0], pDataTest, nBytes);

                m_bChange = true;
                m_nIndexDisplay = -1;
                UpdateTexture();
                m_bChange = true;
            }

            delete[] pDataTest;
        }
    }
}
