﻿using UnityEditor;
using FileUtil = JLGames.RocketDriver.CSharp.Utils.FileUtil;
using EditorFileUtil = UnityEditor.FileUtil;

namespace JLGames.RocketDriver.Editor.Infra
{
    public class RefreshPackageItem
    {
        public bool Interactive;
        public string SourcePackagePath;
        public string LocalAssetPath;
        public string SavePackagePath;

        public void UpdatePackage()
        {
            if (string.IsNullOrEmpty(SourcePackagePath)) return;
            if (!string.IsNullOrEmpty(LocalAssetPath))
            {
                AssetDatabase.DeleteAsset(LocalAssetPath);
            }

            AssetDatabase.ImportPackage(SourcePackagePath, Interactive);

            if (!string.IsNullOrEmpty(SavePackagePath))
            {
                if (FileUtil.Exists(SavePackagePath))
                    EditorFileUtil.DeleteFileOrDirectory(SavePackagePath);
                EditorFileUtil.CopyFileOrDirectory(SourcePackagePath, SavePackagePath);
            }
        }
    }
}