using Intrepid.Diagnostics;
using Intrepid.Utilities;
using UnityEngine;
using UnityEngine.InputSystem;

namespace Intrepid.Integration.Input
{
    [CreateAssetMenu(fileName = "Input Action Service", menuName = "GON/Input Actions Service")]
    public class InputService : SingletonScriptableObject<InputService>
    {
        public InputActions InputActions { get; private set; }
        public InputSchema CurrentInputSchema { get; private set; }
        public InputLocking Locking { get; private set; }

        protected override void WhenInstantiate(InputService singleton)
        {
            singleton.InputActions = new InputActions();
            singleton.Locking = new InputLocking();
            singleton.CurrentInputSchema = InputSchema.None;
        }

        public void OnlyGamepad(bool onlyGamepad)
        {
            if (onlyGamepad)
            {
                InputSystem.DisableDevice(Keyboard.current);
                InputSystem.DisableDevice(Mouse.current);
            }
            else
            {
                InputSystem.EnableDevice(Keyboard.current);
                InputSystem.EnableDevice(Mouse.current);
            }
        }

        public void ChangeInputSchema(InputSchema schema)
        {
            if (schema == CurrentInputSchema) return;
            CurrentInputSchema = schema;
            switch (schema)
            {
                case InputSchema.None:
                    InputActions.Disable();
                    InputActions.PDA.Disable();
                    break;
                case InputSchema.Sentinel:
                    InputActions.sentinel.Enable();
                    InputActions.PDA.Disable();
                    break;
                case InputSchema.PDA:
                    InputActions.sentinel.Disable();
                    InputActions.PDA.Enable();
                    break;
                default:
                    InputActions.Disable();
                    CurrentInputSchema = InputSchema.None;
                    DLogger.LogError($"Unknown input schema: {schema}");
                    break;
            }
        }

    }
}