//                           UNCLASSIFIED
/*-------------------------------------------------------------------------
RAYTHEON PROPRIETARY: THIS DOCUMENT CONTAINS DATA OR INFORMATION
PROPRIETARY TO RAYTHEON COMPANY AND IS RESTRICTED TO USE ONLY BY PERSONS
AUTHORIZED BY RAYTHEON COMPANY IN WRITING TO USE IT. DISCLOSURE TO
UNAUTHORIZED PERSONS WOULD LIKELY CAUSE SUBSTANTIAL COMPETITIVE HARM TO
RAYTHEON COMPANY'S BUSINESS POSITION. NEITHER SAID DOCUMENT NOR ITS
CONTENTS SHALL BE FURNISHED OR DISCLOSED TO OR COPIED OR USED BY PERSONS
OUTSIDE RAYTHEON COMPANY WITHOUT THE EXPRESS WRITTEN APPROVAL OF RAYTHEON
COMPANY.

THIS PROPRIETARY NOTICE IS NOT APPLICABLE IF DELIVERED TO THE U.S.
GOVERNMENT.

UNPUBLISHED WORK - COPYRIGHT RAYTHEON COMPANY.
-------------------------------------------------------------------------*/
using System;
using System.IO;
using NLog;
using System.Reflection;

namespace Raytheon.Common
{
	/// <summary>
	/// Singleton for logging debug information and errors.
	/// </summary>
	public class ErrorLogger : IDisposable
	{
		#region PublicClassMembers

		/// <summary>
		/// Enum for tagging the logged data.
		/// </summary>
		public enum LogLevel
		{
			/// <summary>
			/// (Ordinal = 0) : Most verbose level. Used for development and seldom enabled in production.
			/// </summary>
			TRACE,
			/// <summary>
			/// (Ordinal = 1) : Debugging the application behavior from internal events of interest.
			/// </summary>
			DEBUG,
			/// <summary>
			/// An informational log statement.
			/// (Ordinal = 2) : Information that highlights progress or application lifetime events.
			/// </summary>
			INFO,
			/// <summary>
			/// (Ordinal = 3) : Warnings about validation issues or temporary failures that can be recovered.
			/// </summary>
			WARN,
			/// <summary>
			/// (Ordinal = 4) : Errors where functionality has failed or <see cref="System.Exception"/> have been caught.
			/// an error log statement.
			/// </summary>
			ERROR,
			/// <summary>
			/// (Ordinal = 5) : Most critical level. Application is about to abort.
			/// </summary>
			FATAL
		}

		private readonly ILogger _logger;

		#endregion

		#region PrivateClassMembers
		private static ErrorLogger _errorLoggerInstance;
		#endregion

		#region PublicClassFunctions

		/// <summary>
		/// Getter for this singleton.
		/// </summary>
		/// <param name="loggername">File location for Log.</param>
		/// <returns>The instance of this class.</returns>
		public static ErrorLogger Instance(string loggername = "CTS")
		{
			if (_errorLoggerInstance == null)
			{
				_errorLoggerInstance = new ErrorLogger(loggername);
			}

			return _errorLoggerInstance;
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="logDestination">The logger destination</param>
		/// <param name="logname">The location of the file to write to.</param>
		/// <param name="form"></param>
		private ErrorLogger(string logname)
		{
			_logger = LogManager.GetLogger(logname);

			if (LogManager.Configuration == null)
			{
				var assemblyFolder = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
				//TODO: Unhandled exception if no nlog.config
				LogManager.Configuration = new NLog.Config.XmlLoggingConfiguration(assemblyFolder + "\\nlog.config");
			}
		}

		/// <summary>
		/// Write data to the log file.
		/// </summary>
		/// <param name="message">The data to write.</param>
		public void Write(string message, LogLevel logLevel = LogLevel.ERROR)
		{
			_logger.Log(NLog.LogLevel.FromOrdinal((int)logLevel), message);
		}

		public void Dispose()
		{
		}

		#endregion

	}
}
