using UnityEditor;
using UnityEngine;

[CustomPropertyDrawer(typeof(ResourceCount))]
public sealed class ResourceCountDrawer : PropertyDrawer
{
    public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    {
        // Using BeginProperty / EndProperty on the parent property means that
        // prefab override logic works on the entire property.
        EditorGUI.BeginProperty(position, label, property);

        // Draw label
        position = EditorGUI.PrefixLabel(position, GUIUtility.GetControlID(FocusType.Passive), label);

        // Don't make child fields be indented
        var indent = EditorGUI.indentLevel;
        EditorGUI.indentLevel = 0;

        // Calculate rects
        var idRect = new Rect(position.x, position.y, position.width - 50, position.height);
        var countRect = new Rect(position.x + position.width - 50, position.y, 50, position.height);

        // Draw fields - pass GUIContent.none to each so they are drawn without labels
        EditorGUI.PropertyField(idRect, property.FindPropertyRelative(nameof(ResourceCount.Id)), GUIContent.none);
        EditorGUI.PropertyField(countRect, property.FindPropertyRelative(nameof(ResourceCount.Count)), GUIContent.none);

        // Set indent back to what it was
        EditorGUI.indentLevel = indent;

        EditorGUI.EndProperty();
    }
}