namespace ServiceCenter.Data.BLT
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using DataAnnotations = System.ComponentModel.DataAnnotations;

    using BLToolkit.Common;
    using BLToolkit.DataAccess;
    using BLToolkit.Mapping;

    /// <summary>
    /// Сотрудник
    /// </summary>
    [TableName("Employee")]
    public class Employee : EntityBase
    {
        /// <summary>
        /// Пустой конструктор для BLToolkit
        /// </summary>
        public Employee()
        {

        }

        public Employee(Employee employee)
        {
            this.Id = employee.Id;
            this.SurName = employee.SurName;
            this.Name = employee.Name;
            this.MidName = employee.MidName;
            this.PositionId = employee.PositionId;
            this.Position = employee.Position;
        }

        [MapField("EmployeeId"), PrimaryKey, Identity, NonUpdatable, DisplayName("Id")]
        public int Id { get; set; }

        /// <summary>
        /// Имя
        /// </summary>
        [NotNull, DisplayName("Имя"), DataAnnotations.Required(ErrorMessage = "Имя не может быть пустым"), DataAnnotations.DataType(DataAnnotations.DataType.Text), DataAnnotations.StringLength(50, ErrorMessage = "Максимум 50 символов")]
        public string Name { get; set; }

        /// <summary>
        /// Фамилия
        /// </summary>
        [Nullable, DisplayName("Фамилия"), DataAnnotations.DataType(DataAnnotations.DataType.Text), DataAnnotations.StringLength(50, ErrorMessage = "Максимум 50 символов")]
        public string SurName { get; set; }

        /// <summary>
        /// Отчество
        /// </summary>
        [Nullable, DisplayName("Отчество"), DataAnnotations.DataType(DataAnnotations.DataType.Text), DataAnnotations.StringLength(50, ErrorMessage = "Максимум 50 символов")]
        public string MidName { get; set; }

        /// <summary>
        /// Должность
        /// </summary>
        [MapField("PositionId"), NotNull, DisplayName("Должность")]
        public int PositionId { get; set; }

        /// <summary>
        /// Должность
        /// </summary>
        [Association(CanBeNull = false, ThisKey = "PositionId", OtherKey = "Id"), DisplayName("Должность")]
        public Position Position { get; set; }

        /// <summary>
        /// Полное имя
        /// </summary>
        [MapIgnore, DisplayName("Ф.И.О.")]
        public string FullName
        {
            get
            {
                string fullName = this.Name;

                if (!string.IsNullOrEmpty(this.SurName) && !string.IsNullOrEmpty(this.MidName))
                    fullName = string.Format("{0} {1}.{2}.", this.SurName, this.Name[0], this.MidName[0]);
                else if (!string.IsNullOrEmpty(this.SurName))
                    fullName = string.Format("{0} {1}", this.SurName, this.Name);

                return fullName;
            }
        }

        /// <summary>
        /// Заявки
        /// </summary>
        [Association(ThisKey = "Id", OtherKey = "EmployeeId"), DisplayName("Заявки")]
        public List<Request> Requests { get; set; }
    }
}