using System.IO;
using System.Linq;
using UnityEngine.Events;
using System.Text.RegularExpressions;
using Application = UnityEngine.Application;


#if UNITY_EDITOR
#endif

namespace ChillConnect
{
    public class ChillConnectFileKit
    {
        private static bool isLog = true;

        private static string AssetBundleRootUrl = Application.streamingAssetsPath + "/ChillConnectAssets/AssetBundles/";
        private static string mFileUrl = Application.streamingAssetsPath + "/ChillConnectAssets/ChillConnectFile.txt";
        public static string ConfigFileUrl = Application.streamingAssetsPath + "/Config/JarvisConfigFile.txt";

        public static string GetSavePath()
        {
            return Application.persistentDataPath;
        }

        public static string GetLocalSavePath()
        {
            return Application.streamingAssetsPath;
        }



        public static string GetFilePath()
        {
            return $"{GetSavePath()}/ChillConnectAssets/";
        }

        public static void GetLocalAssetBundle(UnityAction<int> onPreloadCompleted, UnityAction loadCompleted)
        {
            GetAssetFile(s => { GetMAssetBundle(s, onPreloadCompleted, loadCompleted); });
        }


        public static void GetMAssetBundle(string content, UnityAction<int> onPreloadCompleted,
            UnityAction onLoadCompleted)
        {
            if (content.IsNullOrWhiteSpace())
            {
                return;
            }


            var mFile = Regex.Split(content, "\r\n", RegexOptions.IgnoreCase).ToList();
            for (var i = mFile.Count - 1; i >= 0; i--)
            {
                var aotMetaAssemblyFile = mFile[i];
                if (string.IsNullOrEmpty(aotMetaAssemblyFile) || string.IsNullOrWhiteSpace(aotMetaAssemblyFile))
                {
                    mFile.RemoveAt(i);
                }
            }


            var completedCount = mFile.Count;
            onPreloadCompleted?.Invoke(completedCount);


            foreach (var m in mFile)
            {
                if (m.IsNullOrWhiteSpace())
                {
                    onLoadCompleted?.Invoke();
                    completedCount--;
                    if (completedCount <= 0)
                    {
                        OnGetAssetBundleCompleted();
                    }

                    continue;
                }


                var assetBundleNameInfoArray =
                    m.Split(ChillConnectConstant.fgklpk.ToCharArray());

                var fileName = assetBundleNameInfoArray[0];

                var mUrl = $"{AssetBundleRootUrl}{fileName}";

                var mFilePath = $"{GetFilePath()}{fileName}";

                if (File.Exists(mFilePath))
                {
                    var fileMD5 = MD5Kit.GetFileMD5(mFilePath);

                    var fileMD5New = assetBundleNameInfoArray[1];
                    if (fileMD5.Equals(fileMD5New))
                    {
                        if (isLog)
                        {
                        }

                        onLoadCompleted?.Invoke();
                        completedCount--;
                        if (completedCount <= 0)
                        {
                            OnGetAssetBundleCompleted();
                        }

                        continue;
                    }

                    if (isLog)
                    {
                    }
                }


                CrazyAsyKit.StartCoroutine(DownloadKit.GetFileFromUrl(mUrl, fileName, isSuccess =>
                {
                    if (isLog)
                    {
                    }

                    onLoadCompleted?.Invoke();
                    completedCount--;
                    if (completedCount <= 0)
                    {
                        OnGetAssetBundleCompleted();
                    }
                }));
            }
        }


        private static void OnGetAssetBundleCompleted()
        {
            ChillConnectKit.Instance.InitManifest(ChillConnectConstant.lesest,
                manifestInfo =>
                {
                    AppDispatcher.Instance.Dispatch(AppMsg.UI_LoadingInitAsset);
                });
        }

        public static void GetAssetFile(UnityAction<string> action)
        {

            CrazyAsyKit.StartCoroutine(DownloadKit.GetTextFromUrl(mFileUrl, "ChillConnectFile",
                content => { action?.Invoke(content); }));
        }
    }
}