using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using UnityEditor;
using UnityEngine;

namespace Stary.Evo.Editor
{
    public static class FileGet
    {
        public static void CreateContent(StringBuilder variable, string path, string tab)
        {
            tab += "\t\t";
            DirectoryInfo dir = new DirectoryInfo(path);
            DirectoryInfo[] first = dir.GetDirectories();
            FileInfo[] fil = dir.GetFiles();
            if (first.Length > 0)
            {
                foreach (FileInfo f in fil)
                {
                    CreateFile(variable, f, tab);
                }

                foreach (DirectoryInfo f in first)
                {
                    CreateFolder(variable, f, tab);
                }
            }
            else
            {
                foreach (FileInfo f in fil)
                {
                    CreateFile(variable, f, tab);
                }
            }
        }

        /// <summary>
        /// 生成脚本文件
        /// </summary>
        /// <param name="className">类名</param>
        /// <param name="content">字符串事件</param>
        public static void CreateClass(string className, string package, string outputPath,
            Action<StringBuilder> content = null)
        {
            string tdClassName = className;
            StringBuilder variable = new StringBuilder();
            //variable.Append("using System;\n");
            variable.Append("namespace " + "R" + "\n");
            variable.Append("{\n");
            variable.Append("\tpublic class " + tdClassName + "\n");
            variable.Append("\t{\n");
            variable.Append("\t\tpublic class " + package + "\n");
            variable.Append("\t\t{\n");
            content?.Invoke(variable);

            variable.Append("\t\t}\n");
            variable.Append("\t}\n");
            variable.Append("}");


            outputPath = $"{outputPath}/{tdClassName}.cs";
            if (File.Exists(outputPath))
            {
                File.Delete(outputPath);
            }
            FileHelper.WriteFile(outputPath, variable.ToString());

            AssetDatabase.SaveAssets();
            AssetDatabase.Refresh();
        }

        /// <summary>
        /// 文件夹生成
        /// </summary>
        /// <param name="folderName"></param>
        private static void CreateFolder(StringBuilder variable, DirectoryInfo folderName, string tab)
        {
            string classname = FilesUtils.ChineseToPinYin(folderName.Name, false);
            variable.Append(tab + " public static class " + classname + "\n");
            variable.Append(tab + "{\n");
            CreateContent(variable, folderName.FullName, tab);

            variable.Append(tab + "}\n");
        }

        /// <summary>
        /// 文件生成
        /// </summary>
        /// <param name="fileName"></param>
        private static void CreateFile(StringBuilder variable, FileInfo fileName, string tab)
        {
             var fullname =System.IO.Path.GetExtension(fileName.FullName);
            if (fullname != ".meta"&&fullname!=".hint" )
            {
                Debug.Log("FileInfo：" + fileName.Name);
                string[] fileSplit = fileName.FullName.Split(new string[] { "AddressableRes\\" },
                    StringSplitOptions.RemoveEmptyEntries);
                string[] resSplit = fileSplit[fileSplit.Length - 1].Split('\\');
                // fileSplit = fileName.FullName.Split(new string[] { $"AddressableRes\\{resSplit[0]}\\" },
                //     StringSplitOptions.RemoveEmptyEntries);
                // string filepath = fileSplit[fileSplit.Length - 1].Replace("\\", "/");
                // // 删除文件后缀
                // filepath = Path.Combine(
                //     Path.GetDirectoryName(filepath), 
                //     Path.GetFileNameWithoutExtension(filepath)
                // ).Replace("\\", "/");
                // Debug.Log(filepath);
                string filepath;
                if (MarkAdressable.addressDic.ContainsKey(fileName.FullName))
                {
                    filepath = MarkAdressable.addressDic[fileName.FullName];
                }
                else
                {
                    Debug.LogError("未找到地址,请检查资源自动化标记逻辑：" + fileName.FullName);
                    filepath = "";
                }


                string name = FilesUtils.ChineseToPinYin(fileName.Name.Replace(".", "_"), false)
                    .Replace(" ", "")
                    .Replace("-", "_")
                    .Replace(".", "_"); // 保留点号替换为下划线
                variable.Append(tab + "public const string " + name + " = \"" + filepath + "\";\n");
            }
        }
    }
}