using System;
using System.Collections;
using System.Collections.Generic;
using ThirdParty.DownloadSystem;
using UnityEngine;
using UnityEngine.Networking;

public class FileDownloadSystem
{
    public static FileDownloadSystem Instance
    {
        get
        {
            if (_instance == null)
            {
                _instance = new FileDownloadSystem();
            }

            return _instance;
        }
    }

    private static FileDownloadSystem _instance;
    public List<HttpDownloadBasic> HttpDownloadBasics = new List<HttpDownloadBasic>();
    private int _maxDownloadCount = 18;
    private List<UnityWebHttpDownload> _awitDow = new List<UnityWebHttpDownload>();

    private List<DownLoadHanderGroup> _handerGroups = new List<DownLoadHanderGroup>();

    public List<ICheckUpdate> CheckUpdateList = new List<ICheckUpdate>();

    public DownloadHander DownloadFile(DownloadFileData downloadFileData)
    {
        UnityWebHttpDownload httpDownload = new UnityWebHttpDownload();

        DownloadHander downloadHander = new DownloadHander();
        httpDownload.Init(downloadFileData, downloadHander);
        downloadHander.HttpDownloadBasic = httpDownload;
        if (HttpDownloadBasics.Count < _maxDownloadCount)
        {
            StartDownload(httpDownload);
        }
        else
        {
            _awitDow.Add(httpDownload);
        }

        return downloadHander;
    }

    public void AddHanderGroup(DownLoadHanderGroup downLoadHanderGroup)
    {
        _handerGroups.Add(downLoadHanderGroup);
    }

    public void RemoveHanderGroup(DownLoadHanderGroup downLoadHanderGroup)
    {
        _handerGroups.Remove(downLoadHanderGroup);
    }

    private void StartDownload(UnityWebHttpDownload httpDownload)
    {
        httpDownload.StartDown();
        HttpDownloadBasics.Add(httpDownload);
    }

    public void Update()
    {
        try
        {
            for (int i = 0; i < HttpDownloadBasics.Count; i++)
            {
                HttpDownloadBasics[i].Update();
                if (HttpDownloadBasics[i].AllFinish())
                {
                    // if (HttpDownloadBasics[i].Result() != UnityWebRequest.Result.Success)
                    // {
                    //     Debug.Log("多次下载失败");
                    //     HttpDownloadBasics[i].maxCount = 2;
                    //     _awitDow.Add(HttpDownloadBasics[i] as UnityWebHttpDownload);
                    // }

                    HttpDownloadBasics.RemoveAt(i);
                    i--;
                }
            }

            if (_awitDow.Count > 0 && HttpDownloadBasics.Count < _maxDownloadCount)
            {
                for (int i = 0; i < _awitDow.Count; i++)
                {
                    if (HttpDownloadBasics.Count < _maxDownloadCount)
                    {
                        UnityWebHttpDownload download = _awitDow[i];
                        StartDownload(download);
                        _awitDow.RemoveAt(i);
                        i--;
                    }
                    else
                    {
                        break;
                    }
                }
            }

            for (int i = 0; i < _handerGroups.Count; i++)
            {
                _handerGroups[i].Update();
            }

            for (int i = 0; i < CheckUpdateList.Count; i++)
            {
                CheckUpdateList[i].CheckUpdate();
            }
        }
        catch (Exception e)
        {
            Debug.LogError(e);
        }
    }

    public void AddCheckUpdate(ICheckUpdate checkUpdate)
    {
        lock (CheckUpdateList)
        {
            CheckUpdateList.Add(checkUpdate);
        }
    }
    public void RemvoeCheckUpdate(ICheckUpdate checkUpdate)
    {
        lock (CheckUpdateList)
        {
            CheckUpdateList.Remove(checkUpdate);
        }
    }
}