#include "Mesh.hpp"
#include "MathF.hpp"
#include <cmath>

TSE::Mesh::Mesh()
{
    name = "";
}

TSE::Mesh::Mesh(string _name, const std::vector<Vector3> &_vertecies, const std::vector<ushort> &_indecies, const std::vector<Vector2> &_uvs)
{
    name = _name;
    vertecies = std::move(_vertecies);
    indecies = std::move(_indecies);
    uvs = std::move(_uvs);
}

size_t TSE::Mesh::IndeciesCount() const
{
    return indecies.size();
}

size_t TSE::Mesh::VerteciesCount() const
{
    return vertecies.size();
}

TSE::Mesh TSE::Mesh::GetCircleMesh(ushort segments)
{
    std::vector<Vector3> verts;
    std::vector<ushort> indices;
    std::vector<Vector2> uvs;

    verts.emplace_back(0.0f, 0.0f, 0.0f);
    uvs.emplace_back(0.5f, 0.5f);

    float angleStep = 2.0f * TSE_PI / segments;

    for (int i = 0; i <= segments; ++i) {
        float angle = i * angleStep;
        float x = std::cos(angle) * 0.5f;
        float y = std::sin(angle) * 0.5f;
        verts.emplace_back(x, y, 0);
        uvs.emplace_back(x + 0.5f, y + 0.5f);

        if (i > 0) {
            indices.push_back(0);
            indices.push_back(i);
            indices.push_back(i + 1);
        }
    }

    return Mesh("Circle", verts, indices, uvs);
}

TSE::Mesh TSE::Mesh::GetQuadMesh()
{
    std::vector<Vector3> verts = {
        Vector3(-0.5f, -0.5f, 0),
        Vector3( 0.5f, -0.5f, 0),
        Vector3( 0.5f,  0.5f, 0),
        Vector3(-0.5f,  0.5f, 0)
    };

    std::vector<Vector2> uvs = {
        Vector2(0.0f, 0.0f),
        Vector2(1.0f, 0.0f),
        Vector2(1.0f, 1.0f),
        Vector2(0.0f, 1.0f)
    };

    std::vector<unsigned short> indices = {
        0, 1, 2,
        2, 3, 0
    };

    return Mesh("Quad", verts, indices, uvs);
}
