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

public class BuildUpdateZip : EditorWindow
{
    private VersionGUI mVersionGUI;
    private string mLuaFrameworkFilesVersion;
    private List<UnityEngine.Object> mObjList = new List<UnityEngine.Object>();
    private Vector2 mScrollViewPos;

    [MenuItem("Build/打热更包")]
    public static void ShowWindow()
    {
        var win = GetWindow<BuildUpdateZip>();
        win.Show();
    }

    private void Awake()
    {
        mVersionGUI = new VersionGUI();
        mVersionGUI.Awake();
        mLuaFrameworkFilesVersion = BuildUtils.TryGetLuaFrameworkFilesVersion();
    }

    private void OnGUI()
    {
        mVersionGUI.DrawVersion();
        DoBuildUpdateZip();
    }

    private void DoBuildUpdateZip()
    {
        GUILayout.BeginHorizontal();
        GUILayout.Label("LuaFrameworkFiles.json");
        mLuaFrameworkFilesVersion = EditorGUILayout.TextField(mLuaFrameworkFilesVersion);
        GUILayout.EndHorizontal();
        if (GUILayout.Button("打热更包"))
        {
            var zipDir = BuildUtils.CreateTmpDir("updateZip");
            BuildUtils.BuildNormalCfgBundle(zipDir);
            BuildUpdateUtils.BuildLuaUpdateBundle(zipDir, mLuaFrameworkFilesVersion);
            var contionsObj = BuildUpdateUtils.BuildObjBundle(zipDir, mObjList);
            BuildUpdateUtils.ZipUpdateDir(zipDir, contionsObj);
            BuildUtils.DeleteDir(zipDir);
            AssetDatabase.Refresh();
        }
        DrawObjList();
    }

    private void DrawObjList()
    {
        mScrollViewPos = GUILayout.BeginScrollView(mScrollViewPos);
        if (GUILayout.Button("+"))
        {
            mObjList.Add(null);
        }
        for (int i = 0; i < mObjList.Count; ++i)
        {
            GUILayout.BeginHorizontal();
            mObjList[i] = EditorGUILayout.ObjectField(mObjList[i], typeof(UnityEngine.Object), false);
            if (GUILayout.Button("-"))
            {
                mObjList.RemoveAt(i);
                break;
            }
            GUILayout.EndHorizontal();
        }

        GUILayout.EndScrollView();

    }
}