#if UNITY_EDITOR
namespace NiAkaDev.EzMirror.Editor
{
    using UnityEditor;
    using UnityEngine;

    internal static class EzMirrorMenuItems
    {
        [MenuItem("Tools/NiAka/EzMirror/Validate Distribution Package")]
        private static void ValidateDistributionPackage()
        {
            LogReport(EzMirrorDistributionValidator.ValidateDistributionPackage());
        }

        [MenuItem("Tools/NiAka/EzMirror/Validate Current Scene")]
        private static void ValidateCurrentScene()
        {
            LogReport(EzMirrorDistributionValidator.ValidateCurrentScene());
        }

        [MenuItem("Tools/NiAka/EzMirror/Repair Selected EzMirror")]
        private static void RepairSelectedEzMirror()
        {
            EzMirror mirror = GetSelectedEzMirror();
            if (mirror == null)
            {
                EditorUtility.DisplayDialog("EzMirror", "EzMirrorを選択してください。", "OK");
                return;
            }
            bool changed = EzMirrorRepairUtility.RepairSafeIssues(mirror);
            AssetDatabase.SaveAssets();
            Debug.Log(changed ? "[EzMirror] 安全修復を実行しました。" : "[EzMirror] 安全修復の対象はありませんでした。");
        }

        [MenuItem("Tools/NiAka/EzMirror/Generate Persist Key For Selected")]
        private static void GeneratePersistKeyForSelected()
        {
            EzMirrorRepairUtility.GeneratePersistKeyForSelected();
        }

        [MenuItem("GameObject/NiAka/EzMirror/Create EzMirror", false, 10)]
        private static void CreateEzMirror(MenuCommand command)
        {
            Transform parent = command.context is GameObject contextObject ? contextObject.transform : null;
            CreateEzMirrorFromPrefab(parent);
        }

        internal static GameObject CreateEzMirrorFromPrefabForTest(Transform parent)
        {
            return CreateEzMirrorFromPrefab(parent);
        }

        private static GameObject CreateEzMirrorFromPrefab(Transform parent)
        {
            GameObject prefab = EzMirrorAssetResolver.LoadDefaultPrefab();
            if (prefab == null)
                return null;
            GameObject instance = PrefabUtility.InstantiatePrefab(prefab, parent) as GameObject;
            if (instance == null)
                instance = Object.Instantiate(prefab, parent);
            Undo.RegisterCreatedObjectUndo(instance, "Create EzMirror");
            instance.name = prefab.name;
            Selection.activeGameObject = instance;
            EditorGUIUtility.PingObject(instance);
            return instance;
        }

        private static EzMirror GetSelectedEzMirror()
        {
            return Selection.activeGameObject == null
                ? null
                : Selection.activeGameObject.GetComponentInParent<EzMirror>();
        }

        private static void LogReport(EzMirrorValidationReport report)
        {
            string message = report.ToLogString();
            if (report.Success && report.Warnings.Length == 0)
                Debug.Log(message);
            else if (report.Success)
                Debug.LogWarning(message);
            else
                Debug.LogError(message);
        }
    }
}
#endif
