package electrosphere.renderer.ui.layout;

import electrosphere.main.Globals;
import electrosphere.renderer.Material;
import electrosphere.renderer.framebuffer.Framebuffer;
import electrosphere.renderer.ui.Widget;
import electrosphere.renderer.Model;
import electrosphere.engine.assetmanager.AssetDataStrings;
import electrosphere.renderer.framebuffer.FramebufferUtils;
import java.util.LinkedList;
import java.util.List;
import org.joml.Vector3f;
import static org.lwjgl.opengl.GL11.GL_COLOR_BUFFER_BIT;
import static org.lwjgl.opengl.GL11.GL_DEPTH_BUFFER_BIT;
import static org.lwjgl.opengl.GL11.GL_TEXTURE_2D;
import static org.lwjgl.opengl.GL11.glBindTexture;
import static org.lwjgl.opengl.GL11.glClear;
import static org.lwjgl.opengl.GL11.glClearColor;
import static org.lwjgl.opengl.GL13.GL_TEXTURE0;
import static org.lwjgl.opengl.GL13.GL_TEXTURE1;
import static org.lwjgl.opengl.GL13.GL_TEXTURE2;
import static org.lwjgl.opengl.GL13.GL_TEXTURE3;
import static org.lwjgl.opengl.GL13.glActiveTexture;
import static org.lwjgl.opengl.GL30.GL_FRAMEBUFFER;
import static org.lwjgl.opengl.GL30.glBindFramebuffer;

/**
 *
 * @author amaterasu
 */
public class LayoutSchemeListScrollable extends Widget {
    
    List<Widget> widgetList = new LinkedList();
    Framebuffer widgetBuffer = FramebufferUtils.generateScreensizeTextureFramebuffer();
    Material customMat = new Material();
    
    Vector3f boxPosition = new Vector3f();
    Vector3f boxDimensions = new Vector3f();
    Vector3f texPosition = new Vector3f(0,0,0);
    Vector3f texScale = new Vector3f(1,1,0);
    
    public LayoutSchemeListScrollable(int positionX, int positionY, int width, int height, boolean draw){
        customMat.setTexturePointer(widgetBuffer.getTexturePointer());
//        customMat.setTexturePointer(Globals.assetManager.fetchTexture("Textures/Branch.png").getTexturePointer());
        
        float ndcX =      (float)positionX/Globals.WINDOW_WIDTH;
        float ndcY =      (float)positionY/Globals.WINDOW_HEIGHT;
        float ndcWidth =  (float)width/Globals.WINDOW_WIDTH;
        float ndcHeight = (float)height/Globals.WINDOW_HEIGHT;
        boxPosition = new Vector3f(ndcX,ndcY,0);
        boxDimensions = new Vector3f(ndcWidth,ndcHeight,0);
    }

    static float aaaa = 0;
    
    @Override
    public void draw(int parentFramebufferPointer, int parentWidth, int parentHeight) {
        widgetBuffer.bind();
        glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        for(Widget child : widgetList){
            child.draw(widgetBuffer.getFramebufferPointer(),width,height);
        }
        //this call binds the screen as the "texture" we're rendering to
        //have to call before actually rendering
        glBindFramebuffer(GL_FRAMEBUFFER, 0);
        aaaa = aaaa + 0.0005f;
        if(aaaa > 1){
            aaaa = -1;
        }
        texPosition.y = aaaa;
        Model planeModel = Globals.assetManager.fetchModel(Globals.planeModelID);
        planeModel.pushUniformToMesh("plane", "mPosition", boxPosition);
        planeModel.pushUniformToMesh("plane", "mDimension", boxDimensions);
        planeModel.pushUniformToMesh("plane", "tPosition", texPosition);
        planeModel.pushUniformToMesh("plane", "tDimension", texScale);
        planeModel.meshes.get(0).setMaterial(customMat);
        planeModel.drawUI();
    }

    public void pack() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    public void addWidget(Widget widget) {
        widgetList.add(widget);
        widget.setVisible(false);
    }

    public List<Widget> getWidgets() {
        return widgetList;
    }

    public void destroy() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
    
}
