using System;
using System.Collections.Generic;
namespace Wayne.Lib.Log
{
    /// <summary>
    /// A log writer that writes the log to an external log writer class.
    /// </summary>
    internal class ExternalLogWriterWrapper : LogWriter
    {
        #region Fields

        private bool disposed;
        private string externalLogType;
        private string externalLogName;
        private LogTextWritingParameters writingParameters;
        private Dictionary<string, string> parameters;

        #endregion

        #region Construction

        public ExternalLogWriterWrapper(string logName, LogConfigExternalLogWriterOutput output)
            : base(logName)
        {
            externalLogType = output.ExternalLogType;
            externalLogName = output.ExternalLogName;
            writingParameters = new LogTextWritingParameters(output.DateTimeFormat, output.EntityLogKind, output.SuppressCategory, true);
            parameters = new Dictionary<string, string>(output.Params);
        }

        #endregion

        #region IDisposable Members

        /// <summary>
        /// Internal dispose method.
        /// </summary>
        /// <param name="disposing"></param>
        /// <param name="reason"></param>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes")]
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2102:CatchNonClsCompliantExceptionsInGeneralHandlers")]
        protected override void Dispose(bool disposing, string reason)
        {
            if (!disposed)
            {
                try
                {

                }
                catch (Exception) { }
                disposed = true;
            }
        }

        #endregion

        #region Public Properties

        public string ExternalLogType
        {
            get { return externalLogType; }
        }

        public string ExternalLogName
        {
            get { return externalLogName; }
        }

        public LogTextWritingParameters WritingParameters
        {
            get { return writingParameters; }
        }

        public Dictionary<string, string> Parameters
        {
            get { return parameters; }
        }

        #endregion

        #region Public Methods

        public override void PerformWrite(LogEntry logEntry)
        {
            if (disposed)
                return;
            PerformLogEntry(logEntry);
        }

        public override bool Equals(object obj)
        {
            ExternalLogWriterWrapper externalLogWriterWrapper = obj as ExternalLogWriterWrapper;
            if (externalLogWriterWrapper != null)
            {
                return externalLogWriterWrapper.externalLogType.Equals(externalLogType) &&
                    externalLogWriterWrapper.externalLogName.Equals(externalLogName);
            }
            return false;
        }

        public override int GetHashCode()
        {
            return externalLogType.GetHashCode() ^ externalLogName.GetHashCode();
        }

        #endregion

        #region Private Methods: Writing

        private void PerformLogEntry(LogEntry logEntry)
        {
            Logger.PerformExternalLogEntry(this, logEntry);
        }

        #endregion
    }
}