using System;

namespace Quadarax.Foundation.Common.Log.Providers
{
    public abstract class AbstractLoggingProvider : ILoggingProvider
    {
        public bool Enabled { get; set; }
        
        public abstract void Log(SeverityEnum severity, int? code, string message, Exception e = null);

        protected virtual string GetMessageString(SeverityEnum severity, int? code, string message, Exception e = null)
        {
            if (e == null)
                if (code == null)
                    return TranslateMessage(message);
                else
                    return $"[{TranslateCode(code)}] {TranslateMessage(message)}";
            else
                if (code == null)
                    return TranslateMessage(message);
                else
                    return $"[{TranslateCode(code)}] {TranslateMessage(message)} : {TranslateException(e)}";
        }

        protected virtual string TranslateCode(int? code)
        {
            return code.HasValue ? code.ToString() : string.Empty;
        }

        protected virtual string TranslateMessage(string message)
        {
            return message;
        }

        protected virtual string TranslateSeverity(SeverityEnum severity)
        {
            return severity.ToString();
        }

        protected virtual string TranslateException(Exception e)
        {
            return e == null ? string.Empty : e.ToString();
        }
    }
}
