#if UNITY_EDITOR || UNITY_STANDALONE || UNITY_STANDALONE_WIN

using XGame.Framework.Define;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace XGame.Framework.Asset.Monitor
{
    public sealed partial class AssetsMonitor
    {
        private string MonitorPath => Path.Combine(PathDefine.SandboxPath, "AssetsMonitor.log");

        private Dictionary<string, AssetLoadingRecord> _assetInfoMap = new Dictionary<string, AssetLoadingRecord>();

        private void RecordAssetInternal(string addressableName, object obj)
        {
            if (!_assetInfoMap.ContainsKey(addressableName))
            {
                var info = new AssetLoadingRecord()
                {
                    addressableName = addressableName,
                    loadedTime = UnityEngine.Time.realtimeSinceStartup,
                    contents = obj.GetType().ToString(),
                };
                _assetInfoMap.Add(addressableName, info);
            }
        }
        private AssetLoadingRecord[] GetRecordsInternal()
        {
            return _assetInfoMap.Values.ToArray();
        }
        private void WriteInternal()
        {
            try
            {
                var path = MonitorPath;
                if (System.IO.File.Exists(path))
                {
                    System.IO.File.Delete(path);
                }
                var sb = new StringBuilder();
                if (_assetInfoMap != null)
                {
                    sb.AppendLine("AssetLoadingRecord Begin.");
                    foreach (var item in _assetInfoMap)
                    {
                        sb.AppendLine(item.Value.ToString());
                    }
                    sb.AppendLine("AssetLoadingRecord End.");
                }
                System.IO.File.WriteAllText(path, sb.ToString());
                UnityEngine.Debug.Log("AssetsMonitor Write End.");
            }
            catch (Exception ex)
            {
                UnityEngine.Debug.LogException(ex);
            }
        }
    }

}

#endif

