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

namespace NBF
{
    public static class EditorUtils
    {
        
        /// <summary>
        /// 自动大小
        /// </summary>
        /// <param name="headerState"></param>
        /// <param name="maxWidth">最大宽</param>
        /// <param name="index">自由大小的序号</param>
        public static void AutoWidth(this MultiColumnHeaderState headerState, float maxWidth, int index = 0)
        {
            var columns = headerState.columns;
            if (columns == null) return;
            var residue = maxWidth - 16;
            for (int i = 0; i < columns.Length; i++)
            {
                var column = columns[i];
                if (i != index)
                {
                    residue -= column.width;
                }
            }

            if (residue < 100) residue = 100;
            columns[index].width = residue;
        }

        /// <summary>
        /// 将一个文件夹复制到另一个文件夹，根据包含的字符串和是否排除参数过滤处理的文件
        /// </summary>
        /// <param name="sourceDir">源目录</param>
        /// <param name="destDir">目标目录</param>
        /// <param name="containStr">文件路径包含的字符串，用于过滤后缀名</param>
        /// <param name="exclusion">是否排除包含字符串的路径</param>
        public static void CopySourceDirToDestDir(string sourceDir, string destDir, string containStr = null,
            bool exclusion = false)
        {
            IList<string> files = Directory.GetFiles(sourceDir, "*.*", SearchOption.AllDirectories);
            if (!string.IsNullOrEmpty(containStr))
            {
                files = files.FindAll((f) =>
                {
                    bool contain = f.Contains(containStr);
                    return exclusion ? !contain : contain;
                });
            }

            foreach (string file in files)
            {
                string relativePath = file.Substring(sourceDir.Length + 1);
                string destFilePath = Path.Combine(destDir, relativePath);
                string destFileDir = Path.GetDirectoryName(destFilePath);
                if (!Directory.Exists(destFileDir))
                {
                    Directory.CreateDirectory(destFileDir);
                }

                File.Copy(file, destFilePath, true);
            }
        }


        /// <summary>
        /// 创建新的ScriptableObject资产文件
        /// </summary>
        public static void CreateAsset<T>(string fullPath) where T : ScriptableObject
        {
            T asset = ScriptableObject.CreateInstance<T>();

            string directoryPath = Path.GetDirectoryName(fullPath);
            if (!Directory.Exists(directoryPath))
            {
                Directory.CreateDirectory(directoryPath);
            }

            string assetPathAndName = AssetDatabase.GenerateUniqueAssetPath(fullPath);

            AssetDatabase.CreateAsset(asset, assetPathAndName);

            AssetDatabase.SaveAssets();
            AssetDatabase.Refresh();
            EditorUtility.FocusProjectWindow();
            Selection.activeObject = asset;
        }

        /// <summary>
        /// 获取或创建ScriptableObject资产文件
        /// </summary>
        public static T GetOrCreateAsset<T>(string fullPath) where T : ScriptableObject
        {
            T asset = AssetDatabase.LoadAssetAtPath<T>(fullPath);
            if (asset == null)
            {
                CreateAsset<T>(fullPath);
                asset = AssetDatabase.LoadAssetAtPath<T>(fullPath);
            }

            return asset;
        }
    }
}