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

public class SFXPlayer : UdonSharpBehaviour
{
    [Header("Audio Settings")]
    public AudioPoolPlayer audio;
    public bool playSoundForEveryone = false;

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

    public void Play()
    {
        // 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();
            }
        }
    }

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