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

namespace PSCHelpdesk.Services;

public class SettingsService: ReactiveObject, ISettingsService
    {
        private List<Item> _settings = new List<Item>();
        
        public event EventHandler MenuChanged;

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

        public SettingsService()
        {
            this.Settings.Clear();
            this.Settings.Clear();
            
            var globalSettings = new Item()
            {
                Header = "Global",
                CommandParameter = new SettingsGlobalViewModel(),
            };
            
            this.Settings.Add(globalSettings);
        }

        public void AddSetting(Item item)
        {
            this.Settings.Add(item);
            OnMenuChanged(EventArgs.Empty);
        }

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

        public Item GetMenuOptionByLabel(string coreSettingsActiveMenuItem)
        {
            foreach (var setting in this._settings)
            {
                if (setting.Header == coreSettingsActiveMenuItem)
                {
                    return setting;
                }
            }

            return null;
        }
    }