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

public class AudioPoolPlayer : UdonSharpBehaviour
{
    [Header("Audio Settings")]
    public AudioClip[] audioClips;
    public AudioSource audioSource;

    private int currentIndex = 0;

    void Start()
    {
        // Auto-find AudioSource if not assigned
        if (audioSource == null)
        {
            audioSource = GetComponent<AudioSource>();
        }
    }

    /// <summary>
    /// Plays the next audio clip in the round-robin sequence.
    /// Returns the index of the clip that was played, or -1 if failed.
    /// </summary>
    public int Play()
    {
        // Validate setup
        if (audioClips == null || audioClips.Length == 0)
        {
            Debug.LogWarning("[AudioPoolPlayer] No audio clips assigned!");
            return -1;
        }

        if (audioSource == null)
        {
            Debug.LogWarning("[AudioPoolPlayer] No AudioSource assigned!");
            return -1;
        }

        // Play current clip
        audioSource.PlayOneShot(audioClips[currentIndex]);
        int playedIndex = currentIndex;

        // Advance to next clip (round-robin)
        currentIndex = (currentIndex + 1) % audioClips.Length;

        return playedIndex;
    }
}
