using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using FoodsharingSiegen.Contracts.Enums;
using FoodsharingSiegen.Contracts.Helper;

namespace FoodsharingSiegen.Contracts.Entity
{
    /// <summary>
    ///     The user class (a. beging, 06.04.2022)
    /// </summary>
    public class User
    {
        #region Public Properties

        /// <summary>
        ///     Gets or sets the value of the created (ab)
        /// </summary>
        public DateTime Created { get; set; }

        /// <summary>
        ///     Gets or sets the value of the encrypted password (ab)
        /// </summary>
        public string EncryptedPassword { get; set; } = string.Empty;

        /// <summary>
        ///     Gets or sets the value of the force logout (ab)
        /// </summary>
        public bool ForceLogout { get; set; }

        /// <summary>
        ///     Gets or sets the value of the groups (ab)
        /// </summary>
        public string Groups { get; set; } = string.Empty;

        /// <summary>
        ///     Gets or sets the value of the groups list (ab)
        /// </summary>
        [NotMapped]
        public List<UserGroup> GroupsList
        {
            get
            {
                if (string.IsNullOrWhiteSpace(Groups)) return new List<UserGroup>();
                var stringList = Groups.Split(",");
                var enumList = stringList
                    .Where(x => !string.IsNullOrWhiteSpace(x))
                    .Select(x => Enum.TryParse<UserGroup>(x.Trim(), out var result) ? result : (UserGroup?)null)
                    .Where(x => x.HasValue)
                    .Select(x => x!.Value)
                    .ToList();
                return enumList;
            }
            set => Groups = string.Join(",", value);
        }

        /// <summary>
        ///     Gets or sets the value of the id (ab)
        /// </summary>
        [Key]
        public Guid Id { get; set; }

        /// <summary>
        ///     Gets or sets the value of the interactions (ab)
        /// </summary>
        public IList<Interaction> Interactions { get; set; } = new List<Interaction>();

        /// <summary>
        ///     Gets or sets the value of the mail (ab)
        /// </summary>
        public string Mail { get; set; } = string.Empty;

        /// <summary>
        ///     Gets or sets the value of the memo (ab)
        /// </summary>
        public string? Memo { get; set; }

        /// <summary>
        ///     Gets or sets the value of the name (ab)
        /// </summary>
        public string Name { get; set; } = string.Empty;

        /// <summary>
        ///     Gets or sets the value of the network (ab)
        /// </summary>
        public FsNetworkType Network { get; set; }

        /// <summary>
        ///     Gets the value of the network link (ab)
        /// </summary>
        [NotMapped]
        public string NetworkLink => Network.GetCustomValue();

        /// <summary>
        ///     Gets or sets the value of the password (ab)
        /// </summary>
        [NotMapped]
        public string Password
        {
            get => Cryptor.TryDecrypt(EncryptedPassword, out var password) ? password : string.Empty;
            set => EncryptedPassword = Cryptor.Encrypt(value);
        }

        /// <summary>
        ///     Gets or sets the reset token for password recovery (ab)
        /// </summary>
        public string? ResetToken { get; set; }

        /// <summary>
        ///     Gets or sets the expiry date for the reset token (ab)
        /// </summary>
        public DateTime? ResetTokenExpiry { get; set; }

        /// <summary>
        ///     Gets or sets the value of the type (ab)
        /// </summary>
        public UserType Type { get; set; }

        #endregion

        #region Public Method Clone

        /// <summary>
        ///     Clones this instance (a. beging, 11.04.2022)
        /// </summary>
        /// <returns>The user</returns>
        public User Clone() => (User)MemberwiseClone();

        #endregion
    }
}