using UnityEditor;
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;


	// Use this for initializatio
[CustomEditor(typeof(AudioCue))]
public class AudioCueEditor : Editor {
		
	AudioCue m_object;
	List<AudioCue.Clip> m_items;
	SerializedObject m_targetObject = null;
	SerializedProperty m_listProperty = null;
	
	void OnEnable()
	{
		
		m_object = (AudioCue)target;
		m_items = m_object.m_sounds;
		m_targetObject = new SerializedObject( target );	
		m_listProperty = m_targetObject.FindProperty("m_sounds");
		
	}
	
	override public void OnInspectorGUI() 
	{
		//EditorGUILayout.LabelField("Hi!");
		
		//EditorGUILayout.Space();
		
		
		EditorGUILayout.BeginHorizontal();
		
			if ( GUILayout.Button("Play") && m_object )
			{
				if ( Application.isEditor )
				{
					if ( SystemAudio.HasInstance() )
					{
						GameObject.DestroyImmediate( SystemAudio.Instance.gameObject );
					}
				}				
				SystemAudio.Play(m_object);
			}
		
			if ( GUILayout.Button("Stop")  )
			{
				if ( SystemAudio.HasInstance() )
				{
					GameObject.DestroyImmediate( SystemAudio.Instance.gameObject );
				}
			}
		EditorGUILayout.EndHorizontal();
		
		SerializedProperty prop = m_targetObject.GetIterator();
		if ( prop.Next(true) )
		{
			// Skip built in properites
			for ( int i = 0; i < 8 && prop.Next (false); ++i )	{}
			
			while ( prop.Next (false) )
			{				
				if ( prop.editable && prop.name != "m_sounds" && prop.name != "m_totalWeight"  )
				{
					EditorGUILayout.PropertyField( prop );
				}
			}
		}
		
		EditorGUILayout.Space();
		EditorUtils.UpdateListInspector<AudioCue.Clip>( ref m_items, null, new EditorUtils.CreateListItemGUIDelegate(BuildSpawnItemInspector),  null );				
		DropAreaGUI();
		
		EditorGUI.indentLevel = 0;
		
				
		if (GUI.changed)
		{
			
        	m_targetObject.ApplyModifiedProperties();	
			EditorUtility.SetDirty(target);					
			
			m_object.UpdateWeights();
			
			m_targetObject = new SerializedObject( target );	
			m_listProperty = m_targetObject.FindProperty("m_sounds");
		}
	}


	private void DropAreaGUI()
	{
		var evt = Event.current;
		var dropArea = GUILayoutUtility.GetRect(0f, 20f, GUILayout.ExpandWidth(true));
		GUI.Box(dropArea, "Drop sounds here to add");
		
		switch (evt.type)
		{
		case EventType.DragUpdated:
		case EventType.DragPerform:
		{
			if ( dropArea.Contains(evt.mousePosition))
			{
				
				DragAndDrop.visualMode = DragAndDropVisualMode.Copy;
				
				if (evt.type == EventType.DragPerform)
				{
					DragAndDrop.AcceptDrag();
					foreach(var draggedObject in DragAndDrop.objectReferences)
					{
						AudioClip clip = draggedObject as AudioClip;
						if ( !clip )
							continue;
						m_items.Add(new AudioCue.Clip() { m_sound = clip } );
						GUI.changed = true;
					}
				}
				
				Event.current.Use();
			}
		} break;
		}
	}

	
	// Delegate
	void BuildSpawnItemInspector( int i )
	{		
		EditorGUILayout.BeginVertical();		
			m_items[i].m_weight = EditorGUILayout.Slider( (100.0f*m_items[i].m_weight/m_object.m_totalWeight).ToString ("0.00")+"%", m_items[i].m_weight, 0,m_object.m_totalWeight);				
			m_items[i].m_sound = EditorGUILayout.ObjectField("", (Object)m_items[i].m_sound, typeof(AudioClip), false) as AudioClip; // false is "allowSceneObjects"
			
			if ( i >= m_listProperty.arraySize )
			{
				m_targetObject = new SerializedObject( target );	
				m_listProperty = m_targetObject.FindProperty("m_sounds");
			}
		
			if ( i < m_listProperty.arraySize )
			{
				SerializedProperty listItem = m_listProperty.GetArrayElementAtIndex(i);		
				EditorGUILayout.PropertyField( listItem, new GUIContent("Data"), true );	
			}
		
		EditorGUILayout.EndVertical();
		
	}
	
	
}
 
 