using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class HeartBeatManager : MonoBehaviour
{
    public AudioClip highPace;
    public AudioClip mediumPace;
    public AudioClip lowPace;
    private AudioSource audioSource;

    void Start()
    {
        PubSubService<PlayerAIDistanceChanged>.GetInstance().Suscribe(PlayerAIDistanceChanged);
        this.audioSource = this.GetComponent<AudioSource>();
        if (this.audioSource == null || lowPace == null || mediumPace == null || highPace == null)
            throw new System.Exception("Fix dependencies (audiosources and audioclips)");
        this.audioSource.clip = lowPace;
        this.audioSource.loop = true;
    }

    void PlayerAIDistanceChanged(PlayerAIDistanceChanged message)
    {
        switch (message.distance)
        {
            case Distance.High:
                this.audioSource.clip = lowPace;
                break;
            case Distance.Low:
                this.audioSource.clip = highPace;
                break;
            case Distance.Medium:
                this.audioSource.clip = mediumPace;
                break;
        }
        this.audioSource.Play();
    }

    private void OnDestroy()
    {
        PubSubService<PlayerAIDistanceChanged>.GetInstance().Unsuscribe(PlayerAIDistanceChanged);
    }
}
