﻿using System;
using UnityEngine;

namespace JLGames.RocketDriver.Actions.Loaderx
{
    public class LoaderVersion
    {
        private AssetBundleManifest m_VersionManifest;

        private string[] m_AbList;

        public LoaderVersion()
        {
        }

        public LoaderVersion(AssetBundleManifest manifest)
        {
            InitVersion(manifest);
        }

        /// <summary>
        /// Init version
        /// 初始化版本
        /// </summary>
        /// <param name="manifes"></param>
        public void InitVersion(AssetBundleManifest manifes)
        {
            m_VersionManifest = manifes;
            InnerHandleManifest();
            InnerCheckAssetBundleNames();
        }

        /// <summary>
        /// Check exist of bundle
        /// AssetBundle存在性检查
        /// </summary>
        /// <param name="bundleName"></param>
        /// <returns></returns>
        public bool CheckExist(string bundleName)
        {
            foreach (var ab in m_AbList)
            {
                if (ab == bundleName)
                {
                    return true;
                }
            }

            return false;
        }

        /// <summary>
        /// Get cache info.
        /// 提取缓存信息
        /// </summary>
        /// <param name="bundleName"></param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public CachedAssetBundle GetCachedInfo(string bundleName)
        {
            if (!CheckExist(bundleName))
            {
                var info = $"AssetBundle undefinded:{bundleName}.";
//                DebugUtil.LogWarning(info);
                throw new Exception(info);
            }

            return new CachedAssetBundle
            {
                // Internal and current cache directory will be merged
                // Then use the absolute path to create the file
                // For example, after adding "ttt" to "C:/abc", the path is synthesized as "C:/abcttt", which makes it impossible to cache
                // 内部和会当前缓存目录进行进行合并
                // 然后使用绝对路私进行创建文件
                // 如"C：/abc"加上"ttt"后，路径出合成为"C：/abcttt"，造成无法缓存
                name = bundleName,
                hash = m_VersionManifest.GetAssetBundleHash(bundleName)
            };
        }

        /// <summary>
        /// Find all dependencies
        /// 查找全部依赖
        /// </summary>
        /// <param name="bundleName">ab名称</param>
        /// <returns>如果名称不在版本管理中，返回空数组</returns>
        public string[] GetDependencies(string bundleName)
        {
            return m_VersionManifest.GetAllDependencies(bundleName);
        }

        /// <summary>
        /// Get cache info that AssetBundle depends on
        /// 提取AssetBundle所依赖的缓存信息
        /// </summary>
        /// <param name="bundleName"></param>
        /// <returns></returns>
        public CachedAssetBundle[] GetDependenciesCachedInfo(string bundleName)
        {
            var tmp = m_VersionManifest.GetAllDependencies(bundleName);
            if (0 == tmp.Length)
            {
                return null;
            }

            var rs = new CachedAssetBundle[tmp.Length];
            for (var i = 0; i < tmp.Length; i++)
            {
                rs[i] = GetCachedInfo(tmp[i]);
            }

            return rs;
        }

        private void InnerHandleManifest()
        {
            m_AbList = m_VersionManifest.GetAllAssetBundles();
        }

        /// <summary>
        /// Should check before publish
        /// </summary>
        private void InnerCheckAssetBundleNames()
        {
//            var fbList = new List<string>();
//            foreach (var n in m_AbList)
//            {
//                if (LoaderForbit.CheckForbit(n))
//                {
//                    fbList.Add(n);
//                }
//            }
//
//            if (fbList.Count > 0)
//            {
//                throw new Exception("AssetBundle name forbit: " + string.Join(",", fbList));
//            }
        }
    }
}