using System;
using static DH.Commons.Enums.EnumHelper;


namespace DH.Commons.Helper
{
    public enum ExceptionLevel
    {
        Info = 0,
        Warning = 1,
        Fatal = 2,
    }

    //public delegate void OnProcessExceptionRaisedDelegate(DateTime dt, ProcessException ex);
    //[Serializable]
    public class ProcessException : Exception
    {
        public ExceptionLevel Level { get; set; } = ExceptionLevel.Warning;

        public ProcessException()
        {
        }

        public ProcessException(Exception ex, ExceptionLevel lvl = ExceptionLevel.Warning) : base(ex.Message, ex)
        {
            Level = lvl;
            ExceptionNotice();
        }

        public ProcessException(string error, Exception ex = null, ExceptionLevel lvl = ExceptionLevel.Warning) : base(error, ex)
        {
            Level = lvl;
            ExceptionNotice();
        }

        public void ExceptionNotice()
        {
            //OnProcessExceptionRaised?.Invoke(DateTime.Now, this);
        }
    }

    public static class ExceptionHelper
    {
        public static LogLevel LogLevel = LogLevel.Information;
        public static string GetExceptionMessage(this Exception ex)
        {
            string msg = "异常信息:" + ex.Message;
            if (ex.InnerException != null)
            {
                msg += ";\t内部异常信息:" + ex.InnerException.GetExceptionMessage();
            }

            if (LogLevel <= LogLevel.Assist)
            {
                msg += (";\r\n\t\tStackTrace:" + ex.StackTrace);
            }
            return msg;
        }
    }

    public class AuthorityException : ProcessException
    {
    }
}