using System;
using System.Reflection;
using TMPro.EditorUtilities;
using UnityEditor;
using UnityEngine;
using UnityEngine.UI;


internal class UXUIEditor : Editor
{
    static object InvokeMethod(Type type, string methodName, object[] parameters = null)
    {
        if (parameters == null)
        {
            return type.GetMethod(methodName, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static).Invoke(null, null);
        }

        Type[] types = new Type[parameters.Length];
        for (int i = 0; i < parameters.Length; i++)
        {
            types[i] = parameters[i].GetType();
        }

        return type.GetMethod(methodName, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static, null, types, null).Invoke(null, parameters);
    }

    [MenuItem("GameObject/UI/UXImage")]
    public static void CreateUXImage(MenuCommand menuCommand)
    {
        Type MenuOptionsType = typeof(UnityEditor.UI.ImageEditor).Assembly.GetType("UnityEditor.UI.MenuOptions");
        InvokeMethod(MenuOptionsType, "AddImage", new object[] { menuCommand });
        GameObject obj = Selection.activeGameObject;
        obj.name = "UXImage";
        DestroyImmediate(obj.GetComponent<Image>());
        var image = obj.AddComponent<UXImage>();
        image.material = AssetDatabase.LoadAssetAtPath<Material>(UXGUIConfig.UIDefaultMatPath);
    }


    [MenuItem("GameObject/UI/UXTextMeshPro")]
    private static void CreateUXTextMeshPro(MenuCommand menuCommand)
    {
        Type MenuOptionsType = typeof(UnityEditor.UI.ImageEditor).Assembly.GetType("UnityEditor.UI.MenuOptions");
        InvokeMethod(MenuOptionsType, "AddText", new object[] { menuCommand });
        GameObject obj = Selection.activeGameObject;
        obj.name = "UXTextMeshPro";
        DestroyImmediate(obj.GetComponent<Text>());
        obj.AddComponent<UXTextMeshPro>();
    }

    [MenuItem("GameObject/UI/UXButton")]
    public static void CreateUXButton(MenuCommand menuCommand)
    {
        Type MenuOptionsType = typeof(TMPro.EditorUtilities.TMPro_CreateObjectMenu).Assembly.GetType("TMPro.EditorUtilities.TMPro_CreateObjectMenu");
        InvokeMethod(MenuOptionsType, "AddButton", new object[] { menuCommand });
        GameObject obj = Selection.activeGameObject;
        obj.name = "UXButton";
        DestroyImmediate(obj.GetComponent<Button>());
        obj.AddComponent<UXButton>();
    }

    [MenuItem("GameObject/UI/UXScrollView")]
    private static void CreateUxRecyclerView()
    {
        GameObject selectionObject = Selection.activeGameObject;
        if (selectionObject == null) return;
        const string prefabPath = "Packages/com.alicizax.unity.ui.extension/Editor/RecyclerView/Res/ScrollView.prefab";
        GameObject prefab = AssetDatabase.LoadAssetAtPath<GameObject>(prefabPath);
        GameObject instance = (GameObject)PrefabUtility.InstantiatePrefab(prefab, selectionObject.transform);
        PrefabUtility.UnpackPrefabInstance(instance, PrefabUnpackMode.Completely, InteractionMode.UserAction);
        Selection.activeGameObject = instance;
    }

    [MenuItem("GameObject/UI/UXTemplateWindow")]
    private static void CreateTemplateWindow()
    {
        GameObject selectionObject = Selection.activeGameObject;
        if (selectionObject == null) return;
        const string prefabPath = "Packages/com.alicizax.unity.ui.extension/Editor/Res/Template/UITemplateWindow.prefab";
        GameObject prefab = AssetDatabase.LoadAssetAtPath<GameObject>(prefabPath);
        GameObject instance = (GameObject)PrefabUtility.InstantiatePrefab(prefab, selectionObject.transform);
        PrefabUtility.UnpackPrefabInstance(instance, PrefabUnpackMode.Completely, InteractionMode.UserAction);
        Selection.activeGameObject = instance;
    }
}
