using NaughtyAttributes;
using UnityEngine;

namespace EscapeRoomEngine.Desert.Runtime
{
    /// <summary>
    /// A holographic <see cref="Button"/> that changes its colour when it is pressed.
    /// </summary>
    public class HoloButton : Button
    {
        private static readonly int FresnelColor = Shader.PropertyToID("_FresnelColor");
        private static readonly int Color = Shader.PropertyToID("_Color");

        [BoxGroup("Internal")] [Required] public MeshRenderer holoRenderer;

        private Material _material;

        private void Awake()
        {
            _material = holoRenderer.material;
        }

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

            ButtonEvent += (_, _) =>
            {
                var color = Engine.Runtime.Engine.Theme.puzzleColor;
                if (!Active)
                {
                    color = Engine.Runtime.Engine.Theme.solvedColor;
                } else if (Pressed)
                {
                    color = Engine.Runtime.Engine.Theme.activeColor;
                }
                
                _material.SetColor(FresnelColor, color.hdr);
                _material.SetColor(Color, color.hdr);
            };
        }
    }
}