using GreenTree.Nachtragsmanagement.Core.Domain.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace GreenTree.Nachtragsmanagement.Web.Models.Misc
{
    public class LogDataModel
    {
        public int Id { get; set; }
        public int LogLevelId { get; set; }
        public string LogLevelDescription { get; set; }
        public string ShortMessage { get; set; }
        public string FullMessage { get; set; }
        public string IpAddress { get; set; }
        public int? UserId { get; set; }
        public string UserDescription { get; set; }
        public int? EntityId { get; set; }
        public string EntityType { get; set; }
        public string PageUrl { get; set; }
        public string ReferrerUrl { get; set; }
        public DateTime CreatedOnUtc { get; set; }
        public DateTime CreatedOnLocal
        {
            get
            {
                return CreatedOnUtc.ToLocalTime();
            }
        }

        public LogDataModel()
        {

        }

        public static LogDataModel FromLog(Core.Domain.Logging.Log logEntity, bool newWhenIsNull)
        {
            if (logEntity == null && newWhenIsNull)
                return new LogDataModel
                {
                    Id = -1,
                };

            if (logEntity == null && !newWhenIsNull)
                throw new ArgumentNullException("logEntity", "Cannot create LogDataModel from NULL log entity.");

            var logDataModel = new LogDataModel
            {
                Id = logEntity.Id,
                LogLevelId = logEntity.LogLevelId,
                LogLevelDescription = Enum.GetName(typeof(LogLevel), (LogLevel)logEntity.LogLevelId),
                ShortMessage = logEntity.ShortMessage,
                FullMessage = logEntity.FullMessage,
                IpAddress = logEntity.IpAddress,
                UserId = logEntity.UserId,
                UserDescription = logEntity.User == null
                    ? String.Empty
                    : logEntity.User.Lastname,
                EntityId = logEntity.EntityId,
                EntityType = logEntity.EntityType,
                PageUrl = logEntity.PageUrl,
                ReferrerUrl = logEntity.ReferrerUrl,
                CreatedOnUtc = logEntity.CreatedOnUtc
            };

            return logDataModel;
        }
    }
}