using UnityEditor;
using UnityEngine;

namespace Jovian.InspectorTools {
    public class LinkAttribute : PropertyAttribute {

        public readonly string message;
        public readonly string url;

        public LinkAttribute(string message, string url) {
            this.message = message;
            this.url = url;
        }
    }
}

#if UNITY_EDITOR
namespace Jovian.InspectorTools.Internal {

    [CustomPropertyDrawer(typeof(LinkAttribute))]
    public class LinkAttributeDrawer : PropertyDrawer {
        public override float GetPropertyHeight(SerializedProperty property, GUIContent label) {
            return base.GetPropertyHeight(property, label) + EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
        }

        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) {

            //If you don't capture these and recreate below, GUIContent label becomes replaces with LinkButtons GUIContent
            string labelText = label.text;
            string labelTooltip = label.tooltip;
            
            string message = ((LinkAttribute)attribute).message;
            string url = ((LinkAttribute)attribute).url;
            
            var linkPosition = new Rect(position.x, position.y, position.width, EditorGUIUtility.singleLineHeight);
            
            if(EditorGUI.LinkButton(linkPosition, message)) {
                Application.OpenURL(url);
            }

            var fieldPosition = new Rect(position.x, position.y + EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing, position.width, EditorGUIUtility.singleLineHeight);
            
            EditorGUI.PropertyField(fieldPosition, property, new GUIContent(labelText,labelTooltip));
        }
    }
}
#endif