using UnityEngine;
using UnityEditor;
using System.Collections;
using System.Collections.Generic;
using DarkTonic.MasterAudio;

[CanEditMultipleObjects]
[CustomEditor(typeof(tk2dUISoundItem))]
public class tk2dUISoundItemEditor : tk2dUIBaseItemControlEditor
{
	private List<string> groupNames = null;

	public override void OnInspectorGUI()
    {
        base.OnInspectorGUI();
        tk2dUISoundItem sounds = (tk2dUISoundItem)target;

		var ma = MasterAudio.Instance;

		var maInScene = ma != null;		
		if (maInScene) {
			groupNames = ma.GroupNames;
		}

		EditorGUILayout.Separator();

		var isDirty = false;

		if (maInScene) {
			var existingIndex = groupNames.IndexOf(sounds.downButtonSound);

			int? groupIndex = null;

			if (existingIndex >= 1) {
				groupIndex = EditorGUILayout.Popup("Down Sound", existingIndex, groupNames.ToArray());
			} else if (existingIndex == -1 && sounds.downButtonSound == MasterAudio.NoGroupName) {
				groupIndex = EditorGUILayout.Popup("Down Sound", existingIndex, groupNames.ToArray());
			} else { // non-match
				DTGUIHelper.ShowColorWarning("Sound Type found no match. Type in or choose one.");

				var newMouseDown = EditorGUILayout.TextField("Down Sound", sounds.downButtonSound);
				if (newMouseDown != sounds.downButtonSound) {
					AudioUndoHelper.RecordObjectPropertyForUndo(ref isDirty, sounds, "change Down Sound");
					sounds.downButtonSound = newMouseDown;
				}
				var newIndex = EditorGUILayout.Popup("All Sound Types", -1, groupNames.ToArray());
				if (newIndex >= 0) {
					groupIndex = newIndex;
				}
			}
			
			if (groupIndex.HasValue) {
				if (existingIndex != groupIndex.Value) {
                    AudioUndoHelper.RecordObjectPropertyForUndo(ref isDirty, sounds, "change Down Sound");
				}
				
				if (groupIndex.Value == -1) {
                    sounds.downButtonSound = MasterAudio.NoGroupName;
				} else {
					sounds.downButtonSound = groupNames[groupIndex.Value];
				}
			}
		} else {
			var newDown = EditorGUILayout.TextField("Down Sound", sounds.downButtonSound);
			if (newDown != sounds.downButtonSound) {
                AudioUndoHelper.RecordObjectPropertyForUndo(ref isDirty, sounds, "change Down Sound");
				sounds.downButtonSound = newDown;
			}
		}

		EditorGUILayout.Separator();

		if (maInScene) {
			var existingIndex = groupNames.IndexOf(sounds.upButtonSound);
			
			int? groupIndex = null;
			
			if (existingIndex >= 1) {
				groupIndex = EditorGUILayout.Popup("Up Sound", existingIndex, groupNames.ToArray());
            } else if (existingIndex == -1 && sounds.upButtonSound == MasterAudio.NoGroupName) {
				groupIndex = EditorGUILayout.Popup("Up Sound", existingIndex, groupNames.ToArray());
			} else { // non-match
				DTGUIHelper.ShowColorWarning("Sound Type found no match. Type in or choose one.");
				
				var newUp = EditorGUILayout.TextField("Up Sound", sounds.upButtonSound);
				if (newUp != sounds.upButtonSound) {
                    AudioUndoHelper.RecordObjectPropertyForUndo(ref isDirty, sounds, "change Up Sound");
					sounds.upButtonSound = newUp;
				}
				var newIndex = EditorGUILayout.Popup("All Sound Types", -1, groupNames.ToArray());
				if (newIndex >= 0) {
					groupIndex = newIndex;
				}
			}
			
			if (groupIndex.HasValue) {
				if (existingIndex != groupIndex.Value) {
                    AudioUndoHelper.RecordObjectPropertyForUndo(ref isDirty, sounds, "change Up Sound");
				}
				
				if (groupIndex.Value == -1) {
                    sounds.upButtonSound = MasterAudio.NoGroupName;
				} else {
					sounds.upButtonSound = groupNames[groupIndex.Value];
				}
			}
		} else {
			var newUp = EditorGUILayout.TextField("Up Sound", sounds.upButtonSound);
			if (newUp != sounds.upButtonSound) {
                AudioUndoHelper.RecordObjectPropertyForUndo(ref isDirty, sounds, "change Up Sound");
				sounds.upButtonSound = newUp;
			}
		}

		EditorGUILayout.Separator();

		if (maInScene) {
			var existingIndex = groupNames.IndexOf(sounds.clickButtonSound);
			
			int? groupIndex = null;
			
			if (existingIndex >= 1) {
				groupIndex = EditorGUILayout.Popup("Click Sound", existingIndex, groupNames.ToArray());
            } else if (existingIndex == -1 && sounds.clickButtonSound == MasterAudio.NoGroupName) {
				groupIndex = EditorGUILayout.Popup("Click Sound", existingIndex, groupNames.ToArray());
			} else { // non-match
				DTGUIHelper.ShowColorWarning("Sound Type found no match. Type in or choose one.");
				
				var newClick = EditorGUILayout.TextField("Click Sound", sounds.clickButtonSound);
				if (newClick != sounds.clickButtonSound) {
                    AudioUndoHelper.RecordObjectPropertyForUndo(ref isDirty, sounds, "change Click Sound");
					sounds.clickButtonSound = newClick;
				}
				var newIndex = EditorGUILayout.Popup("All Sound Types", -1, groupNames.ToArray());
				if (newIndex >= 0) {
					groupIndex = newIndex;
				}
			}
			
			if (groupIndex.HasValue) {
				if (existingIndex != groupIndex.Value) {
                    AudioUndoHelper.RecordObjectPropertyForUndo(ref isDirty, sounds, "change Click Sound");
				}
				
				if (groupIndex.Value == -1) {
                    sounds.clickButtonSound = MasterAudio.NoGroupName;
				} else {
					sounds.clickButtonSound = groupNames[groupIndex.Value];
				}
			}
		} else {
			var newClick = EditorGUILayout.TextField("Click Sound", sounds.clickButtonSound);
			if (newClick != sounds.clickButtonSound) {
                AudioUndoHelper.RecordObjectPropertyForUndo(ref isDirty, sounds, "change Click Sound");
				sounds.clickButtonSound = newClick;
			}
		}

		EditorGUILayout.Separator();
		
		if (maInScene) {
			var existingIndex = groupNames.IndexOf(sounds.releaseButtonSound);
			
			int? groupIndex = null;
			
			if (existingIndex >= 1) {
				groupIndex = EditorGUILayout.Popup("Release Sound", existingIndex, groupNames.ToArray());
            } else if (existingIndex == -1 && sounds.releaseButtonSound == MasterAudio.NoGroupName) {
				groupIndex = EditorGUILayout.Popup("Release Sound", existingIndex, groupNames.ToArray());
			} else { // non-match
				DTGUIHelper.ShowColorWarning("Sound Type found no match. Type in or choose one.");
				
				var newClick = EditorGUILayout.TextField("Release Sound", sounds.releaseButtonSound);
				if (newClick != sounds.releaseButtonSound) {
                    AudioUndoHelper.RecordObjectPropertyForUndo(ref isDirty, sounds, "change Release Sound");
					sounds.releaseButtonSound = newClick;
				}
				var newIndex = EditorGUILayout.Popup("All Sound Types", -1, groupNames.ToArray());
				if (newIndex >= 0) {
					groupIndex = newIndex;
				}
			}
			
			if (groupIndex.HasValue) {
				if (existingIndex != groupIndex.Value) {
                    AudioUndoHelper.RecordObjectPropertyForUndo(ref isDirty, sounds, "change Release Sound");
				}
				
				if (groupIndex.Value == -1) {
                    sounds.releaseButtonSound = MasterAudio.NoGroupName;
				} else {
					sounds.releaseButtonSound = groupNames[groupIndex.Value];
				}
			}
		} else {
			var newClick = EditorGUILayout.TextField("Release Sound", sounds.releaseButtonSound);
			if (newClick != sounds.releaseButtonSound) {
                AudioUndoHelper.RecordObjectPropertyForUndo(ref isDirty, sounds, "change Release Sound");
				sounds.releaseButtonSound = newClick;
			}
		}
		
		if (GUI.changed || isDirty)
        {
			EditorUtility.SetDirty(sounds);
        }
    }

}
