//
// Created by sebastian on 29.07.26.
//

#ifndef COLORRACE_VBO_H
#define COLORRACE_VBO_H
#pragma once
#include <glad/glad.h>
#include <cstddef>
#include <memory>

namespace engine {
    class Vbo {
    public:
        static std::unique_ptr<Vbo> create(GLenum type, GLenum usage);
        static std::unique_ptr<Vbo> createAndAllocate(GLenum type, GLenum usage, size_t sizeInBytes);

        ~Vbo();

        Vbo(const Vbo&) = delete;
        Vbo& operator=(const Vbo&) = delete;

        [[nodiscard]] size_t getByteSize() const { return m_sizeInBytes; }
        [[nodiscard]] unsigned int getId() const { return m_id; }

        void bind() const;
        void unbind() const;


        // Legt den Buffer neu an; kopiert optional vorhandene Daten in den neuen, größeren/kleineren Buffer
        void resize(size_t copyDataSize, size_t newSizeInBytes);

        void copyContentsTo(Vbo& destination, size_t readStart, size_t writeStart, size_t dataSize) const;

        void allocateData(size_t sizeInBytes);
        void allocateData(size_t sizeInBytes, GLenum target);
        void orphan();

        void storeData(size_t startInBytes, const void* data, size_t dataSizeInBytes);
        void allocateAndStoreData(const void* data, size_t sizeInBytes);

    private:
        Vbo(unsigned int id, GLenum type, GLenum usage);

        unsigned int m_id;
        GLenum m_type;
        GLenum m_usage;
        size_t m_sizeInBytes = 0;

        static std::unique_ptr<Vbo> createTempVbo(size_t dataSize);

    };
}




#endif //COLORRACE_VBO_H
