/*
 *  <Short Description>
 *  Copyright (C) 2023  Brett Terpstra
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */
#include <blt/gfx/model.h>
#include <blt/std/memory_util.h>
#include "blt/std/logging.h"

namespace blt::gfx
{
    void static_dynamic_array::swap()
    {
        size_t next_size = blt::mem::next_byte_allocation(size_);
        
        auto* new_data = new vbo_type[next_size];
        
        if (std::holds_alternative<vbo_type*>(data_))
        {
            auto* ptr = std::get<vbo_type*>(data_);
            for (size_t i = 0; i < size_; i++)
                new_data[i] = ptr[i];
            delete[] ptr;
        } else
        {
            auto data = std::get<array_t>(data_);
            for (size_t i = 0; i < DATA_SIZE; i++)
                new_data[i] = data[i];
        }
        data_ = new_data;
        size_ = next_size;
    }
    
    static_dynamic_array::static_dynamic_array() = default;
    
    static_dynamic_array::vbo_type& static_dynamic_array::operator[](size_t index)
    {
        if (index >= size_)
            swap();
        max = std::max(index, max);
        if (std::holds_alternative<vbo_type*>(data_))
            return std::get<vbo_type*>(data_)[index];
        else
            return std::get<array_t>(data_)[index];
    }
    
    /*
     * -----------------------------------
     *              vbo_t
     * -----------------------------------
     */
    void vertex_buffer_t::create(GLint type)
    {
        glGenBuffers(1, &bufferID_);
        buffer_type = type;
    }
    
    void vertex_buffer_t::destroy()
    {
        // prevent multiple destroys of the same object, in cases of multi attribute binds
        if (bufferID_ == 0)
            return;
        glDeleteBuffers(1, &bufferID_);
        bufferID_ = 0;
    }
    
    void vertex_buffer_t::allocate(GLsizeiptr size, GLint mem_type, const void* data)
    {
        bind();
        size_ = size;
        glBufferData(buffer_type, size, data, mem_type);
        memory_type = mem_type;
    }
    
    void vertex_buffer_t::update(GLsizeiptr size, void* data)
    {
        if (size <= size_)
            sub_update(0, size, data);
        else
            allocate(size, memory_type, data);
        size_ = size;
    }
    
    void vertex_buffer_t::sub_update(GLsizeiptr offset, GLsizeiptr size, void* data) const
    {
        bind();
        glBufferSubData(buffer_type, offset, size, data);
    }
    
    void vertex_buffer_t::bind() const
    {
        glBindBuffer(buffer_type, bufferID_);
    }
    
    void vertex_buffer_t::unbind() const
    {
        glBindBuffer(buffer_type, 0);
    }
    
    /*
     * ----------------------------
     *      basic_vertex_array
     * ----------------------------
     */
    
    vertex_array_t::vertex_array_t(): vaoID(0)
    {
        glGenVertexArrays(1, &vaoID);
    }
    
    vertex_array_t::~vertex_array_t()
    {
        // free all VBOs
        for (size_t i = 0; i < VBOs.used(); i++)
            VBOs[i] = nullptr;
        element.destroy();
        // then free the vertex array
        glDeleteVertexArrays(1, &vaoID);
    }
    
    void vertex_array_t::bindVBO(const vertex_buffer_t& vbo, int attribute_number, int coordinate_size, GLenum type, int stride, long offset)
    {
        handle_vbo(vbo, attribute_number, coordinate_size, type, stride, offset);
        VBOs[attribute_number] = make_vbo(vbo);
    }
    
    void vertex_array_t::bindVBO(const static_dynamic_array::vbo_type& vbo, int attribute_number, int coordinate_size, GLenum type, int stride, long offset)
    {
        handle_vbo(vbo->vbo, attribute_number, coordinate_size, type, stride, offset);
        VBOs[attribute_number] = vbo;
    }
    
    void vertex_array_t::handle_vbo(const vertex_buffer_t& vbo, int attribute_number, int coordinate_size, GLenum type, int stride, long offset)
    {
        used_attributes.insert(attribute_number);
        bind();
        vbo.bind();
        
        glVertexAttribPointer(attribute_number, coordinate_size, type, GL_FALSE, stride < 0 ? 0 : stride, (void*) offset);
        glEnableVertexAttribArray(attribute_number);
        unbind();
    }
}