using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class DeselectManager : SingletonBehaviour<DeselectManager>
{
    private GameObject _allyUpgrade;
    [SerializeField]
    private Canvas _canvas;

    private void Update()
    {
        if (Input.GetMouseButton(0) && _allyUpgrade != null)
        {
            // Vérifie si click est sur un enfant d'AllyUpgrade
            PointerEventData pointerData = new PointerEventData(EventSystem.current)
            {
                position = Input.mousePosition
            };
            List<RaycastResult> results = new List<RaycastResult>();
            GraphicRaycaster raycaster = _canvas.GetComponent<GraphicRaycaster>();
            raycaster.Raycast(pointerData, results);
            foreach (RaycastResult result in results)
            {
                if (result.gameObject.transform.IsChildOf(_allyUpgrade.transform))
                {
                    return;
                }
            }
            SetAllyUpgrade(null);
        }
    }

    public void SetAllyUpgrade(GameObject allyUpgradeInstance)
    {
        if (_allyUpgrade == null)
        {
            _allyUpgrade = allyUpgradeInstance;

        }
        else if (_allyUpgrade != allyUpgradeInstance)
        {
            Destroy(_allyUpgrade);
            _allyUpgrade = allyUpgradeInstance;
        }
    }
}
