using EscapeRoomEngine.Engine.Runtime.Modules;
using EscapeRoomEngine.Engine.Runtime.Utilities;
using Station46.Modules.Hoop.Scripts;
using Station46.Scripts;

namespace Station46.Modules.Holes.Scripts
{
    /// <summary>
    /// The main component for the orb grid module.
    /// </summary>
    public class FlingHoles : Holes
    {
        private bool ShowHint
        {
            set
            {
                if (!Solved)
                {
                    var on = EscapeRoomEngine.Engine.Runtime.Engine.Theme.activeColor.hdr;
                    var off = EscapeRoomEngine.Engine.Runtime.Engine.Theme.puzzleColor.hdr;
                    
                    for (var i = 0; i < solution.Count; i++)
                    {
                        holes[i].Emission.color = value
                            ? solution[i] == 1 ? on : off
                            : states[i] == 1 ? on : off;
                    }
                }
            }
        }
        
        private Button _button;

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

            _button.ButtonEvent += (_, type) =>
            {
                ShowHint = type == ButtonEventType.Pressed;
            };

            // PuzzleEvent += (_, type) =>
            // {
            //     // ReSharper disable once SwitchStatementMissingSomeEnumCasesNoDefault
            //     switch (type)
            //     {
            //         case PuzzleEventType.Restarted:
            //             var color = EscapeRoomEngine.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 = EscapeRoomEngine.Engine.Runtime.Engine.Theme.solvedColor;
            //             var activeColor = EscapeRoomEngine.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 button fling hoop module
            var secondGradeRelatedModule = Module.relatedModules[0].relatedModules[0];
            if (secondGradeRelatedModule.State is ButtonHoop hoop)
            {
                _button = hoop.Button;
            }
            else
            {
                throw new EngineException("Holes were not assigned a related Dispenser.");
            }
        }
    }
}