#include <extra2d/graphics/batch/shape_batch.h>

namespace extra2d {

// ============================================================================
// ShapeBatch 基础实现（后端无关部分）
// ============================================================================

// 这里可以添加后端无关的工具函数
// 例如：计算圆形的顶点、三角化多边形等

// 计算圆形顶点
void calculateCircleVertices(std::vector<Vec2>& outVertices, 
                              const Vec2& center, float radius, 
                              int segments, bool fill) {
    outVertices.clear();
    outVertices.reserve(fill ? segments + 1 : segments);

    if (fill) {
        // 填充圆形：中心点 + 边缘点
        outVertices.push_back(center);
        for (int i = 0; i <= segments; ++i) {
            float angle = 2.0f * 3.14159265359f * static_cast<float>(i) / static_cast<float>(segments);
            outVertices.emplace_back(
                center.x + radius * cosf(angle),
                center.y + radius * sinf(angle)
            );
        }
    } else {
        // 圆形边框：只保留边缘点
        for (int i = 0; i < segments; ++i) {
            float angle = 2.0f * 3.14159265359f * static_cast<float>(i) / static_cast<float>(segments);
            outVertices.emplace_back(
                center.x + radius * cosf(angle),
                center.y + radius * sinf(angle)
            );
        }
    }
}

// 计算矩形顶点
void calculateRectVertices(std::vector<Vec2>& outVertices, const Rect& rect) {
    outVertices.clear();
    outVertices.reserve(4);

    float x1 = rect.origin.x;
    float y1 = rect.origin.y;
    float x2 = rect.origin.x + rect.size.width;
    float y2 = rect.origin.y + rect.size.height;

    outVertices.emplace_back(x1, y1);  // 左上
    outVertices.emplace_back(x2, y1);  // 右上
    outVertices.emplace_back(x2, y2);  // 右下
    outVertices.emplace_back(x1, y2);  // 左下
}

// 简单的多边形三角化（扇形三角化，适用于凸多边形）
void triangulatePolygon(std::vector<uint16_t>& outIndices, int vertexCount) {
    outIndices.clear();
    if (vertexCount < 3) {
        return;
    }

    // 扇形三角化：以第一个顶点为扇形中心
    for (int i = 1; i < vertexCount - 1; ++i) {
        outIndices.push_back(0);
        outIndices.push_back(i);
        outIndices.push_back(i + 1);
    }
}

} // namespace extra2d
