using System;
using NaughtyAttributes;
using UnityEngine;
using Logger = EscapeRoomEngine.Engine.Runtime.Utilities.Logger;
using LogType = EscapeRoomEngine.Engine.Runtime.Utilities.LogType;

namespace EscapeRoomEngine.Engine.Runtime.Modules
{
    /// <summary>
    /// The <see cref="PuzzleState"/> of a puzzle that includes a number of steps that need to be completed to solve it.
    /// </summary>
    public abstract class StepPuzzle : PuzzleState
    {
        [BoxGroup("Step Puzzle")]
        [InfoBox("In easy mode, the step puzzle will not reset if a wrong input is made.")]
        public bool easyMode;
        [BoxGroup("Step Puzzle")] [Min(0)]
        public int totalSteps;
        [BoxGroup("Step Puzzle")] [ProgressBar("Step", "totalSteps", EColor.Orange)]
        public int currentStep;

        protected override void Start()
        {
            PuzzleEvent += (_, type) =>
            {
                switch (type)
                {
                    case PuzzleEventType.Restarted:
                        currentStep = 0;
                        break;
                    case PuzzleEventType.Solved:
                        currentStep = totalSteps;
                        break;
                    case PuzzleEventType.WrongInput:
                        if (!easyMode)
                        {
                            currentStep = 0;
                        }
                        break;
                    default:
                        throw new ArgumentOutOfRangeException(nameof(type), type, null);
                }
            };
            
            base.Start();
        }

        protected virtual void CheckStep(int step)
        {
            if (!Solved)
            {
                if (step == currentStep)
                {
                    Step();
                }
                else
                {
                    WrongInput();
                }
            }
        }

        #region Debug Buttons

        [Button(enabledMode: EButtonEnableMode.Playmode)]
        protected void Step()
        {
            if (!Solved)
            {
                currentStep++;
                if (currentStep >= totalSteps)
                {
                    Solve();
                }
                else
                {
                    Logger.Log($"{this} step {currentStep}/{totalSteps}", LogType.PuzzleDetail);
                }
            }
        }

        #endregion
    }
}