﻿using System.Collections;
using UnityEngine;

namespace JLGames.RocketDriver.Actions.Loaderx
{
    internal class ResBundleLoader : IBundleLoader
    {
        public bool UnderVersionCache => false;

        public LoaderVersion BundleVersion => null;

        internal ResBundleLoader()
        {
        }

        public IEnumerator InitVersion(LoaderDelegate.OnAssetLoaded<AssetBundleManifest> onVersionAssetBundleLoaded)
        {
            Internal.ApplyAssetCallback(onVersionAssetBundleLoaded, null, true);
            yield break;
        }

        //Bundle加载------------------------------------------------------------

        public IEnumerator LoadBundleAsync(string bundleName, LoaderDelegate.OnBundleLoaded onBundleLoaded,
            bool autoRelease = true, bool unloadObjects = false)
        {
            if (string.IsNullOrEmpty(bundleName))
            {
                throw LoaderErrors.ErrorNameEmpty;
            }

            Internal.ApplyBundleCallback(onBundleLoaded,
                new BundleRef(bundleName, autoRelease, unloadObjects), true);
            yield break;
        }

        public IEnumerator LoadBundleWithDependenciesAsync(string bundleName,
            LoaderDelegate.OnBundleLoaded onBundleLoaded, bool autoRelease = true, bool unloadObjects = false)
        {
            if (string.IsNullOrEmpty(bundleName))
            {
                throw LoaderErrors.ErrorNameEmpty;
            }

            Internal.ApplyBundleCallback(onBundleLoaded,
                new BundleRef(bundleName, autoRelease, unloadObjects), true);
            yield break;
        }

        public IEnumerator LoadMultiBundleAsync(string[] bundleNames,
            LoaderDelegate.OnMultiBundleLoaded onMultiBundleLoaded,
            bool autoRelease = true, bool unloadObjects = false)
        {
            if (null == bundleNames)
            {
                Internal.ApplyMultiBundleCallback(onMultiBundleLoaded, null, false);
            }
            else
            {
                Internal.ApplyMultiBundleCallback(onMultiBundleLoaded, new BundleRef[bundleNames.Length], true);
            }

            yield break;
        }
    }
}