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

//Original by DYLAN ENGELMAN http://jupiterlighthousestudio.com/custom-inspectors-unity/
//Altered by Brecht Lecluyse http://www.brechtos.com
 
[CustomPropertyDrawer(typeof(TagSelectorAttribute))]
public class TagSelectorPropertyDrawer : PropertyDrawer
{
	public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) {
		if (property.propertyType == SerializedPropertyType.String) {
			EditorGUI.BeginProperty(position, label, property);

			var attrib = this.attribute as TagSelectorAttribute;

			List<string> tagList = new List<string>();
			tagList.Add("<none>");
			tagList.AddRange(UnityEditorInternal.InternalEditorUtility.tags);
			int index = 0;
			for (int i = 1; i < tagList.Count; i++) {
				if (tagList[i] == property.stringValue) {
					index = i;
					break;
				}
			}

			index = EditorGUI.Popup(position, label.text, index, tagList.ToArray());
			property.stringValue = (index > 0) ? tagList[index] : "";

			EditorGUI.EndProperty();
		} else {
			EditorGUI.PropertyField(position, property, label);
		}
	}
}
