using UnityEditor;
using UnityEngine;

namespace Jovian.InspectorTools
{
	public class BitmaskAttribute : PropertyAttribute {
        public string[] groupNames;
        public BitmaskAttribute(params string[] groupNames) {
            this.groupNames = groupNames;
        }
    }
}

#if UNITY_EDITOR
namespace Jovian.InspectorTools.Internal
{

    [CustomPropertyDrawer(typeof(BitmaskAttribute))]
	public class BitmaskAttributeDrawer : PropertyDrawer {
        
		public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
		{
            if(property.propertyType != SerializedPropertyType.Integer) {
                GUI.Label(position, "Error, SerializedProperty must be 'Integer' type");
                return;
            }

            if(attribute is not BitmaskAttribute bitmaskAttribute) {
                return;
            }
            
            string[] groups = bitmaskAttribute.groupNames;
            if(groups.Length == 0 || groups.Length > 8) {
                GUI.Label(position, "Error, bitmask group count not valid");
                return;
            }

            EditorGUI.BeginProperty(position, label, property);
            property.intValue = EditorGUI.MaskField(position, label, property.intValue, groups);
            int indent = EditorGUI.indentLevel;
            EditorGUI.indentLevel = 0;
            EditorGUI.indentLevel = indent;
            EditorGUI.EndProperty();
        }
	}
}
#endif
