﻿#if UNITY_EDITOR
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

namespace Arugula.Animation
{
    [CustomEditor(typeof(AnimatedSprite))]
    public class AnimatedSpriteInspector : Editor
    {

        private void OnEnable()
        {

        }

        public override void OnInspectorGUI()
        {
            base.OnInspectorGUI();
        }
    }

    public class AnimatedSpriteDataDrawerBase : PropertyDrawer
    {
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            position = EditorGUI.PrefixLabel(position, label);
            if (GUI.Button(position, property.stringValue == "" ? "----" : property.stringValue, EditorStyles.popup))
            {
                var props = property.serializedObject.GetIterator();
                props.Next(true);
                while (props.Next(false))
                {
                    if (props.propertyType == SerializedPropertyType.ObjectReference && props.objectReferenceValue != null && props.objectReferenceValue is AnimatedSpriteData)
                    {
                        var data = (AnimatedSpriteData)props.objectReferenceValue;

                        PopulateMenu(property, data);


                    }

                }
            }
        }

        protected void SetProperty(object obj)
        {
            var arr = (object[])obj;

            var prop = (SerializedProperty)arr[0];
            var name = (string)arr[1];

            prop.stringValue = name;

            prop.serializedObject.ApplyModifiedProperties();
        }

        protected virtual void PopulateMenu(SerializedProperty property, AnimatedSpriteData data)
        {


        }
    }

    [CustomPropertyDrawer(typeof(AnimatedSpriteSequence))]
    public class AnimatedSpriteSequenceDrawer : AnimatedSpriteDataDrawerBase
    {
        protected override void PopulateMenu(SerializedProperty property, AnimatedSpriteData data)
        {
            GenericMenu menu = new GenericMenu();

            menu.AddItem(new GUIContent("None"), "" == property.stringValue, SetProperty, new object[] { property, "" });
            menu.AddSeparator("");

            foreach (var s in data.sequences)
            {
                menu.AddItem(new GUIContent(s.name), s.name == property.stringValue, SetProperty, new object[] { property, s.name });
            }
            menu.ShowAsContext();
        }
    }

    [CustomPropertyDrawer(typeof(AnimatedSpriteSkin))]
    public class AnimatedSpriteSkinDrawer : AnimatedSpriteDataDrawerBase
    {
        protected override void PopulateMenu(SerializedProperty property, AnimatedSpriteData data)
        {
            GenericMenu menu = new GenericMenu();

            menu.AddItem(new GUIContent("None"), "" == property.stringValue, SetProperty, new object[] { property, "" });
            menu.AddSeparator("");

            foreach (var s in data.skins)
            {
                menu.AddItem(new GUIContent(s.name), s.name == property.stringValue, SetProperty, new object[] { property, s.name });
            }
            menu.ShowAsContext();
        }
    }
}
#endif