﻿using System;
using System.Collections;
using UnityEngine;
using Object = UnityEngine.Object;

namespace JLGames.RocketDriver.Actions.Loaderx
{
    internal class LoaderBase : BaseAssetLoader, IAssetLoader
    {
        protected LoaderSettings m_OriginalSettings;
        protected LoaderBaseInfo m_BaseInfo;
        protected IBundleLoader m_BundleLoader;
        protected IAssetLoader m_AssetLoader;

        public LoaderSettings OriginalSettings => m_OriginalSettings;

        public bool UnderVersionCache => m_BundleLoader.UnderVersionCache;

        public LoaderVersion BundleVersion => m_BundleLoader.BundleVersion;

        public IEnumerator InitVersion(LoaderDelegate.OnAssetLoaded<AssetBundleManifest> onVersionAssetBundleLoaded)
        {
            return m_BundleLoader.InitVersion(onVersionAssetBundleLoaded);
        }

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

        public IEnumerator LoadBundleAsync(string bundleName, LoaderDelegate.OnBundleLoaded onBundleLoaded,
            bool autoRelease = true, bool unloadObjects = false)
        {
            return m_BundleLoader.LoadBundleAsync(bundleName, onBundleLoaded, autoRelease, unloadObjects);
        }

        public IEnumerator LoadBundleWithDependenciesAsync(string bundleName,
            LoaderDelegate.OnBundleLoaded onBundleLoaded, bool autoRelease = true, bool unloadObjects = false)
        {
            return m_BundleLoader.LoadBundleWithDependenciesAsync(bundleName, onBundleLoaded, autoRelease,
                unloadObjects);
        }

        public IEnumerator LoadMultiBundleAsync(string[] bundleNames,
            LoaderDelegate.OnMultiBundleLoaded onMultiAssetLoaded,
            bool autoRelease = true, bool unloadObjects = false)
        {
            return m_BundleLoader.LoadMultiBundleAsync(bundleNames, onMultiAssetLoaded, autoRelease, unloadObjects);
        }

        //同步资源加载

        public override T LoadAssetSync<T>(string assetPath, AssetBundle bundle)
        {
            return m_AssetLoader.LoadAssetSync<T>(assetPath, bundle);
        }

        public override T LoadAssetSyncFull<T>(string fullPath, AssetBundle bundle)
        {
            return m_AssetLoader.LoadAssetSyncFull<T>(fullPath, bundle);
        }

        //--同步子资源(全部)

        public Object[] LoadSubAssetsSync(string path, AssetBundle bundle)
        {
            return m_AssetLoader.LoadSubAssetsSync(path, bundle);
        }

        public Object[] LoadSubAssetsSync(string path, Type type, AssetBundle bundle)
        {
            return m_AssetLoader.LoadSubAssetsSync(path, type, bundle);
        }

        public T[] LoadSubAssetsSync<T>(string path, AssetBundle bundle) where T : Object
        {
            return m_AssetLoader.LoadSubAssetsSync<T>(path, bundle);
        }

        //--同步子资源(名称)

        public Object LoadSubAssetSync(string path, string subName, AssetBundle ab)
        {
            return m_AssetLoader.LoadSubAssetSync(path, subName, ab);
        }

        public Object LoadSubAssetSync(string path, string subName, Type type, AssetBundle ab)
        {
            return m_AssetLoader.LoadSubAssetSync(path, subName, type, ab);
        }

        public T LoadSubAssetSync<T>(string path, string subName, AssetBundle ab) where T : Object
        {
            return m_AssetLoader.LoadSubAssetSync<T>(path, subName, ab);
        }

        //--同步子资源(多个名称)

        public Object[] LoadSubAssetsSync(string path, string[] subNames, AssetBundle bundle)
        {
            return m_AssetLoader.LoadSubAssetsSync(path, subNames, bundle);
        }

        public Object[] LoadSubAssetsSync(string path, string[] subNames, Type type, AssetBundle ab)
        {
            return m_AssetLoader.LoadSubAssetsSync(path, subNames, type, ab);
        }

        public T[] LoadSubAssetsSync<T>(string path, string[] subNames, AssetBundle bundle) where T : Object
        {
            return m_AssetLoader.LoadSubAssetsSync<T>(path, subNames, bundle);
        }

        //异步资源加载------------------------------------------------------------

        public IEnumerator LoadAssetAsync<T>(string assetPath, AssetBundle bundle,
            LoaderDelegate.OnAssetLoaded<T> onAssetLoaded) where T : Object
        {
            return m_AssetLoader.LoadAssetAsync(assetPath, bundle, onAssetLoaded);
        }

        public IEnumerator LoadMulitAssetAsync<T>(string[] assetPaths, AssetBundle bundle,
            LoaderDelegate.OnMultiAssetLoaded<T> onMultiAssetLoaded) where T : Object
        {
            return m_AssetLoader.LoadMulitAssetAsync(assetPaths, bundle, onMultiAssetLoaded);
        }

        public IEnumerator LoadSubAssetsAsync<T>(string assetPath, AssetBundle buundle,
            LoaderDelegate.OnMultiAssetLoaded<T> onAssetsLoaded) where T : Object
        {
            return m_AssetLoader.LoadSubAssetsAsync(assetPath, buundle, onAssetsLoaded);
        }

        public IEnumerator LoadSubAssetAsync<T>(string assetPath, string subName, AssetBundle bundle,
            LoaderDelegate.OnAssetLoaded<T> onAssetLoaded) where T : Object
        {
            return m_AssetLoader.LoadSubAssetAsync(assetPath, subName, bundle, onAssetLoaded);
        }
    }
}