using System;
using Intrepid.GameManagers;
using Intrepid.Gameplay.Entities.Player.PDA.Events;
using Intrepid.Gameplay.Entities.Player.PDA.Inputs;
using Intrepid.SimpleEvents;
using Intrepid.Structures;
using Intrepid.Utilities;
using UnityEngine;

namespace Intrepid.Gameplay.Entities.Player.PDA.UI
{
    public abstract class PDAApplicationUI : Identification, IPDAApplicationUpdater, IPDASystemBootEvents, IPDASystemShutdownEvents
    {
        [SerializeField, GroupConfigurations] private PDADevice device;
        [SerializeField, GroupConfigurations] private PDAContentTag contentTag;

        protected PDADevice Device => device;
        public PDAContentTag ContentTag => contentTag;
        
        public abstract bool IsLoading { get; }
        public abstract void UpdateTheme();
        public abstract void UpdateApplication(PDAInputs inputs);
        public abstract void LateUpdateApplication(PDAInputs inputs);
        protected abstract void Subscribing();
        protected abstract void WhenStarting(ParameterStore parameters);
        protected abstract void WhenFinishing();
        
        public void WhenEntering()
        {
            Subscribing();
            UpdateTheme();
            WhenStarting(device.ParameterStore);
        }

        public void WhenExiting()
        {
            WhenFinishing();
            Proxy.Instance.EventBus.UnSubscribe(Id);
        }

        public virtual void WhenPreparingSystemBoot()
        {
        }

        public virtual void WhenRunningSystemBoot()
        {
        }

        public virtual void WhenPreparingSystemShutdown()
        {
            Proxy.Instance.EventBus.Raise(new PDAExitCurrentApplicationEvent());
        }

        public virtual void WhenRunningSystemShutdown()
        {
        }
        
        protected void Subscribe<TEvent>(Action<TEvent> action) where TEvent : IEventBase
        {
            Proxy.Instance.EventBus.Subscribe(Id, action);
        }
    }
}