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

namespace Giant.Res {

    public class BundleAsyncRequest : BundleRequest
    {
        private AssetBundleCreateRequest _request;

        public override bool isDone
        {
            get
            {
                if (loadState == LoadState.Init)
                    return false;

                if (loadState == LoadState.Loaded)
                {
                    return true;
                }

                if (loadState == LoadState.LoadAssetBundle && _request.isDone)
                {
                    asset = _request.assetBundle;
                    if (_request.assetBundle == null)
                    {
                        error = true;
                        Debug.LogError("load assetbunle error " + path);
                    }
                    loadState = LoadState.Loaded;
                }
                return _request == null || _request.isDone;
            }
        }

        internal override void Load()
        {
            _request = AssetBundle.LoadFromFileAsync(path);
            if (_request == null)
            {
                error = true;
                Debug.LogError("loadfromfileasync error " + path);
                return;
            }

            loadState = LoadState.LoadAssetBundle;
        }

        internal override void Unload()
        {
            _request = null;
            loadState = LoadState.Unload;
            base.Unload();
        }
    }

}