﻿using System.Linq;
using UnityEditor;
using UnityEngine;

namespace Custom_Properties.Editor
{
    [CustomPropertyDrawer(typeof(RangeDropdownAttribute))]
    public class RangedDropdownDrawer : PropertyDrawer
    {
        // Draw the property inside the given rect
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            EditorGUI.BeginProperty(position, label, property);
        
            var range = attribute as RangeDropdownAttribute;
            // First get the attribute since it contains the range for the slider

            if (property.propertyType == SerializedPropertyType.Integer)
            {
                var values = Enumerable.Range(range.min, range.max - range.min + 1).ToArray();
                var names = values.Select(x => x.ToString()).ToArray();

                property.intValue = EditorGUI.IntPopup(position, label.text, property.intValue, names, values);
            }
            else
            {
                EditorGUI.LabelField(position, label.text, "Use RangeDropdown with int.");
            }

        
            EditorGUI.EndProperty();
        }
    }
}