//
// Created by sebastian on 09.02.26.
//

#include "GUIRenderer.h"

#include <iostream>
#include <vector>

#include "../core/gui/uiComponent/UiRenderBundle.h"
#include "../toolbox/MathUtils.h"
#include "loader/Loader.h"

GUIRenderer::GUIRenderer(Loader &loader) {
    std::vector<float> positions = {-1, 1, -1,-1,1,1,1,-1};
    auto model = loader.loadToVAO(positions);
    rawModel = std::make_unique<RawModel>(model);
}

void GUIRenderer::render(std::vector<std::shared_ptr<GUITexture>>& gui_textures) {
    guiShader.start();
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    glDisable(GL_DEPTH_TEST);
    glBindVertexArray(rawModel->vaoID);
    glEnableVertexAttribArray(0);
    for (auto texture : gui_textures) {
        glActiveTexture(GL_TEXTURE0);
        glBindTexture(GL_TEXTURE_2D, texture->getTextureID());
        glm::mat4 transformationMatrix = MathUtils::createTransformationMatrix(texture->getPosition(), texture->getScale());
        guiShader.loadTransformationMatrix(transformationMatrix);
        glDrawArrays(GL_TRIANGLE_STRIP, 0, rawModel->vertexCount);
    }
    //render
    glDisableVertexAttribArray(0);
    glBindVertexArray(0);
    glEnable(GL_DEPTH_TEST);
    glDisable(GL_BLEND);
    guiShader.stop();
}

void GUIRenderer::cleanUp() {
    guiShader.cleanUp();
}

