using NaughtyAttributes;
using UnityEngine;

namespace Station46.Scripts
{
    /// <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");

        public bool hideWhenDisabled, startInactive;
        [BoxGroup("Internal")] [Required] public MeshRenderer holoRenderer;

        private Material _material;

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

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

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