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

#ifndef ENTITYMANAGER_H
#define ENTITYMANAGER_H
#include <cstdint>
#include <memory>
#include <unordered_map>
#include <vector>

#include "../../../game/hexWorld/ecs/components/OwnerComponent.h"

class BuildingComponent;
// Forward Declarations (KEINE includes hier!)
class TransformComponent;
class ModelComponent;
class TileRenderComponent;
class TileGameplayComponent;
class MapEntityComponent;

using EntityID = std::uint32_t;

class EntityManager {
private:
    EntityID nextID = 1;
    std::vector<EntityID> entities;

    std::unordered_map<EntityID, std::shared_ptr<TransformComponent>> transforms;
    std::unordered_map<EntityID, std::shared_ptr<ModelComponent>> models;
    std::unordered_map<EntityID, std::shared_ptr<TileRenderComponent>> tileRenderComponents;
    std::unordered_map<EntityID, std::shared_ptr<TileGameplayComponent>> tileGameplayComponents;
    std::unordered_map<EntityID, std::shared_ptr<MapEntityComponent>> mapEntityComponents;
    std::unordered_map<EntityID, std::shared_ptr<BuildingComponent>> buildings;
    std::unordered_map<EntityID, std::shared_ptr<OwnerComponent>> owners;

public:
    EntityID createEntity();
    void destroyEntity(EntityID entity);

    template<typename T>
    void addComponent(EntityID entity, std::shared_ptr<T> component) {
        if constexpr (std::is_same_v<T, TransformComponent>) {
            transforms[entity] = component;
        } else if constexpr (std::is_same_v<T, ModelComponent>) {
            models[entity] = component;
        } else if constexpr (std::is_same_v<T, TileRenderComponent>) {
            tileRenderComponents[entity] = component;
        } else if constexpr (std::is_same_v<T, TileGameplayComponent>) {
            tileGameplayComponents[entity] = component;
        } else if constexpr (std::is_same_v<T, MapEntityComponent>) {
            mapEntityComponents[entity] = component;
        } else if constexpr (std::is_same_v<T, BuildingComponent>) {
            buildings[entity] = component;
        } else if constexpr (std::is_same_v<T, OwnerComponent>) {
            owners[entity] = component;
        } else {
            static_assert(sizeof(T) == 0, "Component-Typ nicht unterstützt");
        }
    }

    template<typename T>
    std::shared_ptr<T> getComponent(EntityID entity) {
        if constexpr (std::is_same_v<T, TransformComponent>) {
            auto it = transforms.find(entity);
            return (it != transforms.end()) ? it->second : nullptr;
        } else if constexpr (std::is_same_v<T, ModelComponent>) {
            auto it = models.find(entity);
            return (it != models.end()) ? it->second : nullptr;
        } else if constexpr (std::is_same_v<T, TileRenderComponent>) {
            auto it = tileRenderComponents.find(entity);
            return (it != tileRenderComponents.end()) ? it->second : nullptr;
        } else if constexpr (std::is_same_v<T, TileGameplayComponent>) {
            auto it = tileGameplayComponents.find(entity);
            return (it != tileGameplayComponents.end()) ? it->second : nullptr;
        } else if constexpr (std::is_same_v<T, MapEntityComponent>) {
            auto it = mapEntityComponents.find(entity);
            return (it != mapEntityComponents.end()) ? it->second : nullptr;
        } else if constexpr (std::is_same_v<T, BuildingComponent>) {
            auto it = buildings.find(entity);
            return (it != buildings.end()) ? it->second : nullptr;
        } else if constexpr (std::is_same_v<T, OwnerComponent>) {
            auto it = owners.find(entity);
            return (it != owners.end()) ? it->second : nullptr;
        } else {
            static_assert(sizeof(T) == 0, "Component-Typ nicht unterstützt");
        }
    }

    const std::vector<EntityID>& getAllEntities() const { return entities; }
};



#endif //ENTITYMANAGER_H
