using System.Collections.Generic;
using Intrepid.Core.Messages.Events;
using Intrepid.GameManagers;
using Intrepid.Gameplay.Entities.Player.PDA.Events;
using Intrepid.Integration.Input;
using Intrepid.Structures;
using Intrepid.Utilities;
using Intrepid.Window.Data;
using Intrepid.Window.Events;
using UnityEngine;

namespace Intrepid.Gameplay.Entities.Player.Sentinel.StateMachine.States
{
    public class SentinelStatePDA : SentinelState
    {
        [GroupConfigurations, SerializeField] private AnimationClip pda;
        [GroupConfigurations, SerializeField] private WindowData pdaWindowData;
        
        public override SentinelStateType GetStateKey()
        {
            return SentinelStateType.PDA;
        }

        protected override void Subscriptions()
        {
            Subscribe<PDADeviceShutdownEvent>(_ =>
            {
                Brain.ChangeState(SentinelStateType.Idle);
            });
        }

        public override void RunOnFirstFrameOnly()
        {
            InputService.Instance.ChangeInputSchema(InputSchema.PDA);
            Proxy.Instance.EventBus.Raise(new FlexibleWorkableAreaPaddingEvent
            {
                Id = "pda.flexible-workable-area",
            });
            Proxy.Instance.EventBus.Raise(new WindowOpenEvent
            {
                ShouldStack = true,
                WindowDataList = new List<WindowData>
                {
                    pdaWindowData
                },
            });
        }

        protected override void RunOnEnter(ParameterStore ps)
        {
            InputService.Instance.ChangeInputSchema(InputSchema.None);
            Brain.Entity.DesiredVelocity = Vector2.zero;
            Brain.Entity.ModelAnimation.CrossFade(pda, 0.2f);
        }

        protected override void RunOnExit()
        {
            InputService.Instance.ChangeInputSchema(InputSchema.Sentinel);
            Proxy.Instance.EventBus.Raise(new WindowCloseEvent
            {
                WindowDataList = new List<WindowData>
                {
                    pdaWindowData
                },
            });
        }

        public override void RunOnUpdate()
        {
            if (Brain.InputResolver.PDA.ExitPDA())
            {
                Brain.ChangeState(SentinelStateType.Idle);
            }
        }
    }
}