using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.Networking;

/// <summary>
/// 加载文件辅助类
/// </summary>
public class LoadFileHelper : MonoBehaviour
{
    public static LoadFileHelper Instance = null;

    void Awake()
    {
        DontDestroyOnLoad(this);
        Instance = this;
    }

    /// <summary>
    /// 从本地外部目录或本地StreamingAssets目录异步加载文件,得到文件string
    /// </summary>
    /// <param name="path"></param>
    /// <param name="complete"></param>
    public void LoadFileStringAsync(string path, Action<string> complete)
    {
        if (string.IsNullOrEmpty(path))
        {
            throw new ArgumentNullException(path);
        }

        StartCoroutine(GetFileString(path, complete));
    }

    private IEnumerator GetFileString(string path, Action<string> complete)
    {
        using (UnityWebRequest unityWebRequest = UnityWebRequest.Get(path))
        {
            yield return unityWebRequest.SendWebRequest();

            string fileString = unityWebRequest.downloadHandler.text;

            if (complete != null)
                complete(fileString);
        }
    }

    /// <summary>
    /// 从本地外部目录或本地StreamingAssets目录异步加载文件,得到文件byte数组
    /// </summary>
    /// <param name="path"></param>
    /// <param name="complete"></param>
    public void LoadFileBytesAsync(string path, Action<byte[]> complete)
    {
        if (string.IsNullOrEmpty(path))
        {
            throw new ArgumentNullException(path);
        }

        StartCoroutine(GetFileBytes(path, complete));
    }

    private IEnumerator GetFileBytes(string path, Action<byte[]> complete)
    {
        using (UnityWebRequest unityWebRequest = UnityWebRequest.Get(path))
        {
            yield return unityWebRequest.SendWebRequest();

            byte[] bytes = unityWebRequest.downloadHandler.data;

            if (complete != null)
                complete(bytes);
        }
    }

    /// <summary>
    /// 从本地外部目录或本地StreamingAssets目录异步加载多张或一张图片
    /// </summary>
    /// <param name="paths"></param>
    /// <param name="complete"></param>
    public void LoadPicturesAsync(string[] paths, Action<List<Texture2D>> complete)
    {
        if (paths != null)
        {
            StartCoroutine(LoadTexture2Ds(paths, complete));
        }
    }

    private IEnumerator LoadTexture2Ds(string[] paths, Action<List<Texture2D>> complete)
    {
        List<Texture2D> textures = new List<Texture2D>();

        foreach (var path in paths)
        {
            using (UnityWebRequest unityWebRequest = UnityWebRequestTexture.GetTexture(path))
            {
                yield return unityWebRequest.SendWebRequest();

                Texture2D texture = DownloadHandlerTexture.GetContent(unityWebRequest);

                textures.Add(texture);
            }
        }

        if (complete != null)
        {
            complete(textures);
        }
    }

    /// <summary>
    /// 从本地外部目录或本地StreamingAssets目录异步加载多个或一个AB
    /// </summary>
    /// <param name="paths"></param>
    /// <param name="complete"></param>
    public void LoadAssetBundlesAsync(string[] paths, Action<List<AssetBundle>> complete)
    {
        if (paths != null)
        {
            StartCoroutine(LoadAssetBundles(paths, complete));
        }
    }

    private IEnumerator LoadAssetBundles(string[] paths, Action<List<AssetBundle>> complete)
    {
        List<AssetBundle> assetBundles = new List<AssetBundle>();

        foreach (var path in paths)
        {
            using (UnityWebRequest unityWebRequest = UnityWebRequestAssetBundle.GetAssetBundle(path))
            {
                yield return unityWebRequest.SendWebRequest();

                AssetBundle assetBundle = DownloadHandlerAssetBundle.GetContent(unityWebRequest);

                assetBundles.Add(assetBundle);
            }
        }

        if (complete != null)
        {
            complete(assetBundles);
        }
    }

    /// <summary>
    /// 从本地外部目录或本地StreamingAssets目录同步加载文件,得到文件string
    /// </summary>
    /// <param name="path"></param>
    /// <param name="complete"></param>
    public void LoadFileStringSync(string path, Action<string> complete)
    {
        if (!File.Exists(path))
            return;
        StreamReader sr = new StreamReader(path);
        if (sr == null)
            return;

        string fileString = sr.ReadToEnd();
        sr.Close();
        sr.Dispose();
        if (complete != null)
            complete(fileString);
    }
}