using System;
using System.Collections.Generic;
using System.Linq;
using System.Reactive;
using System.Windows.Input;
using Avalonia.Controls;
using CommunityToolkit.Mvvm.DependencyInjection;
using CommunityToolkit.Mvvm.Input;
using Nucs.JsonSettings;
using PSCHelpdesk.Services;
using PSCHelpdesk.Shared.Menu;
using PSCHelpdesk.Shared.Service;
using PSCHelpdesk.Shared.ViewModels;
using ReactiveUI;

namespace PSCHelpdesk.ViewModels;

public class MainWindowViewModel : ViewModelBase
{
    private MenuService _menuService;
    public ReactiveCommand<string, Unit> ClickMenu { get; }
    public MenuService MenuService
    {
        get => this._menuService;
        private set => SetAndRaisePropertyChanged(ref this._menuService, value);
    }

    private object _contentDisplay;
    
    private Item _selectedItem;
    public Item SelectedItem
    {
        get => _selectedItem;
        set => SetAndRaisePropertyChanged(ref _selectedItem, value);
    }
    
    public object ContentDisplay
    {
        get => _contentDisplay;
        set => SetAndRaisePropertyChanged(ref _contentDisplay, value);
    }
    
    public MainWindowViewModel()
    {
        ClickMenu = ReactiveCommand.Create<string>(SelectMenu);
        this.MenuItems = new List<Item>();
        Startup.RegisterServices();
        this.MenuService = (MenuService)Ioc.Default.GetService(typeof(IMenuService));
        this.MenuService.MenuChanged += delegate(object? sender, EventArgs args)
        {
            foreach (var menuServiceMenuItem in MenuService.MenuItems)
            {
                menuServiceMenuItem.Command = this.ClickMenu;
                this.MenuItems.Add(menuServiceMenuItem);
            }
            
            foreach (var menuServiceMenuItem in MenuService.MenuOptionItems)
            {
                menuServiceMenuItem.Command = this.ClickMenu;
                this.MenuItems.Add(menuServiceMenuItem);
            }
        };
            
        this.InitializeClient();
        
        this.SelectedItem = MenuService.MenuItems.Last();
        
    }
    
    private List<Item> _menuItems;
    public List<Item> MenuItems
    {
        get => _menuItems;
        set => SetAndRaisePropertyChanged(ref _menuItems, value);
    }

    
    private void InitializeClient()
    {
        this.MenuService.CreateMenu();
    }
    
    public object SelectedContent
    {
        get
        {
            if (SelectedItem.ViewModel != null)
            {
                var vm = SelectedItem.ViewModel.CreateInstance();
                return vm;
            }
            return this.SelectedItem?.CommandParameter;
        }
    }
    
    public void SelectMenu(string parameter)
    {
        //this.SelectedItem = parameter;
        var obj = this.MenuService.MenuItems.FirstOrDefault(menuItem => menuItem.Header == parameter.ToString(), null);
        if (obj != null)
        {
            this.SelectedItem = obj;
            if (obj.ViewModel != null)
            {
                this.ContentDisplay = obj.ViewModel.CreateInstance();
            }
            else
            {
                this.ContentDisplay = obj.CommandParameter;    
            }
        }
    }
}