#pragma once

#include <glm/glm.hpp>
#include <vector>
#include <memory>
#include <string>
#include <algorithm>

// 假设这些头文件在您的项目中存在
#include "../../systems/RenderSystem.h" 

namespace Vivid2D::Render::Buffer {

    /**
     * @brief 渲染状态容器结构体
     */
    struct VIVID_2D_MYDLL_API RenderState {
        // 渲染状态变量 (对应 Java 中的 RenderState)
        unsigned int textureId = 0;
        int textureUnit = 0;
        unsigned int shaderProgram = 0;
        glm::vec4 color = glm::vec4(1.0f, 1.0f, 1.0f, 1.0f);
        float opacity = 1.0f;
        int blendMode = 0;
        bool depthTest = false;
        bool blending = true;

        /**
         * @brief 保存当前 OpenGL 状态 (Program, Texture, Blending/Depth)
         */
        void saveCurrentState();

        /**
         * @brief 应用保存的 OpenGL 状态 (绑定 Program 和 Texture，设置 GL 状态)
         */
        void applyState() const;

        /**
         * @brief 复制状态
         */
        RenderState copy() const {
            RenderState copy;
            copy.textureId = this->textureId;
            copy.textureUnit = this->textureUnit;
            copy.shaderProgram = this->shaderProgram;
            copy.color = this->color; // glm::vec4 默认支持值拷贝
            copy.opacity = this->opacity;
            copy.blendMode = this->blendMode;
            copy.depthTest = this->depthTest;
            copy.blending = this->blending;
            return copy;
        }
    };

    /**
     * @brief 构建完成的缓冲区数据结构
     */
    struct VIVID_2D_MYDLL_API BuiltBuffer {
        unsigned int vao;
        unsigned int vbo;
        int vertexCount;
        int mode; // GL_TRIANGLES, GL_LINE_LOOP 等
        RenderState renderState;

        BuiltBuffer(unsigned int vao, unsigned int vbo, int vertexCount, int mode, const RenderState& state)
            : vao(vao), vbo(vbo), vertexCount(vertexCount), mode(mode), renderState(state) {}
    };


    /**
     * @brief 简化版 BufferBuilder，用于按顶点流构建并一次性绘制几何体。
     * 每个顶点格式： float x, float y, float u, float v  （共4个 float）
     */
    class VIVID_2D_MYDLL_API BufferBuilder {
    private:
        static constexpr int COMPONENTS_PER_VERTEX = 4; // x,y,u,v
        std::vector<float> array;
        size_t size; // 实际存储的 float 数量 (array.size())
        size_t vertexCount;
        size_t mode; // GL mode
        RenderState renderState;
        bool stateSaved = false;
    public:
        BufferBuilder();
        explicit BufferBuilder(int initialFloatCapacity);

        /**
         * @brief 开始构建，传入要绘制的 GL 模式 (例如 GL_LINE_LOOP)
         */
        void begin(int glMode, int estimatedVertexCount);

        void setTexture(unsigned int textureId, int textureUnit);
        void setShader(unsigned int programId);
        void setColor(const glm::vec4& color);
        void setOpacity(float opacity);

        /**
         * @brief 添加顶点：x,y,u,v
         */
        void vertex(float x, float y, float u, float v);

        /**
         * @brief 结束构建并返回 BuiltBuffer（不立即绘制），调用者负责 dispose。
         */
        std::unique_ptr<BuiltBuffer> end();

        /**
         * @brief 立即上传并绘制，并释放 GPU 资源。
         */
        void endImmediate();

        /**
         * @brief 清理构建器创建的所有 GPU 资源。
         */
        static void dispose(const BuiltBuffer* buffer);

        /**
         * @brief 清除构建器状态以便重用
         */
        void clear();

        size_t getVertexCount() const {
            return vertexCount;
        }

        bool isEmpty() const {
            return vertexCount == 0;
        }
    };

} // namespace Buffer