using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;


namespace Giant.Res {

    /// <summary>
    /// ManifestRequese  异步
    /// </summary>
    public class ManifestAsyncRequest : AssetRequest
    {
        private BundleRequest _request;
        private string _assetName;

        public override bool isDone
        {
            get
            {
                return loadState == LoadState.Loaded;
            }
        }

        internal override void Load()
        {
            _assetName = Path.GetFileName(path);
            if (AssetsMgr.UseAssetBundle)
            {
                var assetBundleName = _assetName.Replace(".asset", "").ToLower();
                _request = AssetsMgr.LoadBundleAsync(assetBundleName);
                _request.completed = RequestCompleted;
                loadState = LoadState.LoadAssetBundle;

            }
            else
            {
                loadState = LoadState.Loaded;
            }

        }

        private void RequestCompleted(AssetRequest abreq)
        {
            _request.completed = null;
            if (_request.assetBundle == null)
            {
                error = true;
                Debug.LogError("ab = nul");
            }
            else
            {
                var manifest = _request.assetBundle.LoadAsset<AssetCatlog>("Assets/" + _assetName);
                if (manifest == null)
                {
                    error = true;
                    Debug.LogError("manifest == nul");
                }
                else
                {
                    AssetsMgr.OnLoadAssetCatlog(manifest);
                    _request.Unload();
                }
            }
            loadState = LoadState.Loaded;
        }

        internal override void Unload()
        {
            if (_request != null)
            {
                _request.Release();
                _request = null;
            }
        }
    }

}