using System.IO;
using UnityEditor;
using UnityEngine;
using UnityEngine.UIElements;
using AssetBank.DockableWindow;
using System.Collections.Generic;
using AssetBank.Editor.DockableWindow;
using AssetBank.DockableWindow.UIHelper;

namespace AssetBank.Editor.Tools
{
    public class UnifiedExporter : EditorWindow
    {
        private VisualElement _rootElement;
        private VisualElement _contentContainer;
        private ProjectExporterController _controller;
        private GuidMapperExporter.ExportFor _exportFor;
        private Dictionary<string, AssetModel> _assetTrees;
        
        private bool _exportScenes = true;
        private bool _exportPrefabs = true;
        private bool _exportScriptableObjects = true;
        private bool _exportMetaFiles = true;
        private bool _exportSettings = true;
        private bool _exportScriptsIndependently;
        private bool _copyScripts = true;
        private bool _exportAllGuidMappers = true;
        
        [MenuItem("Tools/GUI/Unified Exporter")]
        public static void ShowWindow()
        {
            var wnd = GetWindow<UnifiedExporter>(true);
            wnd.titleContent = new GUIContent("Unified Exporter", EditorGUIUtility.IconContent("d_Profiler.GlobalIllumination").image);
            wnd.minSize = new Vector2(400, 300);
        }

        private void OnEnable()
        {
            _assetTrees = new Dictionary<string, AssetModel>();
            _controller = new ProjectExporterController();
            _rootElement = rootVisualElement;
            BuildUI();
        }

        private void BuildUI()
        {
            var scrollView = new ScrollView(ScrollViewMode.Vertical);
            _rootElement.Add(scrollView);
            _contentContainer = ProjectExporterUI.CreateMainContainer();
            scrollView.Add(_contentContainer);
            DrawToggles();
            
            var generateButton = ProjectExporterUI.CreateGenerateButton();
            _rootElement.Add(generateButton);
            generateButton.clicked += PerformExportOperation;
        }

        private void DrawToggles()
        {
            var scenesToggle = ProjectExporterUI.CreateToggle("Export Scenes", _exportScenes, evt => _exportScenes = evt.newValue);
            var prefabsToggle = ProjectExporterUI.CreateToggle("Export Prefabs", _exportPrefabs, evt => _exportPrefabs = evt.newValue);
            var soToggle = ProjectExporterUI.CreateToggle("Export Scriptable Objects", _exportScriptableObjects, evt => _exportScriptableObjects = evt.newValue);
            var metaToggle = ProjectExporterUI.CreateToggle("Export Meta Files", _exportMetaFiles, evt => _exportMetaFiles = evt.newValue);
            var settingsToggle = ProjectExporterUI.CreateToggle("Export Settings", _exportSettings, evt => _exportSettings = evt.newValue);
            var exportScriptsToggle = ProjectExporterUI.CreateToggle("Export Scripts Separately", _exportScriptsIndependently, evt => _exportScriptsIndependently = evt.newValue);
            var copyScriptsToggle = ProjectExporterUI.CreateToggle("Copy Scripts", _copyScripts, evt => _copyScripts = evt.newValue);
            var exportAllGuidMappersToggle = ProjectExporterUI.CreateToggle("Export All Guid Mappers", _exportAllGuidMappers, evt => _exportAllGuidMappers = evt.newValue);
            _contentContainer.Add(scenesToggle);
            _contentContainer.Add(prefabsToggle);
            _contentContainer.Add(soToggle);
            _contentContainer.Add(metaToggle);
            _contentContainer.Add(settingsToggle);
            
            var separator = ProjectExporterUI.CreateSeparator();
            _contentContainer.Add(separator);
            _contentContainer.Add(exportScriptsToggle);
            _contentContainer.Add(copyScriptsToggle);
            
            var separator2 = ProjectExporterUI.CreateSeparator();
            _contentContainer.Add(separator2);
            _contentContainer.Add(exportAllGuidMappersToggle);
        }

        private void PerformExportOperation()
        {
            var savePath = EditorUtility.SaveFolderPanel("Choose export destination", Application.dataPath, "");
            if (string.IsNullOrEmpty(savePath)) return;
            
            ExportAssetData(savePath);
            CopyScripts(savePath);
            ExportGuidMappers(savePath);
            
            EditorUtility.DisplayDialog("Bulk Export", "Bulk export process finished.", "OK");
        }

        private void ExportAssetData(string savePath)
        {
            if (_exportScenes) ExportAllOfType("Scenes", savePath);
            if (_exportPrefabs) ExportAllOfType("Prefabs", savePath);
            if (_exportScriptableObjects) ExportAllOfType("Scriptable Objects", savePath);
            if (_exportMetaFiles) ExportAllOfType("Meta Files", savePath);
            if (_exportSettings) ExportAllOfType("Settings", savePath);
        }

        private void CopyScripts(string savePath)
        {
            if (!_copyScripts) return;
            var exportPath = _exportScriptsIndependently ? Path.Combine(savePath, "Scripts") : Path.Combine(savePath, "Assets");
            ScriptExporter.ProcessAndCopyScripts(exportPath);
        }
        
        private void ExportGuidMappers(string savePath)
        {
            if (!_exportAllGuidMappers) return;
            var exportPath = Path.Combine(savePath, "GuidMappers");
            GuidMapperExporter.BulkExport(exportPath);
        }

        private void ExportAllOfType(string type, string savePath)
        {
            if (!_assetTrees.ContainsKey(type))
            {
                ProjectExporterDataHelper.LoadAssetTree(_assetTrees, _controller, type);
            }
            
            var rootModel = _assetTrees[type];
            if (rootModel != null)
            {
                ProjectExporterDataHelper.SetChildrenSelection(rootModel, true);
                _controller.ExportAssets(rootModel, savePath);
            }
        }
    }
}