using System.Collections.Generic;
using UnityEngine.UIElements;
using WorldBuilder.Plugins;
using WorldBuilder.UI;
using UnityEngine;
using WorldBuilder.Plugins.UI;

namespace WorldBuilderDemoPlugins
{
    public class MenuEntryDemo : PluginBase, ILocalization
    {
        // Localization keys
        private const string SETTINGS_MENU_ENTRY_BUTTON_LABEL_KEY = "SETTING_MENU_ENTRY_BUTTON_LABEL";
        private const string SETTINGS_MENU_PAGE_LABEL_KEY = "SETTINGS_MENU_PAGE_LABEL";
        private const string OPEN_POPUP_BUTTON_LABEL_KEY = "SETTINGS_MENU_PAGE_OPEN_POPUP_BUTTON_LABEL";
        private const string POPUP_TITLE_KEY = "POPUP_TITLE";
        private const string POPUP_TEXT_KEY = "POPUP_TEXT";
        private const string DROPDOWN_LABEL_KEY = "DROPDOWN_LABEL";
        private const string DROPDOWN_ENTRY_SET_DEFAULT_CURSOR_KEY = "DROPDOWN_ENTRY_SET_DEFAULT_CURSOR";
        private const string DROPDOWN_ENTRY_SET_APPLY_CURSOR_KEY = "DROPDOWN_ENTRY_SET_APPLY_CURSOR";
        private const string COLUMN_NAME_KEY = "TABLE_COLUMN_NAME";
        private const string ENTRY_KEY = "TABLE_ENTRY";

        // Localization tooltip keys
        private const string SETTINGS_MENU_ENTRY_TOOLTIP = "#SETTINGS_MENU_ENTRY";

        public override PluginFeatureFlags Features => PluginFeatureFlags.None;

        public string LocalizationFolderName => "Localization Demo Menu Entry";

        public override void CreateSetupProcess()
        {
            base.CreateSetupProcess();

            ReferenceProvider.SettingsMenu.OnMenuBuild += RegisterMenuEntry;
        }

        private void RegisterMenuEntry(IUIMenuPage menuPage)
        {
            menuPage.RegisterMenuItemLocalized("Demo Menu Entry", SETTINGS_MENU_ENTRY_BUTTON_LABEL_KEY,
                SETTINGS_MENU_ENTRY_TOOLTIP, BuildPage);
        }

        private void BuildPage(bool open, VisualElement pageRoot)
        {
            if (!open)
                return;

            pageRoot.Clear();

            IUIElementFactory elementFactory = ReferenceProvider.ElementFactory;

            elementFactory.CreateLabelLocalized(pageRoot, SETTINGS_MENU_PAGE_LABEL_KEY,
                LabelStyles.SectionTitle);

            VisualElement horizontalContainer = elementFactory.CreateContainer(pageRoot, ContainerStyles.Horizontal);

            CreateDropdown(elementFactory, horizontalContainer);

            elementFactory.CreateSpaceHorizontal(horizontalContainer, 80f);

            CreateButton(elementFactory, horizontalContainer);

            elementFactory.CreateSpaceVertical(pageRoot, 20f);

            CreateTable(elementFactory, pageRoot);

            elementFactory.CreateHelpButton(pageRoot, "https://www.portalsunited.com/",
                HelpButtonStyles.Subpage, useAsSuburl: false);
        }

        private void CreateDropdown(IUIElementFactory elementFactory, VisualElement parent)
        {
            List<string> entries = new List<string>()
            {
                ReferenceProvider.LocalizationSystem.Localize(DROPDOWN_ENTRY_SET_DEFAULT_CURSOR_KEY),
                ReferenceProvider.LocalizationSystem.Localize(DROPDOWN_ENTRY_SET_APPLY_CURSOR_KEY)
            };

            DropdownField dropdownField = elementFactory.CreateDropdownFieldLocalized(parent, DROPDOWN_LABEL_KEY, entries, 0);

            dropdownField.RegisterValueChangedCallback(DropdownChangeEventCallback);
        }
        private void DropdownChangeEventCallback(ChangeEvent<string> dropdownLabel)
        {
            if (dropdownLabel.newValue == ReferenceProvider.LocalizationSystem.Localize(DROPDOWN_ENTRY_SET_DEFAULT_CURSOR_KEY))
            {
                ReferenceProvider.CursorController.SetCursor(ICursorController.Modes.Default);
            }
            else if (dropdownLabel.newValue == ReferenceProvider.LocalizationSystem.Localize(DROPDOWN_ENTRY_SET_APPLY_CURSOR_KEY))
            {
                ReferenceProvider.CursorController.SetCursor(ICursorController.Modes.Apply);
            }
        }

        private void CreateButton(IUIElementFactory elementFactory, VisualElement parent)
        {
            Button button = elementFactory.CreateButtonLocalized(parent, OPEN_POPUP_BUTTON_LABEL_KEY, ButtonStyles.Primary);

            button.clicked += OpenPopup;
        }

        private void OpenPopup()
        {
            ReferenceProvider.PopupSystem.ShowOkPopupLocalized(POPUP_TITLE_KEY, POPUP_TEXT_KEY);
        }

        private void OpenErrorPopup()
        {
            ReferenceProvider.PopupSystem.ShowErrorPopup("Something could have gone wrong here. But as it is just a demo, everything is fine!");
        }

        private void CreateTable(IUIElementFactory elementFactory, VisualElement parent)
        {
            string[] labels = new string[]
            {
                "Column Name", "Column Name", ReferenceProvider.LocalizationSystem.Localize(COLUMN_NAME_KEY)
            };

            int[] widths = new int[]
            {
                400, 400, 0
            };

            IUITable table = elementFactory.CreateTable(parent, labels, widths, TableStyles.HiddenScrollbars);

            string[] entries = new string[]
            {
                "Entry", "Entry", ReferenceProvider.LocalizationSystem.Localize(ENTRY_KEY)
            };

            Button secondaryButton = elementFactory.CreateButton(null, "Secondary Button Style", ButtonStyles.Secondary);
            Button warningButton = elementFactory.CreateButton(null, "Warning Button Style", ButtonStyles.Warning);
            Button errorButton = elementFactory.CreateButton(null, "Error Button Style", ButtonStyles.Error);

            VisualElement[] buttons = new VisualElement[]
            {
                secondaryButton,
                warningButton,
                errorButton
            };

            secondaryButton.clicked += OpenPopup;
            warningButton.clicked += OpenPopup;
            errorButton.clicked += OpenErrorPopup;

            table.Add(entries);
            table.Add(entries);
            table.Add(entries);
            table.Add(buttons);
            table.Add(entries);
        }

    }
}
