using EscapeRoomEngine.Desert.Runtime.Dispenser;
using UnityEngine;

namespace EscapeRoomEngine.Desert.Runtime.Puzzle_C
{
    /// <summary>
    /// A hole used in the <see cref="Holes"/> module.
    /// </summary>
    [RequireComponent(typeof(Emission), typeof(Collider))]
    public class Hole : Button
    {
        [Tooltip("The number of this hole, starting from the bottom left.")]
        [Min(0)]
        public int number;

        public Emission Emission { get; private set; }

        private void Awake()
        {
            Emission = GetComponent<Emission>();
        }

        protected override void Start()
        {
            base.Start();

            Emission.active = true;
        }

        private void OnTriggerEnter(Collider other)
        {
            var orb = other.GetComponent<DispenserOrb>();
            if (orb != null)
            {
                if (Active)
                {
                    var color = Engine.Runtime.Engine.Theme.activeColor;
                    
                    orb.Color = color.hdr;
                    Emission.color = color.hdr;
                    Press();
                }
            }
        }

        private void OnTriggerExit(Collider other)
        {
            var orb = other.GetComponent<DispenserOrb>();
            if (orb != null)
            {
                var color = Engine.Runtime.Engine.Theme.puzzleColor;
                
                if (Active)
                {
                    Release();
                    Emission.color = color.hdr;
                }
                orb.Color = color.hdr;
            }
        }
    }
}