using System.IO;
using UnityEditor;
using UnityEngine;

namespace FL.Editor.Imports
{
    public class ModelImportTask
    {
        [MenuItem("Tools/FL/导入模型Spine")]
        public static void Test()
        {
            var task = new ModelImportTask();
            task.Start();
        }
        private const string export_folder = "Export";
        private const string spine_atlas_txt = ".atlas.txt";
        private const string spine_png = ".png";
        private const string spine_skel_bytes = ".skel.bytes";
        public void Start()
        {
            var config = CustomModelConfig.Load();
            if (config == null)
            {
                return;
            }
            if (!Directory.Exists(config.customModelsPath))
            {
                Debug.LogError($"外部资源根路径错误:{config.customModelsPath}");
                return;
            }

            foreach (var item in config.paths)
            {
                var fromPath = Path.Combine(config.customModelsPath, item.from);
                var toPath = item.to;
                if (!Directory.Exists(fromPath) || !Directory.Exists(toPath))
                {
                    Debug.Log($"无效的路径配置.From:{item.from} To:{item.to}");
                    continue;
                }
                var subFolders = Directory.GetDirectories(fromPath);
                foreach (var subFolder in subFolders)
                {
                    CopyFiles(subFolder, toPath);
                }
            }
            AssetDatabase.SaveAssets();
            AssetDatabase.Refresh();
        }
        private void CopyFiles(string fromPath, string toPath)
        {
            var modelName = Path.GetFileName(fromPath);
            if (XGame.Editor.Asset.FileUtil.HasChinese(modelName) || modelName.Contains(' '))
            {
                Debug.LogError($"模型名字包含中文或者空格. Path:{fromPath}");
                return;
            }
            var atlasFile = modelName + spine_atlas_txt;
            var atlasPath = Path.Combine(fromPath, export_folder, atlasFile);
            if (!File.Exists(atlasPath))
            {
                Debug.Log($"找不到模型图集文件. Path:{atlasPath}");
                return;
            }
            var pngFile = modelName + spine_png;
            var pngPath = Path.Combine(fromPath, export_folder, pngFile);
            if (!File.Exists(pngPath))
            {
                Debug.Log($"找不到模型贴图. Path:{pngPath}");
                return;
            }
            var skelFile = modelName + spine_skel_bytes;
            var skelPath = Path.Combine(fromPath, export_folder, skelFile);
            if (!File.Exists(skelPath))
            {
                Debug.Log($"找不到模型动作文件. Path:{skelPath}");
                return;
            }
            toPath = Path.Combine(toPath, modelName);
            if (!Directory.Exists(toPath))
            {
                Directory.CreateDirectory(toPath);
            }
            CopyFile(atlasPath, Path.Combine(toPath, atlasFile));
            CopyFile(pngPath, Path.Combine(toPath, pngFile));
            CopyFile(skelPath, Path.Combine(toPath, skelFile));
        }
        private void CopyFile(string fromPath, string toPath)
        {
            if (File.Exists(toPath))
            {
                FileUtil.DeleteFileOrDirectory(toPath);
            }
            FileUtil.CopyFileOrDirectory(fromPath, toPath);
        }
    }
}
