#include <memory>
#include <pch.hpp>
#include <renderer/renderer.hpp>
#include "opengl/opengl_shader.hpp"
#include "renderer/render_command.hpp"

#include <glad/glad.h>
#include <GLFW/glfw3.h>

namespace OpenEngine {
    void Renderer::Init()
    {
        RenderCommand::Init();
    }

    void Renderer::OnWindowResize(uint32_t width, uint32_t height)
    {
        RenderCommand::SetViewport(0, 0, width, height);
    }

    void Renderer::BeginScene(const OrthographicCamera& camera)
    {
        scene_data->view_projection_matrix = camera.GetViewProjectionMatrix();
    }

    void Renderer::EndScene()
    {
    }

    void Renderer::Submit(const std::shared_ptr<Shader> shader,
            const std::shared_ptr<VertexArray>& vertex_array,
            const glm::mat4& transform)
    {
        shader->Bind();
        std::dynamic_pointer_cast<OpenGLShader>(shader)->SetMat4("u_ViewProjection", scene_data->view_projection_matrix);
        std::dynamic_pointer_cast<OpenGLShader>(shader)->SetMat4("u_Transform", transform);

        vertex_array->Bind();
        RenderCommand::DrawIndexed(vertex_array);
    }
}
