#if UNITY_EDITOR

using XGame.Framework.Asyncs;
using UnityEditor;
using UnityEngine;

namespace XGame.Framework.Asset
{
    /// <summary>
    /// 使用EditorApplication.update来实现AssetData的异步加载
    /// </summary>
    internal class AssetDatabaseAsync : Async, IAssetAsync
    {
        public string Path { get; internal set; }
        public string AddressableName { get; internal set; }
        internal bool IsSprite { get; set; }
        const int WAIT_FOR_FRAME = 1;



        public object GetResult()
        {
            if (!System.IO.File.Exists(Path))
            {
                AssetsLog.Warn($"AssetDatabaseAsync GetResult Error. File is not exists. Name:{AddressableName} Path:{Path}");
                return null;
            }
            return IsSprite ?  AssetDatabase.LoadAssetAtPath(Path, typeof(Sprite)) : AssetDatabase.LoadMainAssetAtPath(Path);
        }

        public void Start()
        {
            SimulateAsyncMono.Instance.StartCoroutine(OnLoop());
        }


        System.Collections.IEnumerator OnLoop()
        {
            yield return WAIT_FOR_FRAME;

            Progress = 1f;
            Completed();
        }
    }

}
#endif

