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

public class AudioCue : MonoBehaviour 
{
	public enum eShake 
	{
		Small,
		Medium,
		Large
	}

	public enum eAudioType
	{
		Sound = 1<<0,
		Music = 1<<1,
		Dialog = 1<<2,
		Menu = 1<<3,
		Contact = 1<<4,
		Swipe = 1<<5,
		Placeholder1 = 1<<6,
		Placeholder2 = 1<<7,
		Placeholder3 = 1<<8,
		Placeholder4 = 1<<9,
		Placeholder5 = 1<<10,
	};
	
	[System.Serializable]
	public class Clip
	{
		[HideInInspector]public AudioClip m_sound = null;
		[HideInInspector]public float m_weight = 100;
		public bool m_loop = false;
		public MinMaxRange m_volume = new MinMaxRange(1.0f);
		public MinMaxRange m_pitch = new MinMaxRange(1.0f);		
		public float m_shakeTime = 0;
		public eShake m_shakeType = eShake.Medium;
		
	}
	
	[BitMask(typeof(eAudioType))]
	public int m_type = (int)eAudioType.Sound;
	public MinMaxRange m_volume =new MinMaxRange(1.0f);
	public int m_priority = 128;
	public float m_chance = 1;
	public AudioCue m_alsoPlay = null;		
	public MinMaxRange m_startDelay = new MinMaxRange(0.0f);
	public List<Clip> m_sounds = new List<Clip>(1);
	public float m_totalWeight = 0;	
	
	
	public void Play()
	{
		SystemAudio.Play(this);
	}
	public void Play( Transform emmitter )
	{
		SystemAudio.Play(this, emmitter);
	}
	
	public Clip GetClip()
	{
		float chance = Random.Range(0,m_totalWeight);
		float itemChance = 0;
		foreach ( Clip item in m_sounds )
		{
			itemChance += item.m_weight;
			if ( chance < itemChance )
			{
				return item;
			}			
		}		
		return null;
	}	
	
	public void UpdateWeights()
	{
		m_totalWeight = 0;
		foreach ( Clip item in m_sounds )
		{
			m_totalWeight += item.m_weight;
		}		
		
		if ( m_totalWeight <= 0 )
		{
			m_totalWeight = 1;
		}	
	}
	
	/*
	// Use this for initialization
	void Start () 
	{
	
	}
	
	// Update is called once per frame
	void Update () 
	{
	
	}
	*/
}
