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

namespace XGame.Editor.Asset.Tools
{
    public class AssetsDependenciesWindow : EditorWindow
    {
        //[MenuItem("Assets/查找资源依赖")]
        public static void CreateWindow()
        {
            var paths = new List<string>();
            foreach (var obj in Selection.objects)
            {
                var assetPath = AssetDatabase.GetAssetPath(obj);
                if (Directory.Exists(assetPath))
                {
                    var files = Directory.GetFiles(assetPath, "*", SearchOption.AllDirectories);
                    foreach (var file in files)
                    {
                        paths.Add(FileUtil.ToRelativePath(file).Replace("\\", "/"));
                    }
                }
                else
                {
                    paths.Add(assetPath);
                }
            }
            paths = paths.Distinct().Where(p => FileUtil.IsFileIgnore(p) == false).ToList();
            if (paths.Count < 1) return;
            paths.Sort();

            var window = EditorWindow.GetWindow<AssetsDependenciesWindow>(true, "资源依赖窗口");
            window.Init(paths);
            window.Show();
        }
        private List<AssetDependenciesEditor> _lstDepEditor;
        private void Init(List<string> assetPaths)
        {
            _lstDepEditor = new List<AssetDependenciesEditor>();
            bool isFirst = true;
            var atlasRootPaths = FileUtil.GetAtlasRoots();
            foreach (var path in assetPaths)
            {
                var depEditor = new AssetDependenciesEditor(path, atlasRootPaths)
                {
                    IsFoldout = isFirst
                };
                if (isFirst)
                {
                    depEditor.Init();
                }
                isFirst = false;
                _lstDepEditor.Add(depEditor);

            }
        }
        //private string[] GetAtlasRootPaths()
        //{
        //    var atlasRoots = Processor.AssetsConfig.AtlasRoots;
        //    var rootPaths = Array.ConvertAll(atlasRoots, (root) => root.path);
        //    return rootPaths;
        //}
        private Vector2 _scrollPosition;
        private void OnGUI()
        {
            _scrollPosition = EditorGUILayout.BeginScrollView(_scrollPosition, false, true);
            EditorGUILayout.HelpBox("注意：\n1.图集资源需要先通过菜单: Build/CreateAtlas 创建图集后才能收集。\n2.资源显示红色表示该依赖资源在Res/Addressable目录下，需要检测该依赖资源是否需要移动到Res/Static目录。", MessageType.Warning);
            foreach (var depEditor in _lstDepEditor)
            {
                depEditor.OnGUI();
            }
            EditorGUILayout.EndScrollView();
        }

        private void OnDisable()
        {
            foreach (var depEditor in _lstDepEditor)
            {
                depEditor.Dispose();
            }
            _lstDepEditor.Clear();
            _lstDepEditor = null;
            //释放编辑器未使用的资源
            EditorUtility.UnloadUnusedAssetsImmediate();
        }

    }

}
