using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using GreenTree.Nachtragsmanagement.Core.Domain.Config;
using GreenTree.Nachtragsmanagement.Core.Domain.Misc;
using Newtonsoft.Json;

namespace GreenTree.Nachtragsmanagement.Core.Domain.User
{
    public class User : BaseEntity
    {
        #region Fields

        /// <summary>
        /// Current role
        /// </summary>
        private Role _currentRole;

        /// <summary>
        /// Rolelist
        /// </summary>
        private ICollection<Role> _roles;

        /// <summary>
        /// Notificationlist
        /// </summary>
        private ICollection<MailNotification> _mailNotifications;

        /// <summary>
        /// Configitemlist
        /// </summary>
        private ICollection<UserConfigItem> _userConfigItems;

        #endregion

        /// <summary>
        /// CustomNumber for identification
        /// </summary>
        public string CustomNumber { get; set; }

        /// <summary>
        /// Forename
        /// </summary>
        public string Forename { get; set; }

        /// <summary>
        /// Lastname
        /// </summary>
        public string Lastname { get; set; }

        /// <summary>
        /// Mail-Address
        /// </summary>
        public string MailAddress { get; set; }

        /// <summary>
        /// Password
        /// </summary>
        public string Password { get; set; }

        /// <summary>
        /// The role the current user is using
        /// </summary>
        public Role CurrentRole
        {
            get
            {
                return _currentRole;
            }
            set
            {
                _currentRole = value;
            }
        }

        /// <summary>
        /// Determines if there are changes to the user settings
        /// </summary>
        public bool? IsChanged { get; set; }


        /// <summary>
        /// Roles the user have
        /// </summary>
        [JsonIgnore]
        public virtual ICollection<Role> Roles
        {
            get { return _roles ?? (_roles = new List<Role>()); }
            protected set { _roles = value; }
        }

        /// <summary>
        /// MailNotification related to the user
        /// </summary>
        [JsonIgnore]
        public virtual ICollection<MailNotification> MailNotifications
        {
            get { return _mailNotifications ?? ( _mailNotifications = new List<MailNotification>()); }
            protected set { _mailNotifications = value; }
        }

        /// <summary>
        /// UserConfigItems related to the user
        /// </summary>
        [JsonIgnore]
        public virtual ICollection<UserConfigItem> UserConfigItems
        {
            get { return _userConfigItems ?? ( _userConfigItems = new List<UserConfigItem>()); }
            protected set { _userConfigItems = value; }
        }

        #region Helper

        /// <summary>
        /// Adds missing roles and removes not selected roles
        /// </summary>
        /// <param name="roles">User roles.</param>
        public void SetRoles(ICollection<Role> roles)
        {
            Roles = roles;
        }

        /// <summary>
        /// Determines if the user is member of role which owns a specific function
        /// </summary>
        /// <param name="functionName">The name of the function.</param>
        public bool HasFunction(string functionName)
        {
            if (String.IsNullOrEmpty(functionName))
                return false;

            if (CurrentRole == null) return false;

            return
                CurrentRole.Functions
                    .Any(f => f.Name == functionName);
        }

        #endregion
    }
}
