using UnityEngine;
using System.Collections.Generic;

public class ModelManager : MonoBehaviour
{
    private Dictionary<string, HashSet<ChildModel>> portDict; //port mapped on 
    private Dictionary<string, ChildModel> childModelDict; //childModels by their ID

    public List<BaseModel> baseModelList; //available BaseModels

    public GameObject baseModel; //current baseModel
    public BaseModelSelector baseModelSelector;

    // Start is called once before the first execution of Update after the MonoBehaviour is created
    void Start()
    {
        Debug.Log("ModelManager: " + this.gameObject.name);
        portDict = new Dictionary<string, HashSet<ChildModel>>();
        childModelDict = new Dictionary<string, ChildModel>();
        baseModelList = new List<BaseModel>();
        //baseModelSelector = FindFirstObjectByType<BaseModelSelector>();
        
        foreach (var baseModel in ModelList.BaseModels)
        {
            registerBaseModel(baseModel);
        }

        foreach (var childModel in ModelList.ChildModels)
        {
            registerChildModel(childModel);
        }
        if (baseModel == null) //set in the editor
        {
            //TODO: send into model selection
            Debug.LogError("baseModel is null, fix the configuration");
        }
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    void registerChildModel(ChildModel model)
    {
        //childModelDict
        if (childModelDict.ContainsKey(model.nameId))
        {
            Debug.LogWarning("Model " + model.nameId + " already registered! [childModelDict] Skipping...");
            return;
        }
        childModelDict[model.nameId] = model;

        //portDict
        if (!portDict.ContainsKey(model.port))
        {
            portDict.Add(model.port, new HashSet<ChildModel>());
        }
        if (portDict[model.port].Contains(model))
        {
            Debug.LogWarning("Model " + model.nameId + " already registered! [portDict] Skipping...");
            return;
        }
        portDict[model.port].Add(model);
    }

    void registerBaseModel(BaseModel baseModel)
    {
        if (baseModel == null)
        {
            return;
        }
        baseModelList ??= new List<BaseModel>();
        baseModelList.Add(baseModel);
    }

    public HashSet<ChildModel> getChildModelsForPort(string port)
    {
        // Überprüfen, ob der Schlüssel im Dictionary vorhanden ist
        if (portDict.TryGetValue(port, out HashSet<ChildModel> childModels))
        {
            return childModels; // Gibt die ChildModels zurück, die zu diesem Port gehören
        }
        else
        {
            // Wenn der Schlüssel nicht gefunden wird, gebe eine Warnung aus und gebe null oder ein leeres Set zurück
            Debug.LogWarning($"Kein Modell für den Port {port} gefunden.");
            return new HashSet<ChildModel>(); // Leeres Set zurückgeben
        }
    }

    public ChildModel getById(string id)
    {
        return childModelDict[id];
    }
    
    public void LoadSelectedModel(BaseModel baseModel)
    {
        if (this.baseModel == null)
        {
            // Neues GameObject für das BaseModel erstellen
            this.baseModel = new GameObject(baseModel.nameId)
            {
                name = baseModel.nameId
            };
            this.baseModel.AddComponent<BaseModelBehaviour>();
            // Positionieren
            this.baseModel.transform.position = new Vector3(0, 0, 1); 
            this.baseModel.transform.rotation = Quaternion.Euler(0, 90, 0);
            this.baseModel.SetActive(true);
        } 
        
        this.baseModel.GetComponent<BaseModelBehaviour>().baseModel = baseModel;
        this.baseModel.name = baseModel.nameId;
        this.baseModel.SetActive(true);
        Debug.Log($"Modell {baseModel.nameHuman} geladen und platziert.");
    }


    
}
    



