using UnityEngine;
using UVRPN.Events;

namespace UVRPN.Core
{
    /// <summary>
    /// This component receives button event via VRPN_NativeBridge and distributes them by the given UnityEvents.
    /// </summary>
    public sealed class VRPN_Button : VRPN_Client
    {
        [SerializeField] private bool debugLog;

        /// <summary>
        /// This is true during the frame the button is pressed down.
        /// </summary>
        public bool ButtonDown { get; private set; }
        
        /// <summary>
        /// This is true when the button is currently down.;
        /// </summary>
        public bool ButtonHold { get; private set; }

        /// <summary>
        /// This is true during the frame the button is released.
        /// </summary>
        public bool ButtonUp { get; private set; }

        [Header("Events")] 
        [Tooltip("This is triggered when a button is pressed.")]
        public ButtonEvent OnButtonUp = new ButtonEvent();

        [Tooltip("This is triggered when a button is released.")] 
        public ButtonEvent OnButtonDown = new ButtonEvent();

        [Tooltip("This is triggered every frame as long as the button is pressed.")]
        public ButtonEvent OnButtonHold = new ButtonEvent();

        private void Start()
        {
            if (debugLog)
            {
                OnButtonDown.AddListener((int c) => print("Button " + c + " Down"));
                OnButtonUp.AddListener((int c) => print("Button " + c + " Up"));
                OnButtonHold.AddListener((int c) => print("Button " + c + " Hold"));
            }
        }

        public void AssignButtonData(string Tracker, int Channel)
        {
            tracker = Tracker;
            channel = Channel;
        }

        //added messages 

        private void Update()
        {            
            ButtonUp = ButtonDown = false;
            
            //tracker is the seat name
            var pressed = host.IsButtonPressed(tracker, channel);
            
            if (ButtonHold)
            {
                if (pressed)
                {
                    OnButtonHold.Invoke(channel);
                    BroadcastMessage("OnButtonHold", channel, SendMessageOptions.DontRequireReceiver);
                }

                else
                {
                    ButtonUp = true;
                    OnButtonUp.Invoke(channel);
                    BroadcastMessage("OnButtonUp", channel, SendMessageOptions.DontRequireReceiver);
                }
            }
            else if (pressed)
            {
                ButtonDown = true;
                OnButtonDown.Invoke(channel);
                BroadcastMessage("OnButtonDown", channel, SendMessageOptions.DontRequireReceiver);
            }

            ButtonHold = pressed;
        }
    }
}



public interface IVPRNButton_Listener
{
    //this int is the CHANNEL index, in otherwords, the button number. Not a value
    public void OnButtonUp(int i); 
    public void OnButtonDown(int i);
    public void OnButtonHold(int i);
}