import sys

from OpenGL import GL

import sys

try:
    from PySide6.QtCore import QLibraryInfo, qVersion, QRect, Qt
    from PySide6.QtGui import QOpenGLFunctions, QColor, QOpenGLContext
    from PySide6.QtOpenGL import  QOpenGLShaderProgram, QOpenGLShader
    from PySide6.QtWidgets import QApplication
    from PySide6.QtOpenGLWidgets import QOpenGLWidget
except ImportError:
    from PySide2.QtCore import QLibraryInfo, qVersion, QRect, Qt
    from PySide2.QtGui import QOpenGLFunctions, QColor, QOpenGLContext, QOpenGLShaderProgram, QOpenGLShader
    from PySide2.QtWidgets import QApplication, QOpenGLWidget


basic_vertex_shader_source = \
"""
attribute highp vec4 vPosition;
void main()
{
    gl_Position = vPosition;
}
"""

basic_fragment_shader_source = \
"""
uniform mediump vec4 color;
void main()
{
gl_FragColor = color; // WILL DRAW A BLACK TRIANGLE EVEN THOUGH THE UNIFORM IS SET
//gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0); // WILL DRAW A RED TRIANGLE
}
"""

class TestWidget(QOpenGLWidget):
    
    def initializeGL(self):
        context = QOpenGLContext.currentContext()
        
        self.GLF =  QOpenGLFunctions(context)
        
        self.shader_program = QOpenGLShaderProgram(context)
        b = self.shader_program.addShaderFromSourceCode(QOpenGLShader.Vertex, basic_vertex_shader_source)
        print('Added Vertex', b)
        b = self.shader_program.addShaderFromSourceCode(QOpenGLShader.Fragment, basic_fragment_shader_source)
        print('Added Fragment', b)
        b = self.shader_program.bind()
        print('bind ', b)
        
        self.GLF.glClear(GL.GL_DEPTH_BUFFER_BIT)
        self.GLF.glClearColor(255, 255, 255, 255)
        self.GLF.glEnable(GL.GL_DEPTH_TEST)
        self.GLF.glEnable(GL.GL_CULL_FACE)
        
    def paintGL(self):
        self.GLF.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT)
        
        vertex_position_location = self.shader_program.attributeLocation("vPosition")
        print('vertex_position_location=', vertex_position_location)
        color_location = self.shader_program.attributeLocation("color")
        
        vertices = [ 0.0,  0.5, 0.0,
                    -0.5, -0.5, 0.0,
                     0.5, -0.5, 0.0 ]
        
        color = QColor(255, 0, 0, 255) # <== THIS SHOULD BE A RED TRIANGLE AS WELL
        print(color)
        
        self.shader_program.enableAttributeArray(vertex_position_location)
        self.shader_program.setAttributeArray(vertex_position_location, vertices, 3)
        print('setting color, color_location=', color_location)
        self.shader_program.setUniformValue(color_location, color) # <= RUNTIME WARNING, COLOR IS NOT SET
        print('setting color')
        
        self.GLF.glDrawArrays(GL.GL_TRIANGLES, 0, 3)
        
        self.shader_program.disableAttributeArray(vertex_position_location)

    def resizeGL(self, w, h):
        self.GLF.glViewport(0, 0,  w,  h)


if __name__ == "__main__":
    print('Python {}.{}'.format(sys.version_info[0], sys.version_info[1]))
    print(QLibraryInfo.build())
    app = QApplication()
    widget = TestWidget()
    widget.setWindowTitle(qVersion())
    widget.show()
    app.exec_()
