using GreenTree.Strohrmann.ERP.Web.Models.Shared;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;

namespace GreenTree.Strohrmann.ERP.Web.Models.Rights.User
{
    public class UserModel
    {
        #region Properties

        /// <summary>
        /// User Id
        /// </summary>
        [Display(Name = "ID")]
        public int? Id { get; set; }

        /// <summary>
        /// User accountname
        /// </summary>
        [Display(Name = "Kontoname")]
        public string Accountname { get; set; }

        /// <summary>
        /// User password (for creation)
        /// </summary>
        [Display(Name = "Passwort")]
        public string Password { get; set; }

        /// <summary>
        /// User forename
        /// </summary>
        [Display(Name = "Vorname")]
        public string Forename { get; set; }

        /// <summary>
        /// User lastname
        /// </summary>
        [Display(Name = "Nachname")]
        public string Lastname { get; set; }

        /// <summary>
        /// User mail address
        /// </summary>
        [Display(Name = "Mail-Adresse")]
        public string MailAddress { get; set; }

        /// <summary>
        /// User birthdate
        /// </summary>
        [Display(Name = "Geburtsdatum")]
        [DisplayFormat(ApplyFormatInEditMode = false, DataFormatString = "{0:dd.MM.yyyy}")]
        public DateTime Birthdate { get; set; }

        /// <summary>
        /// User activation status
        /// </summary>
        [Display(Name = "Aktiviert")]
        public bool Activated { get; set; }

        /// <summary>
        /// User policies
        /// </summary>
        [Display(Name = "Rechte")]
        public string[] Policies { get; set; }

        #endregion

        #region Ctor

        /// <summary>
        /// Initializes a new instance of the UserModel class
        /// </summary>
        public UserModel() { }

        /// <summary>
        /// Initializes a new instance of the UserModel class
        /// </summary>
        /// <param name="user">Base user entity.</param>
        public UserModel(Core.Domain.Rights.User user)
        {
            if (user == null) return;

            Id = user.Id;
            Accountname = user.Accountname;
            Forename = user.Forename;
            Lastname = user.Lastname;
            MailAddress = user.MailAddress;
            Birthdate = user.Birthdate.HasValue
                ? user.Birthdate.Value
                : DateTime.MinValue;
            Activated = user.Activated.Value;
            Policies = user.Policies
                .Select(up => up.PolicyName)
                .ToArray();
        }

        #endregion
    }
}
