﻿using UdonSharp;
using UnityEngine;
using VRC.SDKBase;
using VRC.Udon;
using VRC.Udon.Common;

[UdonBehaviourSyncMode(BehaviourSyncMode.None)]
public class HoldButton : UdonSharpBehaviour
{
    [Header("Hold Settings")]
    [Tooltip("Time in seconds the button must be held to trigger the event")]
    public float secondsToHold = 1.0f;

    [Header("Target Settings")]
    [Tooltip("The UdonSharpBehaviour to send the event to")]
    public UdonSharpBehaviour targetScript;
    [Tooltip("The name of the event to send")]
    public string eventName;

    [Header("UI Settings")]
    [Tooltip("The slider image RectTransform that will be scaled from 0 to 1")]
    public RectTransform sliderImage;

    // Private state
    private bool isHolding = false;
    private float holdProgress = 0f;
    private bool eventTriggered = false;
    private bool isLocalPlayerHolding = false;

    void Start()
    {
        // Hide slider on start
        if (sliderImage != null)
        {
            sliderImage.gameObject.SetActive(false);
        }
        else
        {
            Debug.LogWarning($"[HoldButton] No slider image assigned on {gameObject.name}");
        }

        // Validate setup
        if (targetScript == null)
        {
            Debug.LogWarning($"[HoldButton] No target script assigned on {gameObject.name}");
        }
        else if (string.IsNullOrEmpty(eventName))
        {
            Debug.LogWarning($"[HoldButton] No event name set on {gameObject.name}");
        }
    }

    void Update()
    {
        if (isHolding)
        {
            // Increment progress
            holdProgress += Time.deltaTime / secondsToHold;

            // Update slider visual
            if (sliderImage != null)
            {
                float displayProgress = Mathf.Clamp01(holdProgress);
                sliderImage.localScale = new Vector3(displayProgress, 1f, 1f);
            }

            // Check if hold is complete
            if (holdProgress >= 1.0f && !eventTriggered)
            {
                TriggerEvent();
                eventTriggered = true;
            }
        }
    }

    public override void Interact()
    {
        // Start holding
        isHolding = true;
        holdProgress = 0f;
        eventTriggered = false;
        isLocalPlayerHolding = true;

        // Show and reset slider
        if (sliderImage != null)
        {
            sliderImage.gameObject.SetActive(true);
            sliderImage.localScale = new Vector3(0f, 1f, 1f);
        }
    }

    public override void InputUse(bool value, UdonInputEventArgs args)
    {
        // Only process if this player is currently holding this button
        if (!isLocalPlayerHolding) return;

        // value == false means use button was released
        if (!value)
        {
            StopHolding();
        }
    }

    public void StopHolding()
    {
        isHolding = false;
        holdProgress = 0f;
        eventTriggered = false;
        isLocalPlayerHolding = false;

        // Hide slider
        if (sliderImage != null)
        {
            sliderImage.gameObject.SetActive(false);
        }
    }

    private void TriggerEvent()
    {
        if (targetScript == null)
        {
            Debug.LogError("[HoldButton] Cannot trigger event: targetScript is null");
            return;
        }

        if (string.IsNullOrEmpty(eventName))
        {
            Debug.LogError("[HoldButton] Cannot trigger event: eventName is null or empty");
            return;
        }

        targetScript.SendCustomEvent(eventName);
        
        // Reset the button state after triggering (act as if dropped)
        StopHolding();
    }
}