using System;
using System.Threading;
using Cysharp.Threading.Tasks;
using RebootKit.Engine.Foundation;
using UnityEngine;
using UnityEngine.InputSystem;

namespace RebootKit.Engine.Services.Input {
    public class InputService : IService {
        [Serializable]
        public class Config {
            public InputActionAsset InputAsset;
        }

        private Config _config;

        public InputService(Config config) {
            _config = config;
        }
        
        public void Dispose() {
        }

        public async UniTask OnInit(CancellationToken cancellationToken) {
            await UniTask.Yield(cancellationToken);
        }

        public void EnableControls() {
            _config.InputAsset.Enable();
        }

        public void DisableControls() {
            _config.InputAsset.Disable();
        }

        public InputAction FindInputAction(string path) {
            return _config.InputAsset.FindAction(path);
        }

        public void LockCursor() {
            Cursor.lockState = CursorLockMode.Locked;
            Cursor.visible = false;
        }

        public void UnlockCursor() {
            Cursor.lockState = CursorLockMode.None;
            Cursor.visible = true;
        }
    }
}