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

    using Castle.ActiveRecord;

    /// <summary>
    /// Сотрудник
    /// </summary>
    [ActiveRecord("Employee", Schema = "dbo", UseAutoImport = false), 
    JoinedTable("Position", Column = "PositionId", Fetch = FetchEnum.Unspecified), 
    DisplayName("Сотрудник"), 
    DisplayColumn("FullName")]
    public class Employee : ActiveRecordBase
    {
        #region Свойства
        /// <summary>
        /// Ключ
        /// </summary>
        [Key, PrimaryKey(PrimaryKeyType.Native, Column = "EmployeeId"), DisplayName("Id")]
        public int Id { get; set; }

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

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

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

        /// <summary>
        /// Должность
        /// </summary>
        [DisplayName("Должность"),
        BelongsTo(Column = "PositionId", NotFoundBehaviour = NotFoundBehaviour.Exception, NotNull = true, Type = typeof(Position))]
        public Position Position { get; set; }

        /// <summary>
        /// Полное имя
        /// </summary>
        [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>
        [HasMany(MapType = typeof(Request), ColumnKey = "EmployeeId", Lazy = true, ExtraLazy = true), DisplayName("Заявки")]
        public IList<Request> Requests { get; set; }
        #endregion

        #region Общие методы
        /// <summary>
        /// Возвращает все записи
        /// </summary>
        public static List<Employee> FindAll()
        {
            //using (new Castle.ActiveRecord.TransactionScope(TransactionMode.New))
            return new List<Employee>((Employee[])ActiveRecordMediator.FindAll(typeof(Employee)));
        }

        /// <summary>
        /// Возвращает запись по её Id
        /// </summary>
        /// <param name="id">Id записи в БД</param>
        public static Employee Find(int id)
        {
            return ActiveRecordMediator<Employee>.FindByPrimaryKey(id, false);
        }
        #endregion
    }
}
