using XGame.Framework.Asset.Addressable;
using UnityEditor;
using UnityEngine;

namespace XGame.Editor.Asset
{
    public class AddressableInfoManifestTab
    {
        private AddressableClassify classifyOperation;
        private AddressableInfosSoEditor manifestEditor;
        private string[] assetsRoots;
        public AddressableInfoManifestTab(AddressableClassify classify)
        {
            classifyOperation = classify;
            var manifest = AddressableHelper.LoadAssetManifest(classify);
            if (manifest != null)
            {
                manifestEditor = new AddressableInfosSoEditor(manifest);
                assetsRoots = AddressableHelper.GetAssetsRoots(classify);
            }

        }
        private Vector2 scrollPosition;
        public void OnGUI()
        {
            if (manifestEditor != null)
            {
                if (assetsRoots != null)
                {
                    EditorGUI.BeginDisabledGroup(true);

                    EditorGUILayout.BeginVertical();
                    foreach (var resRoot in assetsRoots)
                    {
                        var fold = AssetDatabase.LoadMainAssetAtPath(resRoot);
                        if (fold != null)
                        {
                            EditorGUILayout.ObjectField("资源目录", fold, fold.GetType(), false);
                        }
                    }
                    EditorGUILayout.EndVertical();
                    EditorGUI.EndDisabledGroup();

                    //EditorGUILayout.BeginHorizontal();
                    scrollPosition = EditorGUILayout.BeginScrollView(scrollPosition, false, true);
                    manifestEditor?.OnGUI();
                    if (classifyOperation == AddressableClassify.Product)
                    {
                        if (GUILayout.Button("导出Excel"))
                        {
                            ExcelHelper.AddressableAssetsToExcel(classifyOperation);
                        }
                    }
                    EditorGUILayout.EndScrollView();
                    //EditorGUILayout.EndHorizontal();

                }
            }
        }
        public void Close()
        {
            manifestEditor?.Close();
        }
    }

}

