/******************************************************************************
*	File		-	vertex_buffer.cpp
*	Author		-	Joey Pollack
*	Date		-	2022/07/13 (y/m/d)
*	Mod Date	-	2022/07/13 (y/m/d)
*	Description	-	OpenGL vertex buffer object
******************************************************************************/

#include "vertex_buffer.h"
#include <glad/gl.h>

#include <utils/logger.h>

namespace lunarium
{    
    /////////////////////////////////////////////////////////////////////
    //				BUFFER LAYOUT
    /////////////////////////////////////////////////////////////////////
    void BufferLayout::PushVertexAttribute(VertexAttribute va)
    {
        mLayout.push_back(va);
    }

    void BufferLayout::ClearLayout()
    {
        mLayout.clear();
    }

    std::vector<VertexAttribute>& BufferLayout::GetLayout()
    {
        return mLayout;
    }


    int BufferLayout::GetNumAttributes() const
    {
        return mLayout.size();
    }

    int BufferLayout::GetLayoutSizeInBytes() const
    {
        int size = 0;
        for (auto va = mLayout.begin(); va != mLayout.end(); va++)
        {
            size += GetAttributeSizeInBytes(*va);
        }

        return size;
    }

    int BufferLayout::GetAttributeSizeInBytes(VertexAttribute va) const
    {
        int type_sizes[] = { 4, 8, 4, 8 };
        return type_sizes[va.Type] * va.NumValues;
    }

    void BufferLayout::ImplementLayout()
    {
        int type_sizes[] = { 4, 8, 4, 8 };
        GLenum gl_types[] =  { GL_INT, GL_INT, GL_FLOAT, GL_FLOAT };
        int stride = 0;
        std::vector<int> offsets;

        for (int i = 0; i < mLayout.size(); i++)
        {
            offsets.push_back(stride);
            stride += type_sizes[mLayout[i].Type] * mLayout[i].NumValues;
        }

        for (int i = 0; i < mLayout.size(); i++)
        {
            glVertexAttribPointer(i, mLayout[i].NumValues, gl_types[mLayout[i].Type], GL_FALSE, stride, (GLvoid*)offsets[i]);
            glEnableVertexAttribArray(i);
        }
    }


    /////////////////////////////////////////////////////////////////////
    //				VERTEX BUFFER
    /////////////////////////////////////////////////////////////////////
    VertexBuffer* VertexBuffer::Create(BufferLayout& VertexLayout, u32 num_vertices, const void* vertices, u32 num_indices, u32* indices)
    {
        return new VertexBuffer(VertexLayout, num_vertices, vertices, num_indices, indices);
    }

    void VertexBuffer::Destroy(VertexBuffer** ppBuf)
    {
        (*ppBuf)->Free();
        delete *ppBuf;
        *ppBuf = nullptr;
    }

    VertexBuffer::VertexBuffer(BufferLayout& VertexLayout, u32 num_vertices, const void* vertices, u32 num_indices, u32* indices)
        : mLayout(VertexLayout), mSize(0), mIndex(0), mEBO(0), mEBOSize(num_indices * sizeof(u32)), mEBOIndex(0), mNumIndices(0),  mIsStatic(false)
    {
        mIsStatic = vertices != nullptr;

        // Calculate buffer size
        mSize = VertexLayout.GetLayoutSizeInBytes() * num_vertices;

        glGenVertexArrays(1, &mVAO);
        glGenBuffers(1, &mVBO);

        if (mEBOSize > 0)
        {
            glGenBuffers(1, &mEBO);
        }

        
        Bind();
		BindVBO();
        if (vertices)
        {
            glBufferData(GL_ARRAY_BUFFER, mSize, vertices, GL_STATIC_DRAW);
        }
        else
        {
            glBufferData(GL_ARRAY_BUFFER, mSize, nullptr, GL_DYNAMIC_DRAW);
        }

        if (mEBOSize > 0)
        {
            glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mEBO);
            u32 mode = indices ? GL_STATIC_DRAW : GL_DYNAMIC_DRAW;
            mEBOIsStatic = indices ? true : false;
            mNumIndices = indices ? num_indices : 0;
            glBufferData(GL_ELEMENT_ARRAY_BUFFER, mEBOSize, indices, mode);
        }

        mLayout.ImplementLayout();
        Unbind();
		UnbindVBO();
    }

    
    void VertexBuffer::Clear()
    {
        if (mIsStatic)
        {
            Logger::Warn(LogCategory::GRAPHICS, "Attemping to clear a static VertexBuffer");
        }

        mIndex = 0;
        mEBOIndex = 0;

        if (!mEBOIsStatic)
            mNumIndices = 0;
    }

    void VertexBuffer::Bind()
    {
        glBindVertexArray(mVAO);
    }

    void VertexBuffer::Unbind()
    {
        glBindVertexArray(0);
    }

    
    void VertexBuffer::BindVBO()
    {
        glBindBuffer(GL_ARRAY_BUFFER, mVBO);
    }

    void VertexBuffer::UnbindVBO()
    {
        glBindBuffer(GL_ARRAY_BUFFER, 0);
    }

    bool VertexBuffer::PushVertices(const void* vertices, u32 num_verts)
    {
        if (mIsStatic)
        {
            Logger::Warn(LogCategory::GRAPHICS, "Cannot push vertices into a static buffer!");
            return false;
        }

        int data_size = mLayout.GetLayoutSizeInBytes() * num_verts;

        // TODO: BE CAREFUL! mSize is the size in bytes, not number of vertices.
        if (mIndex + data_size > mSize)
        {
            // Vertices do not fit into the buffer
           // Logger::Warn(LogCategory::GRAPHICS, "Cannot push vertices into buffer - not enough space left! Space left: %d, data size: %d", mSize - mIndex, data_size);
            return false;
        }

        // Add verts to the buffer
        BindVBO();
		glBufferSubData(GL_ARRAY_BUFFER, mIndex, data_size, vertices);
        UnbindVBO();

        mIndex += data_size;
        return true;
    }

    
    bool VertexBuffer::PushIndices(const u32* indices, u32 num_indices)
    {
        if (mEBOIsStatic)
        {
            Logger::Warn(LogCategory::GRAPHICS, "Cannot push indices into a static buffer!");
            return false;
        }

        int data_size = sizeof(u32) * num_indices;
        if (mEBOIndex + data_size > mEBOSize)
        {
            // Indices do not fit into the buffer
            //Logger::Warn(LogCategory::GRAPHICS, "Cannot push indices into buffer - not enough space left! Space left: %d, data size: %d", mEBOSize - mEBOIndex, data_size);
            return false;
        }

        Bind();
		glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, mEBOIndex, data_size, indices);
        Unbind();
        int error = glGetError();
        if (error > 0)
        {
            Logger::Error(LogCategory::GRAPHICS, "OGL ERROR!");
        }

        mNumIndices += num_indices;
        mEBOIndex += data_size;
        return true;
    }

    /////////////////////////////////////////////////////////////////////
    //				HELPER METHODS
    /////////////////////////////////////////////////////////////////////
    // void VertexBuffer::InitLayout()
    // {
        
        // switch (mLayoutType)
        // {
        //     case LayoutType::LINE:
        //     {
        //         Logger::Error(LogCategory::GRAPHICS, "Vertex layout type LINE not supported yet!");
        //     }
        //     break;

        //     case LayoutType::QUAD:
        //     {
        //         // position
		//         glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 10 * sizeof(GLfloat), (GLvoid*)0);
        //         glEnableVertexAttribArray(0);

        //         // Texture coords
		//         glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 10 * sizeof(GLfloat), (GLvoid*)(3 * sizeof(float)));
        //         glEnableVertexAttribArray(1);

        //         // Color
		//         glVertexAttribPointer(2, 4, GL_FLOAT, GL_FALSE, 10 * sizeof(GLfloat), (GLvoid*)(5 * sizeof(float)));
        //         glEnableVertexAttribArray(2);

        //         // Texture sampler index
		//         glVertexAttribPointer(3, 1, GL_FLOAT, GL_FALSE, 10 * sizeof(GLfloat), (GLvoid*)(9 * sizeof(float)));
        //         glEnableVertexAttribArray(2);

        //         // Texture ID - SHADERS SHOULD IGNORE THIS
		//         glVertexAttribPointer(4, 1, GL_INT, GL_FALSE, 10 * 8, (GLvoid*)(10 * sizeof(float)));
        //         glEnableVertexAttribArray(2);

        //     }
        //     break;

        //     case LayoutType::SPRITE:
        //     {
        //         Logger::Error(LogCategory::GRAPHICS, "Vertex layout type SPRITE not supported yet!");
        //     }
        //     break;

        //     default:
        //         Logger::Warn(LogCategory::GRAPHICS, "UNKNOWN Vertex Layout Type: %d", mLayoutType);

        // }
    //}

    void VertexBuffer::Free()
    {
        glDeleteVertexArrays(1, &mVAO);
        glDeleteBuffers(1, &mVBO);
    }

}