using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.Networking;
namespace YooAsset
{
    /// 
    /// 1. 保证每一时刻资源文件只存在一个下载器
    /// 2. 保证下载器下载完成后立刻验证并缓存
    /// 3. 保证资源文件不会被重复下载
    /// 
    internal class DownloadManager
    {
        private readonly Dictionary _downloaders = new Dictionary(1000);
        private readonly List _removeList = new List(1000);
        private uint _breakpointResumeFileSize;
        /// 
        /// 所属包裹
        /// 
        public readonly string PackageName;
        public DownloadManager(string packageName)
        {
            PackageName = packageName;
        }
        /// 
        /// 初始化
        /// 
        public void Initialize(uint breakpointResumeFileSize)
        {
            _breakpointResumeFileSize = breakpointResumeFileSize;
        }
        /// 
        /// 更新下载器
        /// 
        public void Update()
        {
            // 更新下载器
            _removeList.Clear();
            foreach (var valuePair in _downloaders)
            {
                var downloader = valuePair.Value;
                downloader.Update();
                if (downloader.IsDone())
                {
                    _removeList.Add(valuePair.Key);
                }
            }
            // 移除下载器
            foreach (var key in _removeList)
            {
                _downloaders.Remove(key);
            }
        }
        /// 
        /// 销毁所有下载器
        /// 
        public void DestroyAll()
        {
            foreach (var valuePair in _downloaders)
            {
                var downloader = valuePair.Value;
                downloader.Abort();
            }
            _downloaders.Clear();
            _removeList.Clear();
        }
        /// 
        /// 创建下载器
        /// 注意:只有第一次请求的参数才有效
        /// 
        public DownloaderBase CreateDownload(BundleInfo bundleInfo, int failedTryAgain, int timeout = 60)
        {
            // 查询存在的下载器
            if (_downloaders.TryGetValue(bundleInfo.CachedDataFilePath, out var downloader))
            {
                downloader.Reference();
                return downloader;
            }
            // 如果资源已经缓存
            if (bundleInfo.IsCached())
            {
                var completedDownloader = new CompletedDownloader(bundleInfo);
                return completedDownloader;
            }
            // 创建新的下载器	
            DownloaderBase newDownloader = null;
            YooLogger.Log($"Beginning to download bundle : {bundleInfo.Bundle.BundleName} URL : {bundleInfo.RemoteMainURL}");
#if UNITY_WEBGL
            if (bundleInfo.Bundle.Buildpipeline == EDefaultBuildPipeline.RawFileBuildPipeline.ToString())
            {
                FileUtility.CreateFileDirectory(bundleInfo.CachedDataFilePath);
                System.Type requesterType = typeof(FileGeneralRequest);
                newDownloader = new FileDownloader(bundleInfo, requesterType, failedTryAgain, timeout);
            }
            else
            {
                System.Type requesterType = typeof(AssetBundleWebRequest);
                newDownloader = new WebDownloader(bundleInfo, requesterType, failedTryAgain, timeout);
            }
#else
            FileUtility.CreateFileDirectory(bundleInfo.CachedDataFilePath);
            bool resumeDownload = bundleInfo.Bundle.FileSize >= _breakpointResumeFileSize;
            if (resumeDownload)
            {
                System.Type requesterType = typeof(FileResumeRequest);
                newDownloader = new FileDownloader(bundleInfo, requesterType, failedTryAgain, timeout);
            }
            else
            {
                System.Type requesterType = typeof(FileGeneralRequest);
                newDownloader = new FileDownloader(bundleInfo, requesterType, failedTryAgain, timeout);
            }
#endif
            // 返回新创建的下载器
            _downloaders.Add(bundleInfo.CachedDataFilePath, newDownloader);
            newDownloader.Reference();
            return newDownloader;
        }
        /// 
        /// 停止不再使用的下载器
        /// 
        public void AbortUnusedDownloader()
        {
            foreach (var valuePair in _downloaders)
            {
                var downloader = valuePair.Value;
                if (downloader.RefCount <= 0)
                {
                    downloader.Abort();
                }
            }
        }
    }
}