using AutoMapper;
using DMS.Application.Interfaces;
using DMS.Application.Interfaces.Management;
using DMS.Application.Services.Management;
using DMS.Core.Models;
using DMS.WPF.Interfaces;
using DMS.WPF.ItemViewModel;
using System.Collections.ObjectModel;
using System.Threading.Tasks;

namespace DMS.WPF.Services;

/// <summary>
/// 菜单数据服务类，负责管理菜单相关的数据和操作。
/// </summary>
public class MenuViewService : IMenuViewService
{
    private readonly IMapper _mapper;
    private readonly IWpfDataService _wpfDataService;
    private readonly IAppStorageService _appStorageService;
    private readonly IMenuManagementService _menuManagementService;



    /// <summary>
    /// MenuDataService类的构造函数。
    /// </summary>
    /// <param name="mapper">AutoMapper 实例。</param>
    /// <param name="appStorageService">数据服务中心实例。</param>
    public MenuViewService(IMapper mapper, IWpfDataService dataStorageService, IAppStorageService appStorageService, IMenuManagementService menuManagementService)
    {
        _mapper = mapper;
        _wpfDataService = dataStorageService;
        _appStorageService = appStorageService;
        _menuManagementService = menuManagementService;
    }

    public void LoadAllMenus()
    {
        _wpfDataService.Menus = _mapper.Map<ObservableCollection<MenuItem>>(_appStorageService.Menus.Values);
        BuildMenuTrees();
    }

    /// <summary>
    /// 构建菜单树。
    /// </summary>
    public void BuildMenuTrees()
    {
        // 创建一个新的临时列表来存储根菜单
        var newRootMenus = new List<MenuItem>();
        
        // 首先，确保所有菜单项的Children集合是干净的
        foreach (var menu in _wpfDataService.Menus)
        {
            // 避免循环引用，清空Children集合
            menu.Children.Clear();
        }

        // 遍历所有菜单项，构建树形结构
        foreach (var menu in _wpfDataService.Menus)
        {
            var parentMenu = _wpfDataService.Menus.FirstOrDefault(m => m.Id == menu.ParentId);
            // 检查是否有父ID，并且父ID不为0（通常0或null表示根节点）
            if (parentMenu != null && menu.ParentId != 0)
            {
                // 将当前菜单添加到父菜单的Children列表中
                if (!parentMenu.Children.Contains(menu))
                {
                    parentMenu.Children.Add(menu);
                }
            }
            else
            {
                // 如果没有父ID，则这是一个根菜单
                newRootMenus.Add(menu);
            }
        }
        
        // 在UI线程安全地更新MenuTrees集合
        _wpfDataService.MenuTrees.Clear();
        foreach (var rootMenu in newRootMenus)
        {
            _wpfDataService.MenuTrees.Add(rootMenu);
        }
    }

    /// <summary>
    /// 添加菜单项。
    /// </summary>
    public void AddMenuToView(MenuItem MenuItem)
    {
        if (MenuItem is null) return;

        var parentMenu = _wpfDataService.Menus.FirstOrDefault(m => m.Id == MenuItem.ParentId);
        if (parentMenu is not null)
        {
            // 首先添加到菜单列表
            _wpfDataService.Menus.Add(MenuItem);
            
            // 然后将当前菜单添加到父菜单的Children列表中
            if (!parentMenu.Children.Contains(MenuItem))
            {
                parentMenu.Children.Add(MenuItem);
            }
        }
    }

    /// <summary>
    /// 更新菜单项。
    /// </summary>
    public async Task UpdateMenuItem(MenuItem MenuItem)
    {
        if (MenuItem is null) return;

        var menu = _wpfDataService.Menus.FirstOrDefault(m => m.Id == MenuItem.Id);
        if (menu is not null)
        {

            var res = await _menuManagementService.UpdateMenuAsync(_mapper.Map<MenuBean>(MenuItem));
            if (res > 0)
            {
                menu.Header = MenuItem.Header;
            }

        }
    }


    /// <summary>
    /// 删除菜单项。
    /// </summary>
    public async Task DeleteMenuItem(MenuItem? MenuItem)
    {
        if (MenuItem is null) return;

        await _menuManagementService.DeleteMenuAsync(MenuItem.Id);

        // 从扁平菜单列表中移除
        _wpfDataService.Menus.Remove(MenuItem);

        // 从树形结构中移除
        if (MenuItem.ParentId.HasValue && MenuItem.ParentId.Value != 0)
        {
            // 如果有父菜单，从父菜单的Children中移除
            var parentMenu = _wpfDataService.Menus.FirstOrDefault(m => m.Id == MenuItem.ParentId.Value);
            parentMenu?.Children.Remove(MenuItem);
        }
        else
        {
            // 如果是根菜单，从MenuTrees中移除
            _wpfDataService.MenuTrees.Remove(MenuItem);
        }

        // 重新构建整个菜单树以确保一致性
        BuildMenuTrees();
    }
}