//
// Created by sfd on 25-4-22.
//

#include "OpenGLRendererAPI.h"

#include <glad/glad.h>
#include <Hazel/Debug/Instrumentor.h>

namespace Hazel
{
    void OpenGLRendererAPI::Init()
    {
        HZ_PROFILE_FUNCTION();
        glEnable(GL_BLEND);
        glEnable(GL_DEPTH_TEST);
        glEnable(GL_LINE_SMOOTH);
        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    }

    void OpenGLRendererAPI::SetClearColor(const glm::vec4& color)
    {
        glClearColor(color.x, color.y, color.z, color.w);
    }

    void OpenGLRendererAPI::SetViewPort(uint32_t x, uint32_t y, uint32_t width, uint32_t height)
    {
        HZ_CORE_INFO("Setting viewport to ({0}, {1}), ({2}, {3})", x, y, width, height);
        glViewport(x, y, width, height);
    }

    void OpenGLRendererAPI::Clear()
    {
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    }

    void OpenGLRendererAPI::DrawIndexed(const std::shared_ptr<Hazel::VertexArray>& vertexArray, const uint32_t indexCount)
    {
        uint32_t count;
        if (indexCount != -1)
            count = indexCount;
        else
            count = vertexArray->GetIndexBuffer()->GetCount();
        if (count <= 0)
            return;
        vertexArray->Bind();
        glDrawElements(GL_TRIANGLES, count, GL_UNSIGNED_INT, nullptr);
    }

    void OpenGLRendererAPI::DrawLines(const std::shared_ptr<Hazel::VertexArray>& vertexArray, const uint32_t vertexCount)
    {
        uint32_t count;
        if (vertexCount != -1)
            count = vertexCount;
        else
            count = vertexArray->GetIndexBuffer()->GetCount();
        if (count <= 0)
            return;
        vertexArray->Bind();
        glDrawArrays(GL_LINES, 0, count);
    }

    void OpenGLRendererAPI::SetLineWidth(const float width)
    {
        glLineWidth(width);
    }
}
