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

#include "MapGenerator.h"

float MapGenerator::hexRadius = 10.0f;

void MapGenerator::init(Loader &loader, float hexRadius) {
    MapGenerator::hexRadius = hexRadius;
    auto hexModelNone = HexModelFactory::createTexturedHexModel(loader, hexRadius, RessourceType::NONE);
    auto hexModelWood = HexModelFactory::createTexturedHexModel(loader, hexRadius, RessourceType::WOOD);
    auto hexModelStone = HexModelFactory::createTexturedHexModel(loader, hexRadius, RessourceType::STONE);

    AssetManager::insertGeneratedModel("hexModelNone", std::make_shared<TexturedModel>(hexModelNone));
    AssetManager::insertGeneratedModel("hexModelWood", std::make_shared<TexturedModel>(hexModelWood));
    AssetManager::insertGeneratedModel("hexModelStone", std::make_shared<TexturedModel>(hexModelStone));
}

void MapGenerator::generateHexMap(Map &map, int width, int height, EntityManager &entityManager) {
     Random random;

        map.clear(entityManager);

        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) {
                // Pointy-top Hexes
                float x = xOffset * (q + 0.5f * (r % 2));
                float z = zOffset * r;
                glm::vec3 worldPos = glm::vec3(x, 0.0f, z);
                float radius = hexRadius;

                float randomValue = random.randomFloat(0.0f, 1.0f);
                RessourceType resourceType = RessourceType::NONE;
                std::shared_ptr<TexturedModel> hexModel = AssetManager::getModel("hexModelNone");
                std::vector<EntityID> tileEntities;
                if (randomValue < 0.5f) {
                    resourceType = RessourceType::WOOD;
                    ForestTileGenerator forestTileGenerator;
                    tileEntities = forestTileGenerator.generateHexTile(entityManager, random, worldPos);
                    hexModel = AssetManager::getModel("hexModelWood");
                } else if (randomValue < 0.75f) {
                    resourceType = RessourceType::STONE;
                    hexModel = AssetManager::getModel("hexModelStone");
                }

                EntityID entityID = entityManager.createEntity();
                const auto transformComponent = std::make_shared<TransformComponent>(worldPos, glm::vec3(0), 1.0f);
                const auto modelComponent = std::make_shared<ModelComponent>(hexModel);
                const auto tileHighlightComponent = std::make_shared<TileRenderComponent>(false);
                const auto tileGameplayComponent = std::make_shared<TileGameplayComponent>(q, r, radius, resourceType);
                tileGameplayComponent->entitiesOnTile = tileEntities;

                entityManager.addComponent(entityID, transformComponent);
                entityManager.addComponent(entityID, modelComponent);
                entityManager.addComponent(entityID, tileHighlightComponent);
                entityManager.addComponent(entityID, tileGameplayComponent);
                entityManager.addComponent(entityID, std::make_shared<MapEntityComponent>());
            }
        }
}
