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

public static class ResourceTools
{
    [MenuItem("ResourceTool/OutputEffectRes")]
    public static void OutputEffectRes()
    {
        var effectPath = LoadAssetBundle.assetPathHeader.Open("Effect");
        var prefabPaths = (from path in AssetDatabase.GetAllAssetPaths()
            where path.StartsWith(effectPath)
            where Path.GetExtension(path) == ".prefab"
            select path).ToArray();
        var builder = new StringBuilder();
        var objects = new Object[prefabPaths.Length];
        for (var i = 0; i < prefabPaths.Length; i++)
        {
            var prefabPath = prefabPaths[i];
            objects[i] = AssetDatabase.LoadAssetAtPath<GameObject>(prefabPath);
        }
        Debug.LogWarning("GameObject: " + prefabPaths.Length);
        var collections = EditorUtility.CollectDependencies(objects);
        Debug.LogWarning("Dependencies: " + collections.Length);
        var materialList = new List<EffectMaterial>();
        for (var i = 0; i < collections.Length; i++)
        {
            var material = collections[i] as Material;
            if (material != null)
                materialList.Add(new EffectMaterial(material));
        }
        builder.AppendLine("******** 特效贴图列表 ********");
        for (var i = 0; i < materialList.Count; i++)
        {
            materialList[i].ExpandTexture();
            builder.AppendLine();
            builder.AppendLine("材质" + AssetDatabase.GetAssetPath(materialList[i].material));
            for (var j = 0; j < materialList[i].textureList.Count; j++)
                builder.AppendLine("-- " + materialList[i].textureList[j].GetTextureString());
        }
        var directory = Application.dataPath + Path.DirectorySeparatorChar + "_Test";
        if (!Directory.Exists(directory))
            Directory.CreateDirectory(directory);
        File.WriteAllText(directory + Path.DirectorySeparatorChar + "EffectTextures.txt", builder.ToString());
    }
    
    public class EffectMaterial
    {
        public readonly Material material;
        public readonly List<EffectTexture> textureList;

        public EffectMaterial(Material material)
        {
            this.material = material;
            textureList = new List<EffectTexture>();
        }

        public void ExpandTexture()
        {
            var collections = EditorUtility.CollectDependencies(new Object[]{material});
            for (var i = 0; i < collections.Length; i++)
            {
                var texture = collections[i] as Texture2D;
                if (texture != null)
                    textureList.Add(new EffectTexture(texture));
            }
            textureList.Sort();
        }
    }
    
    
    public class EffectTexture : System.IComparable<EffectTexture>
    {
        public readonly Texture2D texture;
        
        public EffectTexture(Texture2D texture)
        {
            this.texture = texture;
        }

        public int CompareTo(EffectTexture obj)
        {
            var result = -texture.width.CompareTo(obj.texture.width);
            if (result == 0)
                result = -texture.height.CompareTo(obj.texture.height);
            return result;
        }

        public string GetTextureString()
        {
            return string.Format("{0} x {1} 贴图{2}", texture.width, texture.height, AssetDatabase.GetAssetPath(texture));
        }
    }
}