using EscapeRoomEngine.Engine.Runtime.Modules.State;
using JetBrains.Annotations;
using NaughtyAttributes;
using Station46.Scripts;
using UnityEngine;

namespace Station46.Modules.Portal.Scripts
{
    /// <summary>
    /// The Station 46 theme includes its own version of a portal that changes colour when it is unlocked.
    /// </summary>
    [RequireComponent(typeof(Emission))]
    public class Station46Portal : EscapeRoomEngine.Portal.Runtime.Portal
    {
        [BoxGroup("Internal")] [SerializeField] [CanBeNull] private LineRenderer beacon;
        [BoxGroup("Internal")] [SerializeField] [CanBeNull] private AudioSource beaconAudio;
        
        private Emission _emission;

        protected override void Awake()
        {
            base.Awake();
            
            _emission = GetComponent<Emission>();
            
            DoorEvent += (_, type) =>
            {
                // ReSharper disable once SwitchStatementMissingSomeEnumCasesNoDefault ConvertSwitchStatementToSwitchExpression
                switch (type)
                {
                    case DoorEventType.Unlocked:
                        _emission.color = EscapeRoomEngine.Engine.Runtime.Engine.Theme.solvedColor.hdr;
                        if (beacon != null)
                        {
                            beacon.enabled = true;
                        }
                        if (beaconAudio != null)
                        {
                            beaconAudio.Play();
                        }
                        break;
                    case DoorEventType.Locked:
                        _emission.color = EscapeRoomEngine.Engine.Runtime.Engine.Theme.puzzleColor.hdr;
                        if (beacon != null)
                        {
                            beacon.enabled = false;
                        }
                        if (beaconAudio != null)
                        {
                            beaconAudio.Stop();
                        }
                        break;
                }
            };

            _emission.active = true;
        }
    }
}