using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using XRTool.Util;

public class LoadManager : Singleton<LoadManager>
{
    const int maxLoad = 3;
    int nowLoad = 0;
    public LoadManager()
    {
        TimerMgr.Instance.CreateTimer(() => {
            if (downloadQueueList.Count>0&& nowLoad< maxLoad)
            {
                int ct = downloadQueueList.Count;
           //     Debug.LogError("  downloadQueueList =====>   " + ct);
                for (int i = 0; i < ct; i++)
                {
                    DownLoadItem item = downloadQueueList.Dequeue();
                    item.onProgress += (float f) => {

                        Debug.Log("下载进度===>" + f);
                    };

                    item.callback += (bool b) => {

                        Debug.Log("下载完成===>" + nowLoad);
                        nowLoad--;
                    };
                    // GameScene.Instance.StartCoroutine( item.DownloadFile());
                    item.DownloadFileMsg();
                    nowLoad++;
                    if(nowLoad>= maxLoad)
                    {
                        break;
                    }

                }
            }
        
        
        }, 1,-1);

    }

    public Dictionary<string, DownLoadItem> downList = new Dictionary<string, DownLoadItem>();
    public void load(ModelItem model, Action<float> onProgress, Action<GameObject> callback)
    {
        string dName = getLoadName(model.url, model.Version);
        Debug.Log("dName ======>   " + dName+ "   model.url==>"+ model.url);
        if (!downList.ContainsKey(dName))
        {
            GameObject obj = new GameObject(dName);
            DownLoadItem dli = obj.AddComponent<DownLoadItem>();
            dli.Init(model.url, model.uid ,model.modelType,model.updateTime);
            downList.Add(dName, dli);
        }
        

        downList[dName].onProgress += (float f) => {

            onProgress.Invoke(f);
        };

        downList[dName].callback += (bool b) => {

            if(b)
            {
                if (!downList[dName].isAction)
                {
                    //涓嬭浇鎴愬姛骞舵棤澶勭悊
                    switch (model.modelType)
                    {
                        case ModelType.Image:

                            break;
                        case ModelType.Video:

                            break;
                        case ModelType.ABModel:

                            break;
                    }
                    if(downList[dName].downLoadData!=null)
                    {
                        Debug.Log(" LoadManager  " + dName + "  Length :  " + downList[dName].downLoadData.Length);
                        model.SetData(downList[dName].downLoadData);
                    }else if(model.prefabModel==null)
                    {
                        model.prefabModel = new GameObject("空物体"+ downList[dName].id);
                    }
                    callback.Invoke(model.prefabModel);
                }

            }
            else
            {
                //涓嬭浇澶辫触
            }
        };
    }

    public void loadVuforia(string xmlFile, string datFile, Action<bool> xmlcallback, Action<bool> datcallback)
    {
        if (!Directory.Exists(Application.persistentDataPath + "/StreamingAssets"))
        {
            Directory.CreateDirectory(Application.persistentDataPath + "/StreamingAssets");
            if(!Directory.Exists(Application.persistentDataPath + "/StreamingAssets/Vuforia"))
            {
                Directory.CreateDirectory(Application.persistentDataPath + "/StreamingAssets/Vuforia");
            }
        }

        GameObject xmlFileDownObj = new GameObject("VufroiaTriggerDownLoad_xmlFile");
        DownLoadItem xmlFileItem = xmlFileDownObj.AddComponent<DownLoadItem>();
        xmlFileItem.Init(xmlFile, "VufroiaTriggerDownLoad_xmlFile",ModelType.vuforial, long.Parse(GameManager.Instance.m_scene.updateTime));

        xmlFileItem.onProgress += (float f) => {

        };

        xmlFileItem.callback += (bool b) =>
        {
            Debug.Log("Vuforia 下载完成");
            if (b)
            {
                Debug.Log("Vuforia 下载完成  ===>" + VufroiaTrigger.LoacldatFile);
              //  File.WriteAllBytes(VufroiaTrigger.LoaclxmlFile, xmlFileItem.downLoadData);
            }
            xmlcallback.Invoke(b);
        };


        GameObject datFileObj = new GameObject("VufroiaTriggerDownLoad_datFile");
        DownLoadItem datFileItem = datFileObj.AddComponent<DownLoadItem>();
        datFileItem.Init(datFile, "VufroiaTriggerDownLoad_xmlFile",ModelType.vuforial, long.Parse(GameManager.Instance.m_scene.updateTime));

        datFileItem.onProgress += (float f) => {

        };

        datFileItem.callback += (bool b) =>
        {
            Debug.Log("Vuforia 下载完成");
            if (b)
            {
                Debug.Log("Vuforia 下载完成  ===>"+ VufroiaTrigger.LoacldatFile);
              //  File.WriteAllBytes(VufroiaTrigger.LoacldatFile, datFileItem.downLoadData);
            }
            datcallback.Invoke(b);
        };
    }


    public string getLoadName(string url, string version)
    {
        return url + "_" + version;
    }


    public Queue<DownLoadItem> downloadQueueList = new Queue<DownLoadItem>();
}