
using System.Collections.Generic;
using UnityEngine;

namespace Giant.Res {

    public class BundleRequest : AssetRequest
    {
        public readonly List<BundleRequest> dependencies = new List<BundleRequest>();

        public AssetBundle assetBundle
        {
            get { return asset as AssetBundle; }
            internal set { asset = value; }
        }

        internal override void Load()
        {
            assetBundle = AssetBundle.LoadFromFile(path);
            if (assetBundle == null) {
                error = true;
                Debug.LogError(path + "LoadFromFile error");
            }
                
        }

        internal override void Unload()
        {
            if (assetBundle == null)
                return;
            assetBundle.Unload(true);
            assetBundle = null;
            UnloadDependences();
        }

        private void UnloadDependences() {
            for (int i = 0; i < dependencies.Count; i++)
            {
                var item = dependencies[i];
                item.Release();
            }
            dependencies.Clear();
        }
    }

}