using UnityEditor;
using UnityEngine;
using XGame.Framework.Map;

namespace XGame.Editor.Map
{
    [CustomEditor(typeof(EntityBehaviour))]
    public class EntityBehaviourInspector : UnityEditor.Editor
    {
        private static readonly string[] EntityTypes = new string[] {"Entity", "MapUI" };

        private EntityBehaviour _behaviour;
        private int _selectedIdx = 0;

        private void OnEnable()
        {
            _behaviour = serializedObject.targetObject as EntityBehaviour;
        }
        private void OnDisable()
        {
            _behaviour = null;
        }
        public override void OnInspectorGUI()
        {
            base.OnInspectorGUI();
            serializedObject.Update();
            // 运行时不显示代码生成相关
            if (Application.isPlaying) return;
            GUILayout.Space(20);
            EditorGUILayout.BeginVertical("Box");
            DrawCodeTitle();
            DrawGenerateBtn();
            EditorGUILayout.EndVertical();
        }
        private void DrawCodeTitle()
        {
            var titleStyle = new GUIStyle("Label")
            {
                fontSize = 25,
                alignment = TextAnchor.MiddleCenter,
                fontStyle = FontStyle.BoldAndItalic
            };
            GUILayout.Box("以下为代码生成区域", titleStyle);
            GUILayout.Space(10);
            var labelContent = new GUIContent("先选择预制类型:", "MapUI只生成View和VM脚本，Entity额外生成AI控制脚本");
            _selectedIdx = EditorGUILayout.Popup(labelContent, _selectedIdx, EntityTypes);
            GUILayout.Space(10);
        }

        private void DrawGenerateBtn()
        {
            GUILayout.FlexibleSpace();
            GUILayout.BeginVertical();

            GUIStyle style = new GUIStyle("Button")
            {
                alignment = TextAnchor.MiddleCenter,
                fontStyle = FontStyle.BoldAndItalic,
                fontSize = 15,
            };

            GUI.color = Color.green;
            if (GUILayout.Button("Generator", style, GUILayout.Height(40)))
            {
                (new EntityViewCodeGen()).Gen(_behaviour, _selectedIdx);
            }
            GUI.color = Color.white;
            GUILayout.EndVertical();
        }
    }
}
