using System.IO;
using System.Collections;
using System.Collections.Generic;
using LitJson;
using UnityEditor;
using UnityEngine;
using Ionic.Zip;

public class BuildUpdateUtils
{
    public static void BuildLuaUpdateBundle(string zipDir, string luaFrameworkFilesVersion)
    {
        var needUpdateLuaFiles = GetNeedUpdateLuaList(luaFrameworkFilesVersion);
        GameLogger.Log("needUpdateFluaFiles.Count = " + needUpdateLuaFiles.Count);
        var luaBundleDir = BuildUtils.CreateTmpDir("luabundle");

        BuildUtils.CopyLuaToBuildDir(needUpdateLuaFiles, luaBundleDir);
        // 打包AssetBundle
        Hashtable tab = new Hashtable();
        tab["lua_update.bundle"] = "luabundle";
        AssetBundleBuild[] buildArray = BuildUtils.MakeAssetBundleBuildArray(tab);
        BuildUtils.BuildBundles(buildArray, zipDir);
        BuildUtils.DeleteDir(luaBundleDir);
        AssetDatabase.Refresh();
    }

    public static bool BuildObjBundle(string zipDir, List<UnityEngine.Object> objList)
    {
        if (0 == objList.Count) return false;
        AssetBundleBuild[] bundleBuilds = new AssetBundleBuild[objList.Count];
        bool contionsObj = false;
        for (int i = 0; i < objList.Count; i++)
        {
            var obj = objList[i];
            if (null == obj) continue;
            var assetPath = AssetDatabase.GetAssetPath(obj);
            var fileName = Path.GetFileName(assetPath);

            bundleBuilds[i].assetBundleName = fileName;
            bundleBuilds[i].assetNames = new string[] { assetPath };
            contionsObj = true;
        }
        BuildUtils.BuildBundles(bundleBuilds, zipDir);
        return contionsObj;
    }

    public static void ZipUpdateDir(string zipDir, bool contionsObj)
    {
        var zipFilePath = BuildUtils.BIN_PATH + "/" + (contionsObj ? "res_" : "script_") + VersionMgr.instance.resVersion + ".zip";
        if (File.Exists(zipFilePath))
        {
            File.Delete(zipFilePath);
        }
        ZipFile zipFile = new ZipFile();
        var fs = Directory.GetFiles(zipDir);
        foreach (var f in fs)
        {
            if (f.EndsWith("meta") || f.EndsWith("*.manifest"))
            {
                continue;
            }
            zipFile.AddFile(f, "");
        }
        zipFile.Save(zipFilePath);
        zipFile.Dispose();
    }

    /// <summary>
    /// 获取需要更新的Lua列表
    /// </summary>
    /// <returns></returns>
    public static List<string> GetNeedUpdateLuaList(string luaFrameworkFilesVersion)
    {
        var localLuaMD5 = BuildUtils.GetOriginalLuaframeworkMD5Json();
        var originalLuaMD5FilePath = BuildUtils.BIN_PATH + "/LuaFrameworkFiles_" + luaFrameworkFilesVersion + ".json";
        var originalLuaMD5File = File.OpenRead(originalLuaMD5FilePath);
        StreamReader sr = new StreamReader(originalLuaMD5File);
        var jsonStr = sr.ReadToEnd();
        sr.Close();
        var originalLuaMD5 = JsonMapper.ToObject(jsonStr);
        List<string> needUpdateLuaFiles = new List<string>();
        // 对比MD5值，不同的需要更新
        foreach (var key in localLuaMD5.Keys)
        {
            if (!originalLuaMD5.ContainsKey(key) || originalLuaMD5[key].ToString() != localLuaMD5[key].ToString())
            {
                needUpdateLuaFiles.Add(key);
            }
        }
        return needUpdateLuaFiles;
    }
}