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

public enum NetworkMode
{
    Local,          // Execute only on the player who clicked
    NetworkAll,     // Execute on all players
    NetworkOwner    // Execute only on the owner/master
}

public class VRCButton : UdonSharpBehaviour
{


    [Header("Target Settings")]
    public UdonSharpBehaviour targetScript;
    public string eventName;

    [Header("Network Settings")]
    public NetworkMode networkMode = NetworkMode.NetworkAll;

    [Header("SFX Player (separate GameObject)")]
    public SFXPlayer sfxPlayer;

    [Header("Sound Settings")]
    public AudioPoolPlayer audio;
    public bool playSoundForEveryone = false;

    [Header("Particle Settings")]
    public ParticleSystem particles;
    public NetworkMode particleNetworkMode = NetworkMode.Local;

    [Header("Animation Settings")]
    public Animator animator;
    public string animationTriggerName = "Play";
    public NetworkMode animationNetworkMode = NetworkMode.Local;


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

    public override void Interact()
    {
        if (targetScript == null)
        {
            Debug.LogError("[VRCButton] Cannot interact: targetScript is null");
            return;
        }

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

        // Send the event based on network mode
        switch (networkMode)
        {
            case NetworkMode.Local:
                targetScript.SendCustomEvent(eventName);
                break;
            case NetworkMode.NetworkAll:
                targetScript.SendCustomNetworkEvent(VRC.Udon.Common.Interfaces.NetworkEventTarget.All, eventName);
                break;
            case NetworkMode.NetworkOwner:
                targetScript.SendCustomNetworkEvent(VRC.Udon.Common.Interfaces.NetworkEventTarget.Owner, eventName);
                break;
        }

        // SFX: prefer external SFXPlayer (on always-active GameObject) over direct components
        if (sfxPlayer != null)
        {
            sfxPlayer.Play();
        }
        else
        {
            // Particle playback logic
            if (particles != null)
            {
                switch (particleNetworkMode)
                {
                    case NetworkMode.Local:
                        PlayParticles();
                        break;
                    case NetworkMode.NetworkAll:
                        PlayParticles();
                        SendCustomNetworkEvent(VRC.Udon.Common.Interfaces.NetworkEventTarget.Others, nameof(PlayParticles));
                        break;
                    case NetworkMode.NetworkOwner:
                        if (Networking.IsOwner(gameObject))
                            PlayParticles();
                        break;
                }
            }

            // Sound playback logic
            if (audio != null)
            {
                if (playSoundForEveryone)
                {
                    audio.SendCustomNetworkEvent(VRC.Udon.Common.Interfaces.NetworkEventTarget.All, nameof(audio.Play));
                }
                else
                {
                    audio.Play();
                }
            }

            // Animation playback logic
            if (animator != null && !string.IsNullOrEmpty(animationTriggerName))
            {
                switch (animationNetworkMode)
                {
                    case NetworkMode.Local:
                        PlayAnimation();
                        break;
                    case NetworkMode.NetworkAll:
                        PlayAnimation();
                        SendCustomNetworkEvent(VRC.Udon.Common.Interfaces.NetworkEventTarget.Others, nameof(PlayAnimation));
                        break;
                    case NetworkMode.NetworkOwner:
                        if (Networking.IsOwner(gameObject))
                            PlayAnimation();
                        break;
                }
            }
        }

    }

    public void PlayParticles()
    {
        if (particles != null)
        {
            particles.Stop();
            particles.Play();
        }
    }

    public void PlayAnimation()
    {
        if (animator != null && !string.IsNullOrEmpty(animationTriggerName))
        {
            animator.SetTrigger(animationTriggerName);
        }
    }
}
