using System;
using System.Text;
using Quadarax.Foundation.Core.Exceptions;

namespace Quadarax.Foundation.Core.Data
{
    public class Error : IError
    {
        #region *** Properties ***
        public string Message { get; }
        public int Code { get; }
        public ErrorLevelEnum Level { get; }
        public DateTime Timestamp { get; }

        #endregion

        #region *** Constructors ***
        public Error(string message, int code, ErrorLevelEnum level, DateTime? timestamp = null)
        {
            Message = message;
            Code = code;
            Level = level;
            Timestamp = timestamp ?? DateTime.Now;
        }
        public Error(Exception exception)
        {
            if (exception==null)
                throw new ArgumentNullException(nameof(exception));

            Message = exception.Message;
            Code = exception.GetCode();
            Timestamp = DateTime.Now;
        }
        #endregion

        #region *** Operations ***
        public override string ToString()
        {
            var sb = new StringBuilder();
            sb.Append("Error");
            if (Code != 0)
                sb.Append("[").Append(Code).Append("]");
            sb.Append(":").Append(Message);
            return sb.ToString();
        }

        #endregion
    }
}
