using NBC;
using NBF.Utils;
using UnityEngine;
using UnityEngine.InputSystem;

namespace NBF
{
    public partial class FPlayer
    {
        #region Input

        private void AddInputEvent()
        {
            InputManager.OnPlayerPerformed += OnPlayerCanceled;
            InputManager.OnPlayerPerformed += OnPlayerPerformed;

            InputManager.OnPlayerValueCanceled += OnPlayerValueCanceled;
            InputManager.OnPlayerValuePerformed += OnPlayerValuePerformed;
        }

        private void RemoveInputEvent()
        {
            InputManager.OnPlayerPerformed += OnPlayerCanceled;
            InputManager.OnPlayerPerformed += OnPlayerPerformed;

            InputManager.OnPlayerValueCanceled += OnPlayerValueCanceled;
            InputManager.OnPlayerValuePerformed += OnPlayerValuePerformed;
        }

        private void OnPlayerPerformed(string action)
        {
            if (action == InputDef.Player.Run)
            {
                Data.Run = true;
            }
        }

        private void OnPlayerCanceled(string action)
        {
            if (action == InputDef.Player.Run)
            {
                Data.Run = false;
            }
            else if (action == InputDef.Player.ToBag)
            {
                //取消手持物品
                Log.Info($"取消手持物品");
                Game.Instance.StartCoroutine(UnUseItem());
            }
            else if (action.StartsWith(InputDef.Player.QuickStarts))
            {
                var index = int.Parse(action.Replace(InputDef.Player.QuickStarts, string.Empty));
                Log.Info($"快速使用===={index}");
                var item = RoleModel.Instance.GetSlotItem(index - 1);
                if (item != null)
                {
                    Game.Instance.StartCoroutine(UseItem(item));
                }
            }
        }

        private void OnPlayerValueCanceled(InputAction.CallbackContext context)
        {
            var actionName = context.action.name;
            if (actionName == InputDef.Player.Move)
            {
                // var v2 = context.ReadValue<Vector2>();
                Data.MoveInput = Vector2.zero;
                // SendMoveMessage(v2, true);
            }
        }

        private void OnPlayerValuePerformed(InputAction.CallbackContext context)
        {
            // var mapUnit = Parent as MapUnit;
            // Log.Info($"OnPlayerValuePerformed IsSelf={mapUnit.IsSelf()} id={mapUnit.Id}");
            var actionName = context.action.name;
            if (actionName == InputDef.Player.Move)
            {
                var v2 = context.ReadValue<Vector2>();
                Data.MoveInput = v2;
                // SendMoveMessage(v2, false);
            }
            else if (actionName == InputDef.Player.Look)
            {
                var v2 = context.ReadValue<Vector2>();
                // UpdatePlayerRotation(v2);
            }
        }

        #endregion
    }
}