using System.Collections.Generic;
using EscapeRoomEngine.Engine.Runtime.Modules;
using EscapeRoomEngine.Engine.Runtime.Utilities;
using NaughtyAttributes;
using UnityEngine;

namespace EscapeRoomEngine.Desert.Runtime.Puzzle_C
{
    /// <summary>
    /// The main component for the orb grid module.
    /// </summary>
    public class Holes : StatePuzzle
    {
        [BoxGroup("Internal")] [SerializeField]
        private Emission frameLight;
        
        private List<Hole> _holes;
        private Dispenser.Dispenser _dispenser;

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

            _holes = new List<Hole>(GetComponentsInChildren<Hole>());
        }

        protected override void Start()
        {
            base.Start();
            
            PuzzleEvent += (_, type) =>
            {
                // ReSharper disable once SwitchStatementMissingSomeEnumCasesNoDefault
                switch (type)
                {
                    case PuzzleEventType.Restarted:
                        var color = Engine.Runtime.Engine.Theme.puzzleColor;
                        _holes.ForEach(hole =>
                        {
                            hole.Enable();
                            hole.Emission.color = color.hdr;
                        });
                        frameLight.color = color.hdr;
                        _dispenser.Reset();
                        break;
                    case PuzzleEventType.Solved:
                        var solvedColor = Engine.Runtime.Engine.Theme.solvedColor;
                        var activeColor = Engine.Runtime.Engine.Theme.activeColor;
                        _holes.ForEach(hole =>
                        {
                            hole.Disable();
                            hole.Emission.color = solution[hole.number] == 1 ? activeColor.hdr : solvedColor.hdr;
                        });
                        frameLight.color = solvedColor.hdr;
                        _dispenser.Solve();
                        break;
                }
            };
            
            _holes.ForEach(hole => hole.ButtonEvent += (_, type) =>
            {
                // ReSharper disable once SwitchStatementMissingSomeEnumCasesNoDefault
                switch (type)
                {
                    case ButtonEventType.Pressed:
                        SetState(hole.number, 1, true);
                        break;
                    case ButtonEventType.Released:
                        SetState(hole.number, 0, true);
                        break;
                }
            });

            frameLight.active = true;
        }

        public override void SetModule(Module module)
        {
            base.SetModule(module);
            
            // The holes require a related dispenser module
            var firstRelatedModule = Module.relatedModules[0];
            if (firstRelatedModule.State is Dispenser.Dispenser dispenser)
            {
                _dispenser = dispenser;
            }
            else
            {
                throw new EngineException("Holes were not assigned a related Dispenser.");
            }
        }
    }
}