﻿#if UNITY_EDITOR
using System.Text;
using JLGames.RocketDriver.Actions.Utils;
using JLGames.RocketDriver.CSharp.Archive;
using JLGames.RocketDriver.CSharp.Event;
using JLGames.RocketDriver.Editor.Infra;
using UnityEditor;
using UnityEngine;

namespace JLGames.RocketDriver.Samples.ArchiveDemo
{
    public static class MenuTestUnzip
    {
        private static readonly string SrcPath =
            UnityPathUtil.CombineUnityPath(Application.dataPath, "JLGames/RocketDriver/Samples/Archive/Assets/Src/a_3_18.zip");

        private static readonly string DstPath =
            UnityPathUtil.CombineUnityPath(Application.dataPath, "JLGames/RocketDriver/Samples/Archive/Assets/Dst");

        // [MenuItem("Tools/RocketDriver Samples/Import Unzip Env", false, 100)]
        static void TestImportUnzipEnv()
        {
            var importSettings = Resources.Load<ImportSettings>("ArchiveImportSettings");
            ImportPackageUtil.ImportPackage(importSettings.Settings.MainPackage);
        }

        // [MenuItem("Tools/RocketDriver Samples/Test Unzip", false, 101)]
        static void UnzipInMenu()
        {
            DoUnzip();
        }

        private static void DoUnzip()
        {
//        ArchiveUtil.UnzipFile(SrcPath, DstPath, false, Encoding.UTF8);
//        return;
            var @params = new UnarchiveParams(DstPath, false, Encoding.UTF8);
            var unzip = new Unzip();
            unzip.SetUnarchiveParams(@params);
            unzip.OnceEventListener(UnarchiveEvents.EventUnarchive, OnUnArchive);
            unzip.AddEventListener(UnarchiveEvents.EventUnarchiveEntry, OnUnarchiveEntry);
            unzip.UnarchiveFile(SrcPath);
            AssetDatabase.Refresh();
        }

        private static void OnUnArchive(EventData evd)
        {
            Debug.Log("OnUnArchive:" + evd.Data);
            evd.CurrentDispatcher.RemoveEventListener(evd.Type, OnUnArchive);
        }

        private static void OnUnarchiveEntry(EventData evd)
        {
//        Debug.Log("OnUnarchiveEntry:" + evd.Data);
        }
    }
}
#endif