using System;

namespace InABox.Core
{
    public enum LogType
    {
        Information,
        Query,
        Update,
        Error,
        /// <summary>
        /// This log is an important notice that the system administrator should not ignore.
        /// </summary>
        Important
    }

    public delegate void LogEvent(LogType type, string userid, string message, Guid transaction);

    public class Logger
    {
        public static event LogEvent OnLog;

        public static Logger Main = new Logger(Guid.Empty);

        public Guid Transaction { get; set; }

        public Logger(Guid transaction)
        {
            Transaction = transaction;
        }

        public static Logger New() => new Logger(Guid.NewGuid());

        public void Error(string message)
        {
            this.Send(LogType.Error, "", message);
        }
        public void Error(string userid, string message)
        {
            this.Send(LogType.Error, userid, message);
        }
        public void Info(string message)
        {
            this.Send(LogType.Information, "", message);
        }
        public void Info(string userid, string message)
        {
            this.Send(LogType.Information, userid, message);
        }

        public void Send(LogType logtype, string userid, string message)
        {
            OnLog?.Invoke(logtype, userid, message, Transaction);
        }

        public static void Send(LogType logtype, string userid, string message, Guid? transaction = null)
        {
            OnLog?.Invoke(logtype, userid, message, transaction ?? Guid.Empty);
        }
        public static void SendFormat(LogType logtype, string userid, string message, Guid? transaction, params object[] parameters)
        {
            OnLog?.Invoke(logtype, userid, string.Format(message, parameters), transaction ?? Guid.Empty);
        }
    }
}