using System;

namespace Lomont.Utils
    {

    /// <summary>
    /// An interface to output messages from internals
    /// </summary>
    public interface IMessager
        {
        void OnMessage(string format, params object[] args);
        void OnError(string format, params object[] args);
        void OnWarning(string format, params object[] args);
        void OnInfo(string format, params object[] args);
        }

    /// <summary>
    /// Represent a messaging protocol. Tracks some types of message.
    /// TODO - base on trace listeners?
    /// </summary>
    public class Messager : IMessager
        {

        /// <summary>
        /// Listen to the messages
        /// </summary>
        public event EventHandler<Messager.MessageArgs> MessageEvent;

        /// <summary>
        /// Message event args
        /// </summary>
        public class MessageArgs : EventArgs
            {
            public string Message;
            }

        /// <summary>
        /// Trigger the message
        /// </summary>
        /// <param name="disasm"></param>
        protected void OnMessage(string message)
            {
            var e = MessageEvent;
            if (e != null)
                e(this, new MessageArgs() { Message = message });
            }
        /// <summary>
        /// Send a message
        /// </summary>
        /// <param name="format"></param>
        /// <param name="args"></param>
        public void OnMessage(string format, params object[] args)
            {
            OnMessage(String.Format(format, args));
            }
        /// <summary>
        /// Send a warning message
        /// </summary>
        /// <param name="format"></param>
        /// <param name="args"></param>
        public void OnWarning(string format, params object[] args)
            {
            ++WarningCount;
            OnMessage(String.Format("WARNING: " + format, args));
            }
        /// <summary>
        /// Send an error message
        /// </summary>
        /// <param name="format"></param>
        /// <param name="args"></param>
        public void OnError(string format, params object[] args)
            {
            ++ErrorCount;
            OnMessage(String.Format("ERROR: " + format, args));
            }
        protected int ErrorCount { get; set; }
        protected int WarningCount { get; set; }
        protected int InfoCount { get; set; }

        /// <summary>
        /// Send an informational message
        /// </summary>
        /// <param name="format"></param>
        /// <param name="args"></param>
        public void OnInfo(string format, params object[] args)
            {
            ++InfoCount;
            OnMessage(String.Format("INFO: " + format, args));
            }
        }
    }
