using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using Core.AssetLoadTool.Asset;
using Fort23.Core;
using Fort23.UTool;
using UnityEngine;
using UnityEngine.U2D;
using UnityEngine.UI;
using UnityEngine.UI.PackgTool;
using Utility;

public class UGUIPackLoad : IUGUIPackLoad
{
    public void Dispose()
    {
        for (int i = 0; i < _allHandle.Count; i++)
        {
            _allHandle[i].Release();
        }

        _allHandle.Clear();
        allSpriteAtlasMap.Clear();
    }

    private Map<string, AssetHandle> allSpriteAtlasMap = new Map<string, AssetHandle>();

    private List<AssetHandle> _allHandle = new List<AssetHandle>();

    public async Task InitPack()
    {
        if (!Application.isPlaying)
        {
            return;
        }

        CTaskAwaitBuffer cTaskAwaitBuffer = new CTaskAwaitBuffer();
#if UNITY_EDITOR
        if (AssetBundleLoadManager.Instance.BundleLoadType == BundleLoadType.LocalAsset)
        {
            string[] allFilePath = System.IO.Directory.GetFiles(Application.dataPath + @"/Art/UIAssets/TextrueJson/");
            for (int i = 0; i < allFilePath.Length; i++)
            {
                if (allFilePath[i].Contains(".meta"))
                {
                    continue;
                }

                string json = File.ReadAllText(allFilePath[i]);

                TextrueJson textrueJson = JsonManager.FromJson<TextrueJson>(json);
                if (textrueJson.isTyPack&&!textrueJson.packName .Equals("daoju"))
                {
                    cTaskAwaitBuffer.AddTask(AssetBundleLoadManager.Instance.LoadAssetAsyncTask<SpriteAtlas>(
                        textrueJson.packName + ".spriteatlasv2",
                        delegate(AssetHandle handle)
                        {
                            _allHandle.Add(handle);
                            for (int j = 0; j < textrueJson.newTextureJson.Count; j++)
                            {
                                allSpriteAtlasMap[textrueJson.newTextureJson[j].textrueName] = handle;
                            }
                        }));
                }
            }

            await cTaskAwaitBuffer.WaitAll();
            return;
        }

#endif

        // CTask cTask = CTask.Create(false);
        // cTaskAwaitBuffer.AddTask(cTask);
        // AssetBundleLoadManager.Instance.AddBundleTask("textruejson", delegate(BundleLoadBasic basic, object o)
        // {
        //     TextAsset[] allAssets = basic.currBundle.LoadAllAssets<TextAsset>();
        //     for (int i = 0; i < allAssets.Length; i++)
        //     {
        //         TextrueJson textrueJson = JsonManager.FromJson<TextrueJson>(allAssets[i].text);
        //         if (textrueJson.isTyPack&&!textrueJson.packName .Equals("daoju"))
        //         {
        //             cTaskAwaitBuffer.AddTask(AssetBundleLoadManager.Instance.LoadAssetAsyncTask<SpriteAtlas>(
        //                 textrueJson.packName + ".spriteatlasv2",
        //                 delegate(AssetHandle handle)
        //                 {
        //                     _allHandle.Add(handle);
        //                     for (int j = 0; j < textrueJson.newTextureJson.Count; j++)
        //                     {
        //                         allSpriteAtlasMap[textrueJson.newTextureJson[j].textrueName] = handle;
        //                     }
        //                 }));
        //         }
        //
        //         cTask.SetResult();
        //     }
        //     basic.RemoveOwnTask(null);
        // },null);
        await cTaskAwaitBuffer.WaitAll();
    }


    public SpriteAtlas GetSpriteAtlas(string name)
    {
        if (allSpriteAtlasMap.TryGetValue(name, out AssetHandle assetHandle))
        {
            return assetHandle?.AssetObject<SpriteAtlas>();
        }

        return null;
    }
}