using UnityEngine;
using UnityEditor.UI;
using UnityEditor;
using XRTool.Util;

namespace XRTool.WorldUI
{
    [InitializeOnLoad]
    [CustomEditor(typeof(XRIcon3D))]
    public class XRIcon3DEditor : XRImage3DEditor
    {
        private XRIcon3D icon3D;
        private SerializedProperty img;
        [MenuItem("GameObject/XRUI/XRUIModel/XRIcon3D", priority = 0)]
        static void Init()
        {
            var obj = Instantiate(Resources.Load<XRIcon3D>(typeof(XRIcon3D).Name));
            obj.name = (typeof(XRIcon3D).Name);
            if (obj)
            {
                var parent = Selection.activeGameObject;
                if (!parent)
                {
                    var canvas = GameObject.FindObjectsOfType(typeof(Canvas));
                    if (canvas != null)
                    {
                        for (int i = 0; i < canvas.Length; i++)
                        {
                            if ((canvas[i] as Canvas).renderMode == RenderMode.WorldSpace)
                            {
                                parent = (canvas[i] as Canvas).gameObject;
                                break;
                            }
                        }
                    }
                }
                UnityUtil.SetParent(parent ? parent.transform : null, obj.transform);
                Selection.activeGameObject = obj.gameObject;
            }
        }
        [MenuItem("GameObject/XRUI/XRUIModel/XRItem", priority = 0)]
        static void InitItem()
        {
            var obj = Instantiate(Resources.Load<GameObject>("XRItem"));
            obj.name = "XRItem";
            if (obj)
            {
                var parent = Selection.activeGameObject;
                if (!parent)
                {
                    var canvas = GameObject.FindObjectsOfType(typeof(Canvas));
                    if (canvas != null)
                    {
                        for (int i = 0; i < canvas.Length; i++)
                        {
                            if ((canvas[i] as Canvas).renderMode == RenderMode.WorldSpace)
                            {
                                parent = (canvas[i] as Canvas).gameObject;
                                break;
                            }
                        }
                    }
                }
                UnityUtil.SetParent(parent ? parent.transform : null, obj.transform);
                Selection.activeGameObject = obj.gameObject;
            }
        }

        protected override void OnEnable()
        {
            base.OnEnable();
            img = serializedObject.FindProperty("img");
            icon3D = target as XRIcon3D;
            icon3D.AutoSetSize();
        }


        public override void OnInspectorGUI()
        {
            base.OnInspectorGUI();
            EditorGUILayout.PropertyField(img, new GUIContent("Icon"));
            serializedObject.ApplyModifiedProperties();
            if (GUI.changed)
            {
                if (icon3D)
                {
                    icon3D.SetIcon();
                }
            }
        }
    }
}