using System;
using System.Collections.Generic;
using MECF.Framework.Common.Account.Permissions;
using MECF.Framework.Common.CommonData;

namespace MECF.Framework.Common.Account.Extends
{
    [Serializable]
    public class AppMenu : NotifiableItem
    {
        #region Variables

        private string _menuID;
        private string _menuViewModel;
        private string _resKey;
        private string _system;
        private MenuPermissionEnum _permission;
        private AppMenu _parent;
        private List<AppMenu> _subMenus;
        private bool _selected;
        private bool _isAlarm;
        private string _alarmModule;
        private string _description;

        #endregion
        
        #region Constructors

        public AppMenu()
        {
        }

        public AppMenu(string menuID, string menuViewModel, string resKey, List<AppMenu> subMenus, string description = "")
        {
            _menuID = menuID;
            _menuViewModel = menuViewModel;
            _resKey = resKey;
            _subMenus = subMenus;
            _description = description;
        }


        #endregion

        #region Properties

        public string MenuID
        {
            get => _menuID;
            set => _menuID = value;
        }

        public string ResKey
        {
            get => _resKey;
            set => _resKey = value;
        }

        public List<AppMenu> MenuItems
        {
            get => _subMenus;
            set => _subMenus = value;
        }

        public string ViewModel
        {
            get => _menuViewModel;
            set => _menuViewModel = value;
        }

        public MenuPermissionEnum Permission
        {
            get => _permission;
            set => _permission = value;
        }

        public string System
        {
            get => _system;
            set => _system = value;
        }

        /// <summary>
        /// 设置或返回当前菜单的父菜单。
        /// </summary>
        public AppMenu Parent
        {
            get => _parent;
            set => _parent = value;
        }

        /// <summary>
        /// 设置或返回当前菜单是否被选中。
        /// </summary>
        public bool Selected
        {
            get => _selected;
            set => Set(ref _selected, value);
        }

        /// <summary>
        /// 设置或返回是否在当前菜单是否有报警。
        /// </summary>
        public bool IsAlarm
        {
            get => _isAlarm;
            set => Set(ref _isAlarm, value);
        }

        /// <summary>
        /// 设置或返回报警模组。
        /// </summary>
        public string AlarmModule
        {
            get => _alarmModule;
            set => Set(ref _alarmModule, value);
        }

        public object Model { get; set; }

        /// <summary>
        /// 设置或返回被选中的子菜单。
        /// </summary>
        public AppMenu LastSelectedSubMenu { get; set; }

        /// <summary>
        /// 返回当前菜单的描述。
        /// </summary>
        public string Description
        {
            get => _description;
            set => _description = value;
        }
        
        #endregion

        #region Methods
        
        /*private void Set<T>(ref T field, T value, [CallerMemberName] string propertyName = null)
        {
            if (Equals(field, value))
                return;

            field = value;
            //InvokePropertyChanged(propertyName);
        }
        */

        public object Clone(AppMenu parent)
        {
            var appMenu = new AppMenu();
            appMenu.MenuID = MenuID;
            appMenu.ResKey = ResKey;
            appMenu.ViewModel = ViewModel;
            appMenu.Permission = Permission;
            appMenu.Selected = Selected;
            appMenu.System = System;
            appMenu.Parent = parent;
            appMenu.IsAlarm = IsAlarm;
            appMenu.AlarmModule = AlarmModule;
            appMenu.Description = Description;
            if (MenuItems != null)
            {
                appMenu.MenuItems = new List<AppMenu>();
                foreach (var menuItem in MenuItems)
                {
                    appMenu.MenuItems.Add((AppMenu)menuItem.Clone(appMenu));
                }
            }

            return appMenu;
        }

        public override string ToString()
        {
            return $"Name = {MenuID}, Sub Menu = {MenuItems?.Count ?? 0}";
        }

        #endregion
    }
}
