using System;
using Intrepid.FSM;
using Intrepid.Structures;
using UnityEngine;

namespace Intrepid.Gameplay.Journey
{
    [Serializable]
    public class StageLoadingService : IState<StageTag, StageLoadingService>
    {
        private StageTag StageTag { get; }
        
        public StageLoadingService(StageTag stageTag)
        {
            StageTag = stageTag;
        }
        
        private AsyncOperation AsyncOperation { get; set; }
        public bool IsLoadingFinished => AsyncOperation.isDone;
        
        public bool CanEnterState => true;
        public bool CanExitState => true;

        public StageTag GetStateKey()
        {
            return StageTag;
        }

        public StageLoadingService Implementation()
        {
            return this;
        }

        public void OnEnterState(ParameterStore ps)
        {
            if (StageTag.IsReadyToBeLoaded())
            {
                AsyncOperation = StageTag.LoadAsync();
            }
        }

        public void OnExitState()
        {
            StageTag.UnloadAsync();
        }
    }
}