using RebootKit.Editor.Utils;
using RebootKit.Engine;
using RebootKit.Engine.Simulation;
using RebootKit.Engine.UI;
using UnityEditor;
using UnityEditor.SceneManagement;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UIElements;

namespace RebootKit.Editor.RebootWindow {
    public class WorldsView : IView {
        public void Dispose() {
        }

        public VisualElement Build() {
            VisualElement root = new VisualElement {
                style = {
                    paddingBottom = 4,
                    paddingTop = 4,
                    paddingLeft = 4,
                    paddingRight = 4,
                }
            };

            WorldConfigAsset[] worlds = AssetDatabaseEx.LoadAllAssets<WorldConfigAsset>();
            foreach (WorldConfigAsset worldConfigAsset in worlds) {
                VisualElement worldView = CreateWorldView(worldConfigAsset);
                root.Add(worldView);
            }

            return root;
        }

        VisualElement CreateWorldView(WorldConfigAsset worldConfigAsset) {
            VisualElement root = new VisualElement {
                style = {
                    backgroundColor = new Color(0.1f, 0.1f, 0.1f),
                    paddingBottom = 4,
                    paddingTop = 4,
                    paddingLeft = 4,
                    paddingRight = 4,
                    borderBottomLeftRadius = 4,
                    borderBottomRightRadius = 4
                }
            };

            Label label = new Label(worldConfigAsset.name) {
                style = {
                    color = new Color(0.7f, 0.9f, 0.9f),
                    unityFontStyleAndWeight = FontStyle.Bold
                }
            };
            root.Add(label);

            Button openButton = new Button(() => OpenWorldScenes(worldConfigAsset)) {
                text = "Open",
                style = {
                    backgroundColor = new Color(0.2f, 0.2f, 0.2f),
                    paddingBottom = 4,
                    paddingTop = 4,
                    paddingLeft = 4,
                    paddingRight = 4,
                    borderBottomLeftRadius = 4,
                    borderBottomRightRadius = 4
                }
            };
            root.Add(openButton);

            return root;
        }

        static void OpenWorldScenes(WorldConfigAsset worldConfigAsset) {
            if (worldConfigAsset == null) {
                Debug.LogError("WorldConfigAsset is null");
                return;
            }

            // Load first scene from build settings
            string mainScenePath = GetScenePathByBuildIndex(RConsts.k_MainSceneBuildIndex);
            if (mainScenePath == null) {
                return;
            }

            EditorSceneManager.OpenScene(mainScenePath, OpenSceneMode.Single);

            // Load world scene
            string worldScenePath = AssetDatabase.GUIDToAssetPath(worldConfigAsset.Config.mainScene.AssetGUID);
            if (string.IsNullOrEmpty(worldScenePath)) {
                Debug.LogError($"WorldConfigAsset {worldConfigAsset.name} has invalid main scene path");
                return;
            }

            SceneManager.SetActiveScene(EditorSceneManager.OpenScene(worldScenePath, OpenSceneMode.Additive));
        }

        static string GetScenePathByBuildIndex(int buildIndex) {
            if (buildIndex < 0 || buildIndex >= EditorBuildSettings.scenes.Length) {
                Debug.LogError($"Build index {buildIndex} out of range. Total scenes in build: {EditorBuildSettings.scenes.Length}");
                return null;
            }

            return EditorBuildSettings.scenes[buildIndex].path;
        }
    }
}