using RebootKit.Engine.Console;
using RebootKit.Engine.UI;
using UnityEditor;
using UnityEngine;
using UnityEngine.UIElements;

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

        public VisualElement Build() {
            VisualElement rootContainer = new VisualElement {
                style = {
                    flexGrow = 1,
                    fontSize = 14
                }
            };

            Label label = new Label($"{Application.productName} {Application.version}") {
                style = {
                    fontSize = 18,
                    unityFontStyleAndWeight = FontStyle.Bold
                }
            };
            rootContainer.Add(label);

            VisualElement persistentPathContainer = new VisualElement {
                style = {
                    marginTop = 8,
                    marginBottom = 8,
                    paddingLeft = 4,
                    paddingRight = 4,
                    paddingTop = 4,
                    paddingBottom = 4,
                    borderLeftWidth = 1,
                    borderLeftColor = new Color(0.3f, 0.3f, 0.3f),
                    flexDirection = FlexDirection.Row,
                }
            };

            Label persistentPathLabel = new Label($"Persistent Path: {Application.persistentDataPath}") {
                style = {
                    fontSize = 12,
                    color = new Color(0.7f, 0.9f, 0.9f)
                }
            };
            persistentPathContainer.Add(persistentPathLabel);

            Button openPersistentPathButton = new Button(() => {
                Application.OpenURL(Application.persistentDataPath);
            }) {
                style = {
                    fontSize = 12,
                    width = 48
                },
                text = "Open"
            };
            persistentPathContainer.Add(openPersistentPathButton);

            rootContainer.Add(persistentPathContainer);
            
            Label onGameRunScriptLabel = new Label("On Game Run Script (User):") {
                style = {
                    fontSize = 12,
                    color = new Color(0.7f, 0.9f, 0.9f)
                }
            };
            rootContainer.Add(onGameRunScriptLabel);
            
            TextField onGameRunScriptTextField = new TextField {
                style = {
                    fontSize = 12,
                },
                multiline = true,
                value = EditorPrefs.GetString(REditorConsts.k_OnGameRunScriptContentKey, "")
            };
            
            onGameRunScriptTextField.RegisterValueChangedCallback(evt => {
                EditorPrefs.SetString(REditorConsts.k_OnGameRunScriptContentKey, evt.newValue);
            });
            rootContainer.Add(onGameRunScriptTextField);
            
            Label consoleCommandsLabel = new Label("Console Commands:") {
                style = {
                    fontSize = 12,
                    color = new Color(0.7f, 0.9f, 0.9f)
                }
            };
            rootContainer.Add(consoleCommandsLabel);

            ConsoleService.ConsoleCommand[] consoleCommands = ConsoleService.GenerateCommandsToRegister();
            foreach (ConsoleService.ConsoleCommand consoleCommand in consoleCommands) {
                VisualElement commandContainer = new VisualElement {
                    style = {
                        flexDirection = FlexDirection.Row,
                        marginTop = 4,
                        marginBottom = 4,
                        paddingLeft = 4,
                        paddingRight = 4,
                        paddingTop = 4,
                        paddingBottom = 4,
                    }
                };
                Label commandLabel = new Label(consoleCommand.name) {
                    style = {
                        fontSize = 12,
                        color = new Color(0.7f, 0.9f, 0.9f)
                    }
                };
                commandContainer.Add(commandLabel);
                
                Label descriptionLabel = new Label(consoleCommand.description) {
                    style = {
                        fontSize = 12,
                        color = new Color(0.5f, 0.7f, 0.7f)
                    }
                };
                commandContainer.Add(descriptionLabel);
                
                rootContainer.Add(commandContainer);
            }

            return rootContainer;
        }
    }
}