using UnityEngine;
using UnityEngine.UI;

public class PlayAudio : MonoBehaviour
{
    [Header("Audio Clip yang akan dimainkan")]
    public AudioClip audioClip;

    [Header("Audio Source (boleh di object lain)")]
    public AudioSource audioSource;

    [Header("Komponen Image Button Audio")]
    public Image audioButtonImage; // Referensi ke Image pada button

    [Header("Sprite untuk Icon Audio")]
    public Sprite playSprite;  // Icon saat audio tidak bermain (tombol play)
    public Sprite pauseSprite; // Icon saat audio sedang bermain (tombol pause)

    // Method untuk toggle play/pause dan mengganti icon
    public void ToggleAudio()
    {
        if (audioSource == null)
        {
            Debug.LogWarning("AudioSource belum di-assign!");
            return;
        }

        if (audioSource.isPlaying)
        {
            audioSource.Pause();
            if (audioButtonImage != null && playSprite != null)
            {
                audioButtonImage.sprite = playSprite;
            }
        }
        else
        {
            if (audioClip != null)
            {
                audioSource.clip = audioClip;
            }
            audioSource.Play();
            if (audioButtonImage != null && pauseSprite != null)
            {
                audioButtonImage.sprite = pauseSprite;
            }
        }
    }

    // Method untuk mereset audio (stop/pause) dan mengubah icon ke play
    public void ResetAudio()
    {
        if (audioSource != null)
        {
            audioSource.Pause(); // atau audioSource.Stop(); sesuai kebutuhan
        }
        if (audioButtonImage != null && playSprite != null)
        {
            audioButtonImage.sprite = playSprite;
        }
    }
}
