using System;
using UnityEngine;
using UnityEngine.UI;

namespace OCES.Haptic
{
    public class HapticInvoker : MonoBehaviour
    {
        public InputField inputField;
        public Button button;

        uint m_hapticId;
        
        void OnEnable()
        {
            this.inputField.onSubmit.AddListener(GetCurrentHapticId);
            this.inputField.onEndEdit.AddListener(GetCurrentHapticId);
            this.button.onClick.AddListener(InvokeHaptic);
        }

        void OnDisable()
        {
            this.inputField.onSubmit.RemoveListener(GetCurrentHapticId);
            this.inputField.onEndEdit.RemoveListener(GetCurrentHapticId);
            this.button.onClick.RemoveListener(InvokeHaptic);
        }
        
        void GetCurrentHapticId(string value)
        {
            if(string.IsNullOrEmpty(value)) return;
            
            this.m_hapticId = uint.Parse(value); 
        }

        void InvokeHaptic()
        {
            HapticSystem.Instance.Play(this.m_hapticId);
        }
    }
}
