using System.Collections.ObjectModel;
using System.Linq;
using Caliburn.Micro.Core;
using MECF.Framework.Common.Account;
using MECF.Framework.Common.Account.Extends;
using MECF.Framework.Common.Account.Permissions;

namespace MECF.Framework.UI.Client.CenterViews.Configs.Roles
{
    public class RoleItem : PropertyChangedBase
    {
        #region Constructors

        public RoleItem(Role role)
        {
            Role = role;
            DisplayRoleName = RoleName;
            DisplayAutoLogoutTime = AutoLogoutTime;
            DisplayIsAutoLogout = IsAutoLogout;
            DisplayDescription = Description;
        }

        public RoleItem(string roleId)
        {
            Role = new Role(roleId, string.Empty, false, 0, null, false, string.Empty);
        }
        

        #endregion

        #region Properties

        public Role Role { get; }

        public string RoleId
        {
            get => Role.RoleId;
            set => Role.RoleId = value;
        }

        public string RoleName
        {
            get => Role.RoleName;
            set => Role.RoleName = value;
        }

        public int AutoLogoutTime
        {
            get => Role.LogoutTime;
            set => Role.LogoutTime = value;
        }

        public string Description
        {
            get => Role.Description;
            set => Role.Description = value;
        }
        
        public bool IsAutoLogout
        {
            get => Role.IsAutoLogout;
            set => Role.IsAutoLogout = value;
        }

        public bool IsBuildIn
        {
            get => Role.IsBuildIn;
            set => Role.IsBuildIn = value;
        }

        private string _displayRoleName;

        public string DisplayRoleName
        {
            get => _displayRoleName;
            set
            {
                _displayRoleName = value;
                NotifyOfPropertyChange();
            }
        }

        public int DisplayAutoLogoutTime
        {
            get;
            set;
        }

        public bool DisplayIsAutoLogout
        {
            get;
            set;
        }


        public string DisplayDescription
        {
            get;
            set;
        }


        private bool _isSelected;
        public bool IsSelected
        {
            get => _isSelected;
            set { _isSelected = value; NotifyOfPropertyChange(); }
        }

        private bool _roleNameTextSaved = true;
        public bool RoleNameTextSaved
        {
            get => _roleNameTextSaved;
            set
            {
                if (_roleNameTextSaved != value)
                {
                    _roleNameTextSaved = value;
                    NotifyOfPropertyChange();
                }
            }
        }

        private bool _timeTextSaved = true;
        public bool TimeTextSaved
        {
            get => _timeTextSaved;
            set
            {
                if (_timeTextSaved != value)
                {
                    _timeTextSaved = value;
                    NotifyOfPropertyChange();
                }
            }
        }

        private bool _descriptionTextSaved = true;
        public bool DescriptionTextSaved
        {
            get => _descriptionTextSaved;
            set
            {
                if (_descriptionTextSaved != value)
                {
                    _descriptionTextSaved = value;
                    NotifyOfPropertyChange();
                }
            }
        }

        public ObservableCollection<PermissionControlItem> MenuPermCollection { get; } = new();

        public ObservableCollection<PermissionControlItem> RecipePermCollection { get; } = new();

        public ObservableCollection<PermissionControlItem> RecipeStepPermCollection { get; } = new();

        public ObservableCollection<PermissionControlItem> ContentPermCollection { get; } = new();

        #endregion

        #region Methods

        public void AddMenuInfo(PermissionControlItem pInfo)
        {
            if (null == pInfo)
                return;

            MenuPermCollection.Add(pInfo);
        }

        public void AddRecipeInfo(PermissionControlItem pInfo)
        {
            if (null == pInfo)
                return;

            RecipePermCollection.Add(pInfo);
        }

        public void AddStepInfo(PermissionControlItem pInfo)
        {
            if (null == pInfo)
                return;

            RecipeStepPermCollection.Add(pInfo);
        }

        public void AddContentInfo(PermissionControlItem pInfo)
        {
            if (null == pInfo)
                return;

            ContentPermCollection.Add(pInfo);
        }

        public void UpdateRolePermissionControlItems()
        {
            foreach (var perm in MenuPermCollection)
            {
                Role.PermissionControlItems[perm.Name] = perm.Permission.EnumPermission;
            }

            foreach (var perm in RecipePermCollection)
            {
                Role.PermissionControlItems[perm.Name] = perm.Permission.EnumPermission;
            }

            foreach (var perm in RecipeStepPermCollection)
            {
                Role.PermissionControlItems[perm.Name] = perm.Permission.EnumPermission;
            }

            foreach (var perm in ContentPermCollection)
            {
                Role.PermissionControlItems[perm.Name] = perm.Permission.EnumPermission;
            }
        }

        public bool IsRoleChanged()
        {
            if (DisplayRoleName != Role.RoleName)
                return true;

            if (DisplayAutoLogoutTime != Role.LogoutTime)
                return true;

            if (DisplayIsAutoLogout != Role.IsAutoLogout)
                return true;

            if (DisplayDescription != Role.Description)
                return true;

            if (MenuPermCollection.Any(item => item.IsChanged))
            {
                return true;
            }

            if (RecipePermCollection.Any(item => item.IsChanged))
            {
                return true;
            }

            if (RecipeStepPermCollection.Any(item => item.IsChanged))
            {
                return true;
            }
            
            if (ContentPermCollection.Any(item => item.IsChanged))
            {
                return true;
            }
            
            return false;
        }
        
        #endregion
    }
}
