using System;
using UnityEngine;
using UnityEngine.InputSystem;
using Core;

namespace Script.Gameplay.Input
{
    public class InputManager : MonoSingleton<InputManager>, IInputManager
    {
        public PlayerInputActions Input; // 自动生成的输入类

        // 当前输入值
        public Vector2 Move { get; private set; }
        public Vector2 Look { get; private set; }
        public bool JumpPressed { get; private set; }
        public bool PausePressed { get; private set; }
        public bool InteractPressed { get; private set; }
        public bool EditPressed { get; private set; }
        
        private bool _hasFocus = true;

        protected override void Awake()
        {
            Input = new PlayerInputActions();
            base.Awake();
        }

        private void OnEnable()
        {
            Input.Enable();
        }

        private void OnDisable()
        {
            // 可选：取消注册以防止重复订阅（简单项目可不解除）
            Input.Disable();
        }

        private void Start()
        {
            // 注册事件
            Input.Player.Move.performed += ctx => Move = ctx.ReadValue<Vector2>();
            Input.Player.Move.canceled += ctx => Move = Vector2.zero;

            Input.Player.Look.performed += ctx => Look = ctx.ReadValue<Vector2>();
            Input.Player.Look.canceled += ctx => Look = Vector2.zero;

            Input.Player.Jump.performed += ctx => JumpPressed = true;
            Input.Player.Jump.canceled += ctx => JumpPressed = false;

            Input.Player.Pause.performed += ctx => PausePressed = true;
            Input.Player.Pause.canceled += ctx => PausePressed = false;
            
            Input.Player.Interact.performed += ctx => InteractPressed = true;
            Input.Player.Interact.canceled += ctx => InteractPressed = false;
            
            Input.Player.Edit.performed += ctx => EditPressed = true;
            Input.Player.Edit.canceled += ctx => EditPressed = false;
            
            SetCursorState(false, CursorLockMode.Locked);
            SetInputForLook(true);
            SetInputForMove(true);
            
            UIManager.Instance.RegisterInputManager(this);
        }

        private void Update()
        {
            // 在此更新一次性触发的输入，例如“按下瞬间触发”
            // if (PausePressed)
            // {
            //     Debug.Log("Pause Pressed!");
            //     PausePressed = false; // 手动清除（如果需要瞬时）
            // }
        }

        // 🔧 示例方法：允许外部模块手动启用/禁用输入（比如暂停菜单）
        public void SetInputEnabled(bool enabled)
        {
            if (enabled) Input.Enable();
            else Input.Disable();
        }

        public void SetCursorState(bool visible, CursorLockMode lockMode)
        {
            Cursor.visible = visible;
            Cursor.lockState = lockMode;
        }

        public void SetInputForLook(bool enabled)
        {
            if (enabled)
            {
                Input.Player.Look.Enable();
            }
            else
            {
                Input.Player.Look.Disable();
                Look = Vector2.zero; // 禁用时清除Look值
            }
        }

        public void SetInputForMove(bool enabled)
        {
            if (enabled)
            {
                Input.Player.Move.Enable();
            }
            else
            {
                Input.Player.Move.Disable();
                Move = Vector2.zero; // 禁用时清除Move值
            }
        }

        // 当窗口获得/失去焦点时调用（玩家是否在注视游戏窗口）
        private void OnApplicationFocus(bool hasFocus)
        {
            _hasFocus = hasFocus;
            // 失去焦点视为系统级暂停
            if (_hasFocus)
            {
                SetCursorState(_hasFocus, CursorLockMode.Locked);
            }
            else
            {
                SetCursorState(_hasFocus, CursorLockMode.None);
            }
        }
    }
}
