using UnityEngine;
using UnityEditor;
using System;
using Thousandto.Editor.Excel;
using System.Collections.Generic;
using System.IO;
using UnityEngine.Profiling;

public class ResCheckEditor
{
    [MenuItem("Ares/检查所有图片资源并导出")]
    static void ExportAllTextureInfo()
    {
        string excelPath = EditorUtility.SaveFilePanel("Export Excel", "", "", "xlsx");
        if (string.IsNullOrEmpty(excelPath))
            return;

        NormalExcelInfo excelInfo = new NormalExcelInfo();
        excelInfo.Name = "textureinfo";
        var allTexture = AssetDatabase.FindAssets("t:Texture");
        excelInfo.Values = new string[allTexture.Length + 1, 6];
        excelInfo.Values[0, 0] = "path";
        excelInfo.Values[0, 1] = "name";
        excelInfo.Values[0, 2] = "mipmapEnabled";
        excelInfo.Values[0, 3] = "isReadable";
        excelInfo.Values[0, 4] = "size";
        excelInfo.Values[0, 5] = "format";
        for (int i = 0; i < allTexture.Length; ++i)
        {
            try
            {
                var path = AssetDatabase.GUIDToAssetPath(allTexture[i]);
                TextureImporter im = TextureImporter.GetAtPath(path) as TextureImporter;
                if (im != null)
                {
                    excelInfo.Values[i + 1, 0] = path;
                    excelInfo.Values[i + 1, 1] = im.assetBundleName;
                    excelInfo.Values[i + 1, 2] = im.mipmapEnabled ? "true" : "false";
                    excelInfo.Values[i + 1, 3] = im.isReadable ? "true" : "false";
                    var readable = im.isReadable;

                    im.isReadable = true;
                    AssetDatabase.ImportAsset(path, ImportAssetOptions.ForceUpdate);
                    var tex = AssetDatabase.LoadAssetAtPath<Texture2D>(path);
                    if (tex != null)
                    {
                        var rowData = tex.GetRawTextureData<byte>();
                        excelInfo.Values[i + 1, 4] = rowData.Length.ToString();
                        excelInfo.Values[i + 1, 5] = tex.format.ToString();
                        im.isReadable = readable;
                        AssetDatabase.ImportAsset(path, ImportAssetOptions.ForceUpdate);
                    }

                    EditorUtility.DisplayProgressBar(string.Format("导出中 {0}/{1}", i, allTexture.Length), path, (float)i / allTexture.Length);
                }
            }
            catch(Exception e)
            {

            }
        }
        ExcelWriter.Write(excelPath, excelInfo);
        EditorUtility.ClearProgressBar();
    }

    [MenuItem("Ares/检查所有模型资源并导出")]
    static void ExportAllMeshInfo()
    {
        string excelPath = EditorUtility.SaveFilePanel("Export Excel", "", "", "xlsx");
        if (string.IsNullOrEmpty(excelPath))
            return;

        NormalExcelInfo excelInfo = new NormalExcelInfo();
        excelInfo.Name = "meshinfo";
        var allModel = AssetDatabase.FindAssets("t:Model");
        var meshList = new List<Mesh>(allModel.Length * 2);
        var pathList = new List<string>(allModel.Length * 2);
        for (int i = 0; i < allModel.Length; ++i)
        {
            try
            {
                var path = AssetDatabase.GUIDToAssetPath(allModel[i]);
                var assets = AssetDatabase.LoadAllAssetsAtPath(path);
                if (assets != null)
                {
                    for (int j = 0; j < assets.Length; ++j)
                    {
                        var mesh = assets[j] as Mesh;
                        if (mesh != null)
                        {
                            pathList.Add(path);
                            meshList.Add(mesh);
                        }
                    }
                    EditorUtility.DisplayProgressBar(string.Format("导出中 {0}/{1}", i, allModel.Length), path, (float)i / allModel.Length);
                }
            }
            catch (Exception e)
            {
            }
        }
        excelInfo.Values = new string[meshList.Count + 1, 5];
        excelInfo.Values[0, 0] = "path";
        excelInfo.Values[0, 1] = "name";
        excelInfo.Values[0, 2] = "verts";
        excelInfo.Values[0, 3] = "tris";
        excelInfo.Values[0, 4] = "uv";
        for (int i = 0; i < meshList.Count; ++i)
        {
            var mesh = meshList[i];
            excelInfo.Values[i + 1, 0] = pathList[i];
            excelInfo.Values[i + 1, 1] = mesh.name;
            excelInfo.Values[i + 1, 2] = mesh.vertexCount.ToString();
            excelInfo.Values[i + 1, 3] = (mesh.triangles.LongLength / 3).ToString();
            if (mesh.uv8 != null && mesh.uv8.Length > 0)
            {
                excelInfo.Values[i + 1, 4] = "8";
            }
            else if(mesh.uv7 != null && mesh.uv7.Length > 0)
            {
                excelInfo.Values[i + 1, 4] = "7";
            }
            else if(mesh.uv6 != null && mesh.uv6.Length > 0)
            {
                excelInfo.Values[i + 1, 4] = "6";
            }
            else if(mesh.uv5 != null && mesh.uv5.Length > 0)
            {
                excelInfo.Values[i + 1, 4] = "5";
            }
            else if(mesh.uv4 != null && mesh.uv4.Length > 0)
            {
                excelInfo.Values[i + 1, 4] = "4";
            }
            else if(mesh.uv3 != null && mesh.uv3.Length > 0)
            {
                excelInfo.Values[i + 1, 4] = "3";
            }
            else if(mesh.uv2 != null && mesh.uv2.Length > 0)
            {
                excelInfo.Values[i + 1, 4] = "2";
            }
            else
            {
                excelInfo.Values[i + 1, 4] = "1";
            }
        }
        ExcelWriter.Write(excelPath, excelInfo);
        EditorUtility.ClearProgressBar();
    }

    [MenuItem("Ares/检查所有动作资源并导出")]
    static void ExportAllAnimationInfo()
    {
        string excelPath = EditorUtility.SaveFilePanel("Export Excel", "", "", "xlsx");
        if (string.IsNullOrEmpty(excelPath))
            return;

        NormalExcelInfo excelInfo = new NormalExcelInfo();
        excelInfo.Name = "meshinfo";
        var allModel = AssetDatabase.FindAssets("t:Model");
        var allAnimation = AssetDatabase.FindAssets("t:AnimationClip");
        var allCount = allModel.Length + allAnimation.Length;
        var meshList = new List<AnimationClip>(allCount);
        var pathList = new List<string>(allCount);
        var fileNameList = new List<string>(allCount);

        for (int i = 0; i < allModel.Length; ++i)
        {
            try
            {
                var path = AssetDatabase.GUIDToAssetPath(allModel[i]);
                var assets = AssetDatabase.LoadAllAssetsAtPath(path);
                if (assets != null)
                {
                    var asset = AssetDatabase.LoadAssetAtPath<GameObject>(path);
                    for (int j = 0; j < assets.Length; ++j)
                    {
                        var clip = assets[j] as AnimationClip;
                        if (clip != null && !clip.name.StartsWith("__preview__"))
                        {
                            pathList.Add(path);
                            meshList.Add(clip);
                            fileNameList.Add(asset.name);
                        }
                    }
                    EditorUtility.DisplayProgressBar(string.Format("导出中 {0}/{1}", i, allCount), path, (float)i / allCount);
                }
            }
            catch (Exception e)
            {
            }
        }

        for (int i = 0; i < allAnimation.Length; ++i)
        {
            try
            {
                var path = AssetDatabase.GUIDToAssetPath(allAnimation[i]);
                var assets = AssetDatabase.LoadAllAssetsAtPath(path);
                if (assets != null)
                {
                    for (int j = 0; j < assets.Length; ++j)
                    {
                        var clip = assets[j] as AnimationClip;
                        if (clip != null)
                        {
                            pathList.Add(path);
                            meshList.Add(clip);
                            fileNameList.Add(clip.name);
                        }
                    }
                    EditorUtility.DisplayProgressBar(string.Format("导出中 {0}/{1}", i + allModel.Length, allCount), path, (float)(i + allModel.Length) / allCount);
                }
            }
            catch (Exception e)
            {
            }
        }

        excelInfo.Values = new string[meshList.Count + 1, 6];
        excelInfo.Values[0, 0] = "path";
        excelInfo.Values[0, 1] = "file";
        excelInfo.Values[0, 2] = "name";
        excelInfo.Values[0, 3] = "fps";
        excelInfo.Values[0, 4] = "length";
        excelInfo.Values[0, 5] = "memsize";
        for (int i = 0; i < meshList.Count; ++i)
        {
            var mesh = meshList[i];
            excelInfo.Values[i + 1, 0] = pathList[i];
            excelInfo.Values[i + 1, 1] = fileNameList[i];
            excelInfo.Values[i + 1, 2] = mesh.name;
            excelInfo.Values[i + 1, 3] = mesh.frameRate.ToString();
            excelInfo.Values[i + 1, 4] = mesh.length.ToString();
            excelInfo.Values[i + 1, 5] = Profiler.GetRuntimeMemorySizeLong(mesh).ToString();
        }
        ExcelWriter.Write(excelPath, excelInfo);
        EditorUtility.ClearProgressBar();
    }
}