using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//using UnityEngine.XR;
using UnityEngine.InputSystem;

public class HandPresence : MonoBehaviour
{
    [System.Serializable]
    public enum Hand
    {
        Left,
        Right
    }

    public bool showController = false;
  //  public InputDeviceCharacteristics controllerCharacteristics;
    public List<GameObject> controllerPrefabs;
    public GameObject handModel;

    private XRIDefaultInputActions action;
    [SerializeField] private Hand hand;

    float triggerValue = 0f;
    float gripValue = 0f;

    private Animator handAnimator;

    // Start is called before the first frame update
    void Awake()
    {
        action = new XRIDefaultInputActions();

        TryInitialize();
    }

    private void OnEnable()
    {


        if (hand == Hand.Left)
        {
            action.XRILeftHand.Select.Enable();
            action.XRILeftHand.Select.performed += GripPerformed;
            action.XRILeftHand.Select.canceled += GripPerformed;
            action.XRILeftHand.Activate.Enable();
            action.XRILeftHand.Activate.performed += TriggerPerformed;
            action.XRILeftHand.Activate.canceled += TriggerPerformed;
        }
        else
        {
            action.XRIRightHand.Select.Enable();
            action.XRIRightHand.Select.performed += GripPerformed;
            action.XRIRightHand.Select.canceled += GripPerformed;
            action.XRIRightHand.Activate.Enable();
            action.XRIRightHand.Activate.performed += TriggerPerformed;
            action.XRIRightHand.Activate.canceled += TriggerPerformed;
        }
    }

    private void OnDisable()
    {
        action.XRIRightHand.Select.performed -= GripPerformed;
        action.XRIRightHand.Activate.performed -= TriggerPerformed;
        action.XRILeftHand.Select.performed -= GripPerformed;
        action.XRILeftHand.Activate.performed -= TriggerPerformed;
    }

    private void TriggerPerformed(InputAction.CallbackContext obj)
    {
        triggerValue = obj.ReadValue<float>();

    }

    private void GripPerformed(InputAction.CallbackContext obj)
    {
        gripValue = obj.ReadValue<float>();
    }

    void TryInitialize()
    {

        handAnimator = handModel.GetComponent<Animator>();
    //    List<InputDevice> devices = new List<InputDevice>();

    //    InputDevices.GetDevicesWithCharacteristics(controllerCharacteristics, devices);

    //    foreach (var item in devices)
    //    {
    //        Debug.Log(item.name + item.characteristics);
    //    }

    //    if (devices.Count > 0)
    //    {
    //        targetDevice = devices[0];
    //        GameObject prefab = controllerPrefabs.Find(controller => controller.name == targetDevice.name);
    //        if (prefab)
    //        {
    //            spawnedController = Instantiate(prefab, transform);
    //        }
    //        else
    //        {
    //            Debug.Log("Did not find corresponding controller model");
    //        }

    //        spawnedHandModel = Instantiate(handModelPrefab, transform);
    //        
    //    }
    }

    void UpdateHandAnimation()
    {
        handAnimator.SetFloat("Trigger", triggerValue);
        handAnimator.SetFloat("Grip", gripValue);
        
    }

    // Update is called once per frame
    void Update()
    {
        UpdateHandAnimation();
    }
}
