package electrosphere.renderer.pipelines;

import org.lwjgl.opengl.GL40;

import electrosphere.engine.Globals;
import electrosphere.renderer.OpenGLState;
import electrosphere.renderer.RenderPipelineState;
import electrosphere.renderer.RenderingEngine;
import electrosphere.renderer.texture.Texture;
import electrosphere.renderer.ui.elementtypes.DrawableElement;
import electrosphere.renderer.ui.elementtypes.Element;

/**
 * Main ui rendering pipeline
 */
public class UIPipeline implements RenderPipeline {

    @Override
    public void render(OpenGLState openGLState, RenderPipelineState renderPipelineState) {
        Globals.profiler.beginCpuSample("UIPipeline.render");

        //
        //Black background
        //
        if(Globals.RENDER_FLAG_RENDER_BLACK_BACKGROUND){
            openGLState.setActiveShader(renderPipelineState, RenderingEngine.screenTextureShaders);
            openGLState.glDepthTest(false);
            GL40.glBindVertexArray(RenderingEngine.screenTextureVAO);
            Texture blackTexture = Globals.assetManager.fetchTexture(Globals.blackTexture);
            if(blackTexture != null){
                blackTexture.bind(openGLState);
            }
            GL40.glDrawArrays(GL40.GL_TRIANGLES, 0, 6);
            GL40.glBindVertexArray(0);
        }


        //
        //White background
        //
        if(Globals.RENDER_FLAG_RENDER_WHITE_BACKGROUND){
            openGLState.setActiveShader(renderPipelineState, RenderingEngine.screenTextureShaders);
            openGLState.glDepthTest(false);
            GL40.glBindVertexArray(RenderingEngine.screenTextureVAO);
            Texture blackTexture = Globals.assetManager.fetchTexture(Globals.offWhiteTexture);
            if(blackTexture != null){
                blackTexture.bind(openGLState);
            }
            GL40.glDrawArrays(GL40.GL_TRIANGLES, 0, 6);
            GL40.glBindVertexArray(0);
        }


        //
        // Set render pipeline state
        //
        if(Globals.RENDER_FLAG_RENDER_UI){
            renderPipelineState.setUseMeshShader(true);
            renderPipelineState.setBufferStandardUniforms(false);
            renderPipelineState.setBufferNonStandardUniforms(true);
            renderPipelineState.setUseMaterial(true);
            renderPipelineState.setUseShadowMap(false);
            renderPipelineState.setUseBones(false);
            renderPipelineState.setUseLight(false);

            //the initial parent position values
            int parentPosX = 0;
            int parentPosY = 0;

            //set opengl state
            openGLState.glDepthTest(false);
            openGLState.glBlend(true);

            for(Element currentElement : Globals.elementManager.getWindowList()){
                if(currentElement instanceof DrawableElement){
                    DrawableElement drawable = (DrawableElement) currentElement;
                    if(drawable.getVisible()){
                        drawable.draw(renderPipelineState, openGLState, RenderingEngine.GL_DEFAULT_FRAMEBUFFER, parentPosX, parentPosY, Globals.WINDOW_WIDTH, Globals.WINDOW_HEIGHT);
                    }
                }
            }
        }
        
        Globals.profiler.endCpuSample();
    }
    
}
