using System;
using System.Collections.Generic;
using System.Linq;
using Unity.VisualScripting;
using UnityEngine;

public class ObjReferentials : MonoBehaviour
{
    [Serializable]
    public struct ObjToRender
    {
        public String name;
        public GameObject objToRender;
    }

    public ObjToRender[] ObjToRenders;
    public HashSet<ObjToRender> InstantiatesElements = new HashSet<ObjToRender>();


    public void EnableElementRender(String elementName)
    {
        foreach (var objToRender in ObjToRenders)
        {
            if (objToRender.name == elementName)
            {
                if (InstantiatesElements.Any(render => render.name == elementName))
                {
                    // element are already instantiate !
                }
                else
                {
                    Vector3 cameraPosition = Camera.main.transform.position;

                    // Obtenez la rotation de la caméra sans inclure la rotation autour de l'axe Y
                    Quaternion cameraRotation = Quaternion.Euler(0, Camera.main.transform.rotation.eulerAngles.y, 0);

                    // Calculez la position finale en ajoutant la direction vers l'avant multipliée par la distance désirée
                    Vector3 position = cameraPosition + cameraRotation * Vector3.forward * 0.2f;

                    var instantiatedObj = Instantiate(objToRender.objToRender, position, cameraRotation);
                    instantiatedObj.name = objToRender.name;
                    InstantiatesElements.Add(new ObjToRender() {name = objToRender.name, objToRender = instantiatedObj});
                }
            }
        }
    }

    public void DisableElementRender(String elementName)
    {
        foreach (var objToRender in InstantiatesElements)
        {
            if (objToRender.name == elementName)
            {
                InstantiatesElements.Remove(objToRender);
                Destroy(objToRender.objToRender);
                return;
            }
        }
    }
}
