//
// Created by sebastian on 07.02.26.
//

#ifndef MAPGENERATOR_H
#define MAPGENERATOR_H
#include <cmath>
#include <random>

#include "HexTile.h"
#include "Map.h"

class MapGenerator {
public:
    static void generateHexMap(Map& map, int width, int height, float hexRadius, std::vector<std::shared_ptr<Entity>>& mapEntities, const std::shared_ptr<TexturedModel>& treeModel) {
        // Zufallsgenerator initialisieren (einmal)
        std::random_device rd;  // Zufälliger Seed
        std::mt19937 gen(rd()); // Mersenne Twister Generator

        // Verteilung definieren: hier z.B. 0 bis 9
        std::uniform_real_distribution<float> dis(0.0f, 1.0f);
        map.tiles.clear();

        float xOffset = hexRadius * std::sqrt(3.0f);
        float zOffset = hexRadius * 1.5f;

        for (int r = 0; r < height; ++r) {
            for (int q = 0; q < width; ++q) {
                HexTile tile;
                tile.q = q;
                tile.r = r;

                // Pointy-top Hexes
                float x = xOffset * (q + 0.5f * (r % 2));
                float z = zOffset * r;
                tile.worldPos = glm::vec3(x, 0.0f, z);
                tile.radius = hexRadius;

                float randomValue = dis(gen);
                if (randomValue < 0.5f) {
                    tile.resourceType = RessourceType::WOOD;

                    int treeCount = 3 + (int) (dis(gen) * 3);
                    std::uniform_real_distribution<float> offsetDis(-hexRadius * 0.5f, hexRadius * 0.5f);

                    for (int i = 0; i < treeCount; ++i) {
                        glm::vec3 treePos = tile.worldPos;
                        treePos.x += offsetDis(gen);
                        treePos.z += offsetDis(gen);

                        std::shared_ptr<Entity> treeEntity = std::make_shared<Entity>(Entity(treeModel, treePos, 0,0,0,.4f));

                        mapEntities.push_back(treeEntity);
                        tile.entitiesOnTile.push_back(treeEntity);
                    }

                }

                map.tiles.push_back(tile);
            }
        }
    }
};



#endif //MAPGENERATOR_H
