﻿#if UNITY_EDITOR

using System;
using UnityEditor;
using UnityEngine;
using System.Reflection;
using Arugula;

[CustomPropertyDrawer(typeof(EnumFlagAttribute))]
public class EnumFlagDrawer : PropertyDrawer
{
    public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    {
        EnumFlagAttribute flagSettings = (EnumFlagAttribute)attribute;

        Enum targetEnum = null;

        if (property.propertyPath != property.name)
        {
            Type type = property.serializedObject.targetObject.GetType();
            FieldInfo parentField = type.GetField(property.propertyPath.Split('.')[0]);
            var obj = (object)parentField.GetValue(property.serializedObject.targetObject);

            targetEnum = (Enum)fieldInfo.GetValue(obj);
        }
        else
        {
            targetEnum = (Enum)fieldInfo.GetValue(property.serializedObject.targetObject);
        }


        string propName = flagSettings.name;
        if (string.IsNullOrEmpty(propName))
            propName = ObjectNames.NicifyVariableName(property.name);

        EditorGUI.BeginProperty(position, label, property);

        Enum enumNew = EditorGUI.EnumFlagsField(position, propName, targetEnum);
        property.intValue = (int)Convert.ChangeType(enumNew, targetEnum.GetType());
        EditorGUI.EndProperty();
    }
}

[CustomPropertyDrawer(typeof(MinMaxAttribute))]
public class MinMaxDrawer : PropertyDrawer
{
    const float numericWidth = 60;
    public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    {
        if (property.propertyType != SerializedPropertyType.Vector2)
            EditorGUI.LabelField(position, label, "Error: must be Vector2");

        //float space = 2 * EditorGUIUtility.pixelsPerPoint;
        float space = 2;
        Vector2 val = property.vector2Value;
        float min = val.x;
        float max = val.y;

        EditorGUI.BeginProperty(position, label, property);
        MinMaxAttribute settings = (MinMaxAttribute)attribute;

        EditorGUI.PrefixLabel(position, label);

        Rect sliderPosition = position;
        sliderPosition.x += EditorGUIUtility.labelWidth + numericWidth + space;
        sliderPosition.width -= EditorGUIUtility.labelWidth + (numericWidth + space) * 2;

        EditorGUI.BeginChangeCheck();
        EditorGUI.MinMaxSlider(sliderPosition, ref min, ref max, settings.min, settings.max);
        if (EditorGUI.EndChangeCheck())
        {
            val.x = min.Snap(settings.snapping);
            val.y = max.Snap(settings.snapping);
            property.vector2Value = val;
            property.serializedObject.ApplyModifiedProperties();
        }

        Rect minPosition = position;
        minPosition.x += EditorGUIUtility.labelWidth;
        minPosition.width = numericWidth;

        EditorGUI.BeginChangeCheck();
        min = EditorGUI.FloatField(minPosition, min).Snap(settings.snapping);
        if (EditorGUI.EndChangeCheck())
        {
            val.x = Mathf.Max(min, settings.min);
            property.vector2Value = val;
            property.serializedObject.ApplyModifiedProperties();
        }

        Rect maxPosition = position;
        maxPosition.x += maxPosition.width - numericWidth;
        maxPosition.width = numericWidth;

        EditorGUI.BeginChangeCheck();
        max = EditorGUI.FloatField(maxPosition, max).Snap(settings.snapping);
        if (EditorGUI.EndChangeCheck())
        {
            val.y = Mathf.Min(max, settings.max);
            property.vector2Value = val;
            property.serializedObject.ApplyModifiedProperties();
        }
        EditorGUI.EndProperty();
    }

    public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
    {
        return EditorGUI.GetPropertyHeight(property);
        //return EditorGUIUtility.singleLineHeight;
    }
}
#endif