//
// Created by sebastian on 08.02.26.
//

#include "HexModelFactory.h"

#include <vector>

#include "../../engine/renderer/loader/Loader.h"
#include "glm/trigonometric.hpp"

RawModel HexModelFactory::createHexRawModel(Loader &loader, float radius) {
    std::vector<float> vertices;
    std::vector<float> uvs;
    std::vector<int> indices;
    std::vector<float> normals;

    vertices.insert(vertices.end(), {0.f, 0.f, 0.f});
    normals.insert(normals.end(), {0.f, 1.f, 0.f});
    uvs.insert(uvs.end(), {0.5f, 0.5f});

    for (int i = 0; i < 6; i++) {
        float angle = glm::radians((60.f * i - 30.0f));
        float x = radius * std::cos(angle);
        float z = radius * std::sin(angle);

        //Vertex positions
        vertices.push_back(x);
        vertices.push_back(0.0f);
        vertices.push_back(z);

        //Normals
        normals.push_back(0.0f);
        normals.push_back(1.0f);
        normals.push_back(0.0f);

        //uvs mapped to circle
        uvs.push_back((x / radius + 1.f) * 0.5f);
        uvs.push_back((z / radius + 1.f) * 0.5f);
    }

    for (int i = 1; i <= 6; ++i) {
        indices.push_back(0);
        indices.push_back(i % 6 + 1);
        indices.push_back(i);
    }

    return loader.loadToVAO(vertices, normals, uvs, indices);
}

TexturedModel HexModelFactory::createTexturedHexModel(Loader &loader, float radius, RessourceType ressourceType = RessourceType::NONE) {
    RawModel rawModel = createHexRawModel(loader, radius);

    std::shared_ptr<ModelTexture> modelTexture;
    if (ressourceType == RessourceType::NONE) {
        modelTexture = std::make_shared<ModelTexture>(loader.loadTextureFromFile("assets/hex/white.png"));
    } else {
        modelTexture = std::make_shared<ModelTexture>(loader.loadTextureFromFile("assets/hex/wood/Ground053_1K-JPG_Color.jpg"));
    }

    return {std::make_shared<RawModel>(rawModel), std::move(modelTexture)};
}
