/**************************************************** 
 文件：UtilityEditor.cs
 作者：张铮
 邮箱：834207172@qq.com
 日期：2022/3/3 17:53:52
 功能：
*****************************************************/
using UnityEngine;
using UnityEditor;
using System.IO;
using System.Collections.Generic;
namespace Stary.Evo.Editor
{
    public class UtilityEditor
    {
        
        [MenuItem("Evo/Utility/优化/检查模型/一键压缩模型的mesh")]
        static void 一键压缩所有模型的mesh()
        {
            AssetDatabase.StartAssetEditing();
            int count = 0;
            foreach (var guid in AssetDatabase.FindAssets("t:Model"))
            {
                var assetPath = AssetDatabase.GUIDToAssetPath(guid);
                var textureImporter = AssetImporter.GetAtPath(assetPath) as ModelImporter;
                if (textureImporter == null)
                    continue;
                if (textureImporter.meshCompression != ModelImporterMeshCompression.Off)
                {
                    textureImporter.meshCompression = ModelImporterMeshCompression.Off;
                    textureImporter.SaveAndReimport();
                    count++;
                }
            }
            Debug.Log("压缩了:" + count + "个模型");
            AssetDatabase.StopAssetEditing();
            AssetDatabase.Refresh();
        }

        [MenuItem("Evo/Utility/优化/检查模型/选择文件夹压缩模型mesh")]
        static void 选择文件夹压缩模型mesh()
        {
            if (Selection.objects.Length != 1)
            {
                Debug.LogError("选择一个文件夹");
                return;
            }

            string path = AssetDatabase.GetAssetPath(Selection.objects[0]);
            if (!Directory.Exists(path))
            {
                Debug.Log(path);
                return;
            }

            AssetDatabase.StartAssetEditing();
            int count = 0;
            string[] filePath = GetFilePath(Selection.objects);
            foreach (var guid in AssetDatabase.FindAssets("t:Model", filePath))
            {

                var assetPath = AssetDatabase.GUIDToAssetPath(guid);
                var textureImporter = AssetImporter.GetAtPath(assetPath) as ModelImporter;
                if (textureImporter == null)
                    continue;
                if (textureImporter.meshCompression != ModelImporterMeshCompression.Off)
                {
                    Debug.Log(assetPath);
                    textureImporter.meshCompression = ModelImporterMeshCompression.Off;
                    textureImporter.SaveAndReimport();
                    count++;
                }
            }

            Debug.Log("处理了:" + count + "个模型");
            AssetDatabase.StopAssetEditing();
            AssetDatabase.Refresh();
        }

        [MenuItem("Evo/Utility/优化/检查模型/去除文件夹内模型的ReadWrite")]
        static void 去除文件夹内模型的ReadWrite()
        {
            if (Selection.objects.Length != 1)
            {
                Debug.LogError("选择一个文件夹");
                return;
            }

            string path = AssetDatabase.GetAssetPath(Selection.objects[0]);
            if (!Directory.Exists(path))
            {
                Debug.Log(path);
                return;
            }

            AssetDatabase.StartAssetEditing();
            int count = 0;
            string[] filePath = GetFilePath(Selection.objects);
            foreach (var guid in AssetDatabase.FindAssets("t:Model", filePath))
            {
                var assetPath = AssetDatabase.GUIDToAssetPath(guid);
                var textureImporter = AssetImporter.GetAtPath(assetPath) as ModelImporter;
                if (textureImporter == null)
                    continue;
                if (textureImporter.isReadable)
                {
                    Debug.Log(assetPath);
                    textureImporter.isReadable = false;
                    textureImporter.SaveAndReimport();
                    count++;
                }
            }

            Debug.Log("处理了:" + count + "个模型");
            AssetDatabase.StopAssetEditing();
            AssetDatabase.Refresh();
        }

        public static string[] GetFilePath(Object[] targets)
        {
            var folders = new List<string>();
            for (int i = 0; i < targets.Length; i++)
            {
                string assetPath = AssetDatabase.GetAssetPath(targets[i]);
                if (Directory.Exists(assetPath))
                    folders.Add(assetPath);
            }

            return folders.ToArray();
        }

        [MenuItem("Evo/Utility/优化/检查模型/一键压缩模型的动画为Optimal")]
        static void 一键压缩模型的动画()
        {
            AssetDatabase.StartAssetEditing();
            int count = 0;
            foreach (var guid in AssetDatabase.FindAssets("t:Model"))
            {
                var assetPath = AssetDatabase.GUIDToAssetPath(guid);
                var textureImporter = AssetImporter.GetAtPath(assetPath) as ModelImporter;
                if (textureImporter == null)
                    continue;
                if (textureImporter.animationCompression != ModelImporterAnimationCompression.Optimal)
                {
                    Debug.Log(assetPath);
                    textureImporter.animationCompression = ModelImporterAnimationCompression.Optimal;
                    textureImporter.SaveAndReimport();
                    count++;
                }
            }

            Debug.Log("处理了:" + count + "个模型");
            AssetDatabase.StopAssetEditing();
            AssetDatabase.Refresh();
        }

        [MenuItem("Evo/Utility/优化/检查图片/去除文件夹内图片的ReadWrite")]
        static void 去除文件夹内图片的ReadWrite()
        {
            if (Selection.objects.Length != 1)
            {
                Debug.LogError("选择一个文件夹");
                return;
            }

            string path = AssetDatabase.GetAssetPath(Selection.objects[0]);
            if (!Directory.Exists(path))
            {
                Debug.Log(path);
                return;
            }

            AssetDatabase.StartAssetEditing();
            int count = 0;
            string[] filePath = GetFilePath(Selection.objects);
            foreach (var guid in AssetDatabase.FindAssets("t:Texture", filePath))
            {
                var assetPath = AssetDatabase.GUIDToAssetPath(guid);
                var textureImporter = AssetImporter.GetAtPath(assetPath) as TextureImporter;
                if (textureImporter == null)
                    continue;
                if (textureImporter.isReadable)
                {
                    Debug.Log(assetPath);
                    textureImporter.isReadable = false;
                    textureImporter.SaveAndReimport();
                    count++;
                }
            }

            Debug.Log("处理了:" + count + "个图片");
            AssetDatabase.StopAssetEditing();
            AssetDatabase.Refresh();
        }

        [MenuItem("Evo/Utility/优化/检查图片/去除文件夹内图片的mipmap")]
        static void 去除文件夹内图片的mipmap()
        {
            if (Selection.objects.Length != 1)
            {
                Debug.LogError("选择一个文件夹");
                return;
            }

            string path = AssetDatabase.GetAssetPath(Selection.objects[0]);
            if (!Directory.Exists(path))
            {
                Debug.Log(path);
                return;
            }

            AssetDatabase.StartAssetEditing();
            int count = 0;
            string[] filePath = GetFilePath(Selection.objects);
            foreach (var guid in AssetDatabase.FindAssets("t:Texture", filePath))
            {
                var assetPath = AssetDatabase.GUIDToAssetPath(guid);
                var textureImporter = AssetImporter.GetAtPath(assetPath) as TextureImporter;
                if (textureImporter == null)
                    continue;
                if (textureImporter.mipmapEnabled)
                {
                    Debug.Log(assetPath);
                    textureImporter.mipmapEnabled = false;
                    textureImporter.SaveAndReimport();
                    count++;
                }
            }

            Debug.Log("处理了:" + count + "个图片");
            AssetDatabase.StopAssetEditing();
            AssetDatabase.Refresh();
        }
        private static void DeleteEmptyDirectory(DirectoryInfo directory)
        {
            DirectoryInfo[] childDirectory = directory.GetDirectories();

            if (childDirectory.Length > 0)
            {
                for (int i = 0; i < childDirectory.Length; i++)
                {
                    DeleteEmptyDirectory(childDirectory[i]);
                }
            }

            if (directory.GetFileSystemInfos().Length == 0)
            {
                string path = directory.FullName;
                int index = path.IndexOf("Assets");
                path = path.Remove(0, index);
                directory.Delete();
            }
        }

        /// <summary>
        /// 打开文件夹
        /// </summary>
        /// <param name="path"></param>
        public static void OpenDirectory(string path)
        {
#if UNITY_EDITOR
            if (string.IsNullOrEmpty(path)) return;
            path = path.Replace("/", "\\");
            if (!Directory.Exists(path))
            {
                Debug.LogError("No Directory: " + path);
                return;
            }

            System.Diagnostics.Process.Start("explorer.exe", path);
#endif
        }

        /// <summary>
        /// 获取文件夹下所有文件路径
        /// </summary>
        /// <param name="path"></param>
        /// <param name="extension">"*.mp3"</param>
        /// <returns></returns>
        public static string[] GetFiles(string path, string extension = "*")
        {
            if (File.Exists(path))
            {
                return Directory.GetFiles(path, extension, SearchOption.AllDirectories);
            }
            else
            {
                return null;
            }
        }
    }
}