using Intrepid.FSM;
using Intrepid.Integration.Input;
using Intrepid.Theatre.BackStage;
using Intrepid.Theatre.Curtain;
using Intrepid.Utilities;
using UnityEngine;

namespace Intrepid.Core.GameState
{
    public class GameStateMachine : Identification, IBackStageProcessor
    {
        [GroupConfigurations, SerializeField] private GameState bootstrapState;
        [GroupConfigurations, SerializeField] private BackStageService backStageService;
        
        private FStateMachine<GameState, GameStateObject> StateMachine { get; } = new ();
        private GameState GameStateToChange { get; set; }
        private BackStageService BackStageService => backStageService;

        private void Awake()
        {
            foreach (var gameStateObject in gameObject.GetComponentsInChildren<GameStateObject>())
            {
                StateMachine.AddState(gameStateObject);
            }
            StateMachine.InitializeWith(bootstrapState);
        }

        private void Start()
        {
            StateMachine.Start();
        }

        private void Update()
        {
            BackStageService.UpdateCurtains();
            StateMachine.GetCurrentState().Implementation().UpdateState();
        }

        public void ChangeTo(GameState gameState, CurtainTypeTag curtainType, BackStageProcessorAggregator processorAggregator)
        {
            if (BackStageService.KickProcess(curtainType, Id, processorAggregator))
            {
                GameStateToChange = gameState;
            }
        }

        public bool IsProcessFinished => StateMachine.GetCurrentState().Implementation().IsLoadingFinished;
        
        public void AfterOpened()
        {
            InputService.Instance.Locking.UnLock();
        }

        public void BeforeOpening()
        {
        }

        public void AfterClosed()
        {
            StateMachine.ChangeState(GameStateToChange);
        }

        public void BeforeClosing()
        {
            InputService.Instance.Locking.Lock();
        }
    }
}