using System;
using System.Collections.Generic;
using CommunityToolkit.Mvvm.DependencyInjection;
using PSCHelpdesk.Shared.Menu;
using PSCHelpdesk.Shared.Service;
using PSCHelpdesk.ViewModels;
using ReactiveUI;

namespace PSCHelpdesk.Services;

public class MenuService: ReactiveObject, IMenuService
    {
        private List<Item> menuItems = new List<Item>();
        private List<Item> menuOptionItems = new List<Item>();
        
        public event EventHandler MenuChanged;

        public List<Item> MenuItems
        {
            get => this.menuItems;
            private set => this.RaiseAndSetIfChanged(ref this.menuItems, value);
        }

        /// <summary>
        /// Gets the list of options items shown in the hamburger menu (at the bottom).
        /// </summary>
        public List<Item> MenuOptionItems
        {
            get => this.menuOptionItems;
            private set => this.RaiseAndSetIfChanged(ref this.menuOptionItems, value);
        }

        /// <summary>
        /// Gets or sets the currently selected Hamburger Menu Tab.
        /// </summary>
        
        public void CreateMenu()
        {
            this.MenuItems.Clear();
            this.MenuOptionItems.Clear();
            
            var pluginTab = new Item()
            {
                Header = "Plugins",
                CommandParameter = Ioc.Default.GetService<PluginListViewModel>(),
            };
            this.MenuItems.Add(pluginTab);
            OnMenuChanged(EventArgs.Empty);
        }

        public void AddMenuItem(Item item)
        {
            this.MenuItems.Add(item);
            OnMenuChanged(EventArgs.Empty);
        }

        protected virtual void OnMenuChanged(EventArgs e)
        {
            MenuChanged?.Invoke(this, e);
        }

        public Item GetMenuOptionByLabel(string coreSettingsActiveMenuItem)
        {
            foreach (var menuItem in this.menuItems)
            {
                if (menuItem.Header == coreSettingsActiveMenuItem)
                {
                    return menuItem;
                }
            }
            foreach (var menuItem in this.menuOptionItems)
            {
                if (menuItem.Header == coreSettingsActiveMenuItem)
                {
                    return menuItem;
                }
            }

            return null;
        }
    }