// #if UNITY_EDITOR
using System.Text;
using UnityEngine;
using UnityEngine.Events;
using Object = UnityEngine.Object;

namespace ChillConnect
{
    public class AssetDataBaseKit : Singleton<AssetDataBaseKit>, ILoadAsset
    {
        private string assetRootPath = "ChillConnectAssets/";

        public T GetAsset<T>(string dfsad, string sa) where T : Object
        {

            // try
            // {
            var asPath = new StringBuilder(AssetPathFormat(dfsad));
            if (sa.EndsWith("!a.png"))
            {
                // assetFullPath = AssetPathFormat(dfsad, sa);
            }
            else
            {
                sa = sa.Split(".")[0];
            }
            var asset = Resources.Load<T>(asPath.ToString() + "/" + sa);

            if (asset == null)
            {
                Debug.LogError($"[Jarvis] [AssetDataBaseKit] 未能加载资源: {asPath}/{sa}");
                return default; // 或者返回一个备用资源，或者抛出异常
            }

            return asset;
            // }
            // catch (System.Exception e)
            // {
            //     Debug.Log($"[Jarvis] [AssetDataBaseKit] 错误堆栈：{e.StackTrace}");
            //     Debug.LogError($"[Jarvis] [AssetDataBaseKit] 加载资源时发生错误: {e.Message}");
            //     return default; // 或者返回一个备用资源，或者抛出异常
            // }
            // var assetPath = new StringBuilder(AssetPathFormat(dfsad));
            // var assetFullPath = "";

            // if (sa.EndsWith("!a.png"))
            // {
            //     assetFullPath = AssetPathFormat(dfsad, sa);
            // }
            // else
            // {
            //     sa = sa.Split(".")[0];
            //     var assets = AssetDatabase.FindAssets(sa, new[] { assetPath.ToString() });

            //     var nameFilter = sa + ".";
            //     foreach (var temp in assets)
            //     {
            //         var fullPath = AssetDatabase.GUIDToAssetPath(temp);
            //         if (fullPath.Contains(nameFilter))
            //         {
            //             assetFullPath = fullPath;
            //             break;
            //         }
            //     }
            // }

            // var asset = AssetDatabase.LoadAssetAtPath<T>(assetFullPath);
            // return asset;
        }

        public void GetAsset<T>(string dfsad, string sa, UnityAction<T> onCompleted) where T : Object
        {

            // try
            // {
            var asset = GetAsset<T>(dfsad, sa);
            onCompleted?.Invoke(asset);
            // }
            // catch (System.Exception e)
            // {
            //     Debug.Log($"[ Jarvis ] [ GetAsset ] 错误堆栈：{e.StackTrace}");

            //     Debug.LogError($"[ Jarvis ] [ GetAsset ] 异步加载资源 {sa} 时发生错误: {e.Message}");
            //     onCompleted?.Invoke(default); // 可能需要通知用户加载失败
            // }
        }

        public void RecycleAsset(string assetUrl, UnityAction onCompleted)
        {
            onCompleted?.Invoke();
        }

        private string AssetPathFormat(string assetUrl, string assetName)
        {
            var replace = assetUrl.Replace(".", "/");
            var sb = new StringBuilder(replace);
            var folderPath = sb + "/";

            var assetPath = $"{assetRootPath}{folderPath}{assetName}";
            return assetPath;
        }

        private string AssetPathFormat(string assetUrl)
        {
            var assetPath = $"{assetRootPath}{assetUrl.Replace(".", "/")}";
            return assetPath;
        }

        public static T GetAssetstatic<T>(string assetUrl, string assetName) where T : Object
        {
            var assetPath = $"{"ChillConnectAssets/"}{assetUrl.Replace(".", "/")}";
            assetName = assetName.Split(".")[0];
            var asset = Resources.Load<T>(assetPath.ToString() + "/" + assetName);
            return asset;
        }
    }
}
// #endif