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

[RequireComponent(typeof(AudioSource))]
public class SquadBehaviourAudio : MonoBehaviour
{
	[SerializeField]
	private List<AudioClip> _defendSounds;
	[SerializeField]
	private List<AudioClip> _marchSounds;
	[SerializeField]
	private List<AudioClip> _chargeSounds;
	[SerializeField]
	private AudioClip _reinforcementSound;

	private AudioSource _aSource;
	private Vector3 _audioPosition;

	void Start()
	{
		_aSource = GetComponent<AudioSource>();
	}

	public void DoBehaviourAudio(BoidController inController)
	{
		if (inController.Agents.Count > 0)
		{
			transform.position = inController.FlockPositionSum / inController.Agents.Count;

			if (inController.CurBehaviourType == BoidController.BehaviourType.Defend)
			{
				int soundCount = Random.Range(1, Mathf.Min(5, inController.Agents.Count + 1));
				StartCoroutine(PlaySounds(_defendSounds, soundCount));
			}
			else if (inController.CurBehaviourType == BoidController.BehaviourType.March)
			{
				int soundCount = Random.Range(1, Mathf.Min(5, inController.Agents.Count + 1));
				StartCoroutine(PlaySounds(_marchSounds, soundCount));
			}
			else if (inController.CurBehaviourType == BoidController.BehaviourType.Charge)
			{
				int soundCount = Random.Range(1, Mathf.Min(5, inController.Agents.Count + 1));
				StartCoroutine(PlaySounds(_chargeSounds, soundCount));
			}
		}
	}
	public void DoReinforcementAudio()
	{
		_aSource.PlayOneShot(_reinforcementSound);
	}
	private IEnumerator PlaySounds(List<AudioClip> soundList, int numSounds)
	{
		numSounds = 1;
		for(int i = 0; i < numSounds; i++)
		{
			//Play Random Sound from List
			_aSource.pitch = Random.Range(.8f, 1.1f);
			_aSource.PlayOneShot(soundList[Random.Range(0, soundList.Count)]);
			yield return new WaitForSeconds(Random.Range(.1f, .25f));
		}
	}
}
