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

public class SoundSystem : MonoBehaviour, ITestEvent
{
    //[SerializeField]
    //private List<AudioClip> m_hardStepSounds = new List<AudioClip>();

    void Start ()
    {
        EventUtil.Subscribe<ITestEvent>(gameObject);
    }

    public void OnStep(GameObject steppingObject, GameObject steppedOnObject)	
    {
        //switch(steppedOnObject.tag)
        //{
        //    case "SandSurface":
        //        steppingObject.transform.Find("CharacterAudio/SandStepAudio").GetComponent<AudioSource>().Play();
        //        break;
        //    case "HardSurface":
        //        var hardStepSoundObj = steppingObject.transform.Find("CharacterAudio/HardStepSound");
        //        if (hardStepSoundObj)
        //            PlayRandom(m_hardStepSounds, hardStepSoundObj.gameObject);
        //        break;
        //}
    }

    private void PlayRandom(List<AudioClip> sounds, GameObject source)
    {
        if (sounds.Count > 0)
        {
            int random = UnityEngine.Random.Range(0, sounds.Count - 1);
            var audioSource = source.GetComponent<AudioSource>();
            if (audioSource)
            {
                audioSource.clip = sounds[random];
                audioSource.Play();
            }
        }
    }

    private void Play(GameObject go, string name)
    {
        var audioObj = go.transform.Find(name);
        if (audioObj && audioObj.GetComponent<AudioSource>())
            audioObj.GetComponent<AudioSource>().Play();
        else
            Debug.Log("Could not find sound object: " + name + " in object " + go.name);
    }

    public void OnTest(string msg)
    {
        Play(gameObject, "TestSound");
    }
}
