using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Reflection;
using UnivateProperties_API.Containers;

namespace UnivateProperties_API.Model
{
    public class BaseEntity : IDisplay
    {
        #region Properties
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        [Key]
        public int Id { get; set; }
        public DateTime Created { get; set; } = DateTime.Now;
        public DateTime Modified { get; set; } = DateTime.Now;
        public string ModifiedBy { get; set; }
        [NotMapped]
        public ValidateEntity Valid { get; set; }
        public bool IsDeleted { get; set; } = false;
        [NotMapped]
        public virtual string Display { get { return ToString(); } }
        #endregion

        #region Methods
        public virtual void UpdateModified(string modifiedBy)
        {
            Modified = DateTime.Now;
            if (string.IsNullOrEmpty(modifiedBy))
            {
                ModifiedBy = modifiedBy;
            }
        }

        public void Validate()
        {
            Valid = ValidateObject();
        }

        public virtual ValidateEntity ValidateObject()
        {
            ValidateEntity v = new ValidateEntity();
            return v;
        }

        public object this[string propertyName]
        {
            get { return GetType().GetProperty(propertyName).GetValue(this, null); }
            set { GetType().GetProperty(propertyName).SetValue(this, value, null); }
        }

        public string[] GetAllProperties()
        {
            if (this == null) return new string[] { };
            Type t = GetType();
            PropertyInfo[] props = t.GetProperties();
            List<string> propNames = new List<string>();
            foreach (PropertyInfo prp in props)
            {
                propNames.Add(prp.Name);
            }
            return propNames.ToArray();
        }
        #endregion Methods
    }
}