using System.Collections.Generic;
using System.Diagnostics;

namespace XGame.Framework.Asset.Monitor
{
    public sealed partial class AssetsMonitor
    {
        private static AssetsMonitor _instance;

        private static AssetsMonitor Instance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new AssetsMonitor();
                }
                return _instance;
            }
        }
        /// <summary>
        /// 记录加载数据
        /// </summary>
        /// <param name="addressableName"></param>
        /// <param name="obj"></param>
        [Conditional(MacroDefine.UNITY_EDITOR)]
        [Conditional(MacroDefine.UNITY_STANDALONE)]
        [Conditional(MacroDefine.UNITY_STANDALONE_WIN)]
        public static void RecordAsset(string addressableName, object obj)
        {
#if UNITY_EDITOR || UNITY_STANDALONE || UNITY_STANDALONE_WIN
            Instance.RecordAssetInternal(addressableName, obj);
#endif
        }
        /// <summary>
        /// 获取所有已加载过的资源数据
        /// </summary>
        /// <returns></returns>
        public static AssetLoadingRecord[] GetRecords()
        {
#if UNITY_EDITOR || UNITY_STANDALONE || UNITY_STANDALONE_WIN
            return Instance.GetRecordsInternal();
#else
            return null;
#endif
        }

        [Conditional(MacroDefine.UNITY_EDITOR)]
        public static void Write()
        {
#if UNITY_EDITOR || UNITY_STANDALONE || UNITY_STANDALONE_WIN
            Instance.WriteInternal();
#endif
        }

        [Conditional(MacroDefine.UNITY_EDITOR)]
        public static void Dispose()
        {
#if UNITY_EDITOR || UNITY_STANDALONE || UNITY_STANDALONE_WIN
            if (_instance != null)
            {
                //Write();
                _instance._assetInfoMap.Clear();
                _instance._assetInfoMap = null;
            }
            _instance = null;
#endif
        }

        #region AssetModule
        /// <summary>
        /// 返回指定AssetModule的实例数据
        /// </summary>
        /// <param name="assetModule"></param>
        /// <returns></returns>
        public static AssetInstanceCopy[] GetAssetInstances(IAssetModule assetModule)
        {
#if UNITY_EDITOR || UNITY_STANDALONE || UNITY_STANDALONE_WIN
            if (assetModule is AssetModule impl)
            {
                var field = impl.GetType().GetField("assetMap", System.Reflection.BindingFlags.NonPublic |
                                System.Reflection.BindingFlags.Instance);
                if (field != null)
                {
                    var assetMap = field.GetValue(impl) as Dictionary<long, AssetInstance>;
                    var instances = new AssetInstanceCopy[assetMap.Count];
                    var index = 0;
                    foreach(var item in assetMap)
                    {
                        var instance = item.Value;
                        instances[index++] = new AssetInstanceCopy()
                        {
                            asset = instance.Asset,
                            reference = instance.Reference,
                            hashcode = instance.Hashcode,
                            name = instance.Name
                        };
                    }
                    return instances;
                }
            }
#endif
            return null;
        }
        #endregion
    }
}
