using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
/// <summary>
/// Handles what happens when the user clicks on a collider
/// </summary>
public class ClickBehavior : MonoBehaviour
{
    private void Update()
    {
        Vector2 clickPoint = Camera.main.ScreenToWorldPoint(Input.mousePosition);

        if (Input.GetMouseButton(0))
        {
            List<Collider2D> listColliders = new(Physics2D.OverlapCircleAll(clickPoint, 0.05f));
            List<Collider2D> resourceColliders = listColliders.FindAll(obj => obj.CompareTag("Resource"));
            if (resourceColliders.Count > 0)
            {
                foreach (Collider2D collider in resourceColliders)
                {
                    collider.GetComponent<ResourceMaker>().ClickOnResource();
                }

            }
        }
/*#if UNITY_EDITOR
        if (Input.GetMouseButtonUp(0))
        {
            try
            {
                const float clickRangeForSpawn = 0.7f;
                var spawners = LevelManager.Instance.GetAll<SpawnerTile>();
                var closest = spawners.Minimum(spawn => Vector2.Distance(spawn.Position, transform.position));
                var distance = Vector2.Distance(clickPoint, closest.Position);

                if (distance < clickRangeForSpawn)
                {
                    closest.TriggerSpawnDebug();
                }
            }
            catch (Exception e)
            {
                Console.Out.WriteLine(e.ToString());
            }

        }
#endif*/
    }

    /// <summary>
    /// Remet la vitesse de jeux a son etat normal lorsque le dernier ennemi est mort, si necessaire
    /// </summary>
    public static void ResetGameSpeed()
    {
        GameObject.Find("btn_speedup").GetComponent<Button>().image.color = Color.white;
        Time.timeScale = PauseScript.regularSpeed;
    }

}
