using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using Aitex.Core.RT.Log;

namespace MECF.Framework.Common.Account.Permissions;

public class PermissionHelper
{
    #region Variables

    private readonly Dictionary<string, MenuPermissionEnum> _dictPerms = new ();

    #endregion

    #region Constructors

    public MenuPermissionEnum this[string permName] => GetPermissionByID(permName);

    #endregion

    #region Properties

    /// <summary>
    /// 返回权限字典。
    /// </summary>
    public Dictionary<string, MenuPermissionEnum> MenuPermissionDictionary  => _dictPerms;

    #endregion
    
    #region Methods

    /// <summary>
    /// 解析_Account.xml文件中的权限字串。
    /// </summary>
    /// <param name="permissionString"></param>
    public void ParsePermission(string permissionString)
    {
        try
        {
            permissionString = permissionString.Replace("\n", "").Trim(' ').Trim('\t');
            var groupIndex = permissionString.IndexOf(';');
            if (groupIndex == -1)
            {
                AddToDictionary(permissionString);
                return;
            }

            var strGroup = permissionString.Substring(0, groupIndex);
            AddToDictionary(strGroup);
            ParsePermission(permissionString.Substring(groupIndex + 1));
        }
        catch (Exception ex)
        {
            LOG.Write(ex);
        }
    }
    
    /// <summary>
    /// 创建保存于_Account.xml中的权限字串。
    /// </summary>
    /// <param name="menuPermission">底部菜单栏访问权限列表</param>
    /// <param name="recipeColumnsPermission">Recipe列访问权限列表</param>
    /// <param name="contentPermission">内容访问权限列表</param>
    /// <param name="recipeStepPermission">Recipe步骤访问权限列表</param>
    /// <returns></returns>
    public static Dictionary<string, MenuPermissionEnum> PermissionToDictionary(ObservableCollection<PermissionControlItem> menuPermission,
        ObservableCollection<PermissionControlItem> recipeColumnsPermission,
        ObservableCollection<PermissionControlItem> contentPermission,
        ObservableCollection<PermissionControlItem> recipeStepPermission)
    {
        var dictPerm = new Dictionary<string, MenuPermissionEnum>();

        if (menuPermission == null)
            return null;

        foreach (var info in menuPermission)
            dictPerm.Add(info.ID, info.Permission.EnumValue);

        foreach (var info in recipeColumnsPermission)
            dictPerm.Add(info.ID, info.Permission.EnumValue);
        
        foreach (var info in contentPermission)
            dictPerm.Add(info.ID, info.Permission.EnumValue);

        foreach (var info in recipeStepPermission)
            dictPerm.Add(info.ID, info.Permission.EnumValue);

        return dictPerm;
    }
    
    public MenuPermissionEnum GetPermissionByID(string id)
    {
        if (MenuPermissionDictionary.TryGetValue(id, out var perm))
            return perm;

        return MenuPermissionEnum.MP_NONE;
    }
    
    private void AddToDictionary(string permItem)
    {
        var index = permItem.IndexOf(',');
        if (index == -1)
            return;

        var strMenuID = permItem.Substring(0, index);
        var strMenuPermission = permItem.Substring(index + 1);

        // 优化后的MenuPermissionEnum从0开始，原_Account.xml从1开始，而该函数用于解析老的xml文件，因此需要-1。
        _dictPerms.Add(strMenuID, (MenuPermissionEnum)Convert.ToInt32(strMenuPermission) - 1);
    }
    
    #endregion

}

