﻿#if UNITY_EDITOR
using JLGames.RocketDriver.Editor.Infra;
using JLGames.RocketDriver.Actions.Utils;
using UnityEditor;
using UnityEngine;

namespace JLGames.RocketDriver.Samples.AssetsExample
{
    public static class MenuTestAsset
    {
        private const string MissingPath = "Assets/JLGames/RocketDriver/Samples/EditorAsset/Prefabs/MissingPrefab.prefab";
        private const string PrefabsFolder = "Assets/JLGames/RocketDriver/Samples/EditorAsset/Prefabs";

        [MenuItem("Tools/RocketDriver Samples/Test Find Missing", false, 140)]
        static void TestFindMissing()
        {
            var missing = AssetDatabase.LoadAssetAtPath<GameObject>(MissingPath);
            var missResult = EditorAssetReferenceUtil.FindMissingReferences<Component>(missing);
            DebugUtil.LogWarning("PrefabPath:", MissingPath, "MissingSize:", missResult.MissingSize);
            missResult.Foreach(missPaths => { DebugUtil.Log(missPaths.ToString()); });
        }

        [MenuItem("Tools/RocketDriver Samples/Test Find Missing in Folder", false, 141)]
        static void TestFindMissingFolder()
        {
            var guids = AssetDatabase.FindAssets("t:Prefab", new[] {PrefabsFolder});
            DebugUtil.Log("TestFindMissingFolder:", guids.Length);
            foreach (var guid in guids)
            {
                var path = AssetDatabase.GUIDToAssetPath(guid);
                var missing = AssetDatabase.LoadAssetAtPath<GameObject>(path);
                var missResult = EditorAssetReferenceUtil.FindMissingReferences<Component>(missing);
                DebugUtil.LogWarning("PrefabPath:", path, "MissingSize:", missResult.MissingSize);
                missResult.Foreach(missPaths => { DebugUtil.Log(missPaths.ToString()); });
            }
        }

        private const string SlicesAssetIndexPath = "Assets/JLGames/RocketDriver/Samples/EditorAsset/EditorAssetIndexSlices.asset";
        private const string AtlasAssetIndexPath = "Assets/JLGames/RocketDriver/Samples/EditorAsset/EditorAssetIndexAtlas.asset";
        private const string ReplaceReferencesUnityPath = "Assets/JLGames/RocketDriver/Samples/EditorAsset/Prefabs/ReplaceReferences_1.prefab";

        [MenuItem("Tools/RocketDriver Samples/Test ReplaceReferences", false, 142)]
        static void TestReplaceReferences()
        {
            var idxSlices = AssetDatabase.LoadAssetAtPath<EditorAssetIndex>(SlicesAssetIndexPath);
            var idxAtlas = AssetDatabase.LoadAssetAtPath<EditorAssetIndex>(AtlasAssetIndexPath);
            var prefab = AssetDatabase.LoadAssetAtPath<GameObject>(ReplaceReferencesUnityPath);
            EditorAssetReferenceUtil.ReplaceReferences(prefab, idxSlices, idxAtlas, true);
        }

        private const string MissingReferencesFixUnityPath = "Assets/JLGames/RocketDriver/Samples/EditorAsset/Prefabs/MissingReferencesFix_1.prefab";

        [MenuItem("Tools/RocketDriver Samples/Test FixMissing", false, 143)]
        static void FixMissing()
        {
            var idxSlices = AssetDatabase.LoadAssetAtPath<EditorAssetIndex>(SlicesAssetIndexPath);
            var prefab = AssetDatabase.LoadAssetAtPath<GameObject>(MissingReferencesFixUnityPath);
            EditorAssetReferenceUtil.FixReferenceMissing(prefab, idxSlices, idxSlices);
        }
    }
}
#endif