using RebootKit.Engine.Foundation;
using RebootKit.Engine.UI;
using UnityEngine;
using UnityEngine.UIElements;

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

        public VisualElement Build() {
            ScrollView scrollView = new ScrollView {
                style = {
                    flexGrow = 1
                }
            };

            ConfigVarsContainer.Init();

            foreach (ConfigVar cvar in ConfigVarsContainer.All()) {
                VisualElement varContainer = new VisualElement {
                    style = {
                        marginBottom = 8,
                        paddingBottom = 8,
                        borderBottomWidth = 1,
                        borderBottomColor = new Color(0.3f, 0.3f, 0.3f),
                    }
                };

                VisualElement valueContainer = new VisualElement {
                    style = {
                        flexDirection = FlexDirection.Row
                    }
                };
                varContainer.Add(valueContainer);

                Label nameLabel = new Label(cvar.name) {
                    style = {
                        color = new Color(0.7f, 0.9f, 0.9f),
                        unityFontStyleAndWeight = FontStyle.Bold
                    }
                };
                valueContainer.Add(nameLabel);

                Label valueLabel = new Label(cvar.ToString()) {
                    style = {
                        color = RTheme.s_FirstColor
                    }
                };
                valueContainer.Add(valueLabel);

                if (cvar.flags.HasFlag(CVarFlags.User)) {
                    valueContainer.Add(CreateFlagLabel("User", new Color(0.36f, 0.41f, 0.42f)));
                }

                if (cvar.flags.HasFlag(CVarFlags.Client)) {
                    valueContainer.Add(CreateFlagLabel("Client", new Color(0.81f, 0.29f, 0.15f)));
                }
                
                if (cvar.flags.HasFlag(CVarFlags.Server)) {
                    valueContainer.Add(CreateFlagLabel("Server", new Color(0.18f, 0.64f, 0.18f)));
                }
                
                if (cvar.flags.HasFlag(CVarFlags.ReadOnly)) {
                    valueContainer.Add(CreateFlagLabel("ReadOnly", new Color(0.13f, 0.07f, 0.47f)));
                }

                Label descLabel = new Label(cvar.description) {
                    style = {
                        fontSize = 10,
                        color = new Color(0.7f, 0.7f, 0.7f)
                    }
                };
                varContainer.Add(descLabel);

                scrollView.Add(varContainer);
            }

            return scrollView;
        }

        VisualElement CreateFlagLabel(string text, Color color) {
            Label label = new Label(text) {
                style = {
                    fontSize = 12,
                    color = new Color(0.7f, 0.7f, 0.7f),
                    backgroundColor = color,
                    paddingLeft = 4,
                    paddingRight = 4,
                    paddingTop = 4,
                    paddingBottom = 4,
                    marginLeft = 4,
                    marginRight = 4,
                    borderTopLeftRadius = 8,
                    borderTopRightRadius = 8,
                    borderBottomLeftRadius = 8,
                    borderBottomRightRadius = 8,
                    unityFontStyleAndWeight = FontStyle.Bold,
                }
            };
            return label;
        }
    }
}