using System;
using System.Diagnostics;
using System.IO;
using Medivators.Common;
using Medivators.Common.Enums;
using Medivators.Common.Settings;

namespace Medivators.Common.Logging
{
	/// <summary>
	/// Summary description for TextLog.
	/// </summary>
	/// 
	public class TextLog : ILogProvider
	{
		private StreamWriter	_file = null;
		private string			_path = string.Empty;

		#region construct/init/dispose
		public TextLog( string FilePath )
		{
			this._path = FilePath;
		}
		public void Dispose()
		{
			if ( this._file != null )
			{
				this._file.Flush();
				this._file.Close();
			}
		}
		#endregion

		#region ILogProvider Members

		public void Initialize()
		{
			try 
			{
				// validate file path
				if (!Directory.Exists(Path.GetDirectoryName(this._path)))
					Directory.CreateDirectory(Path.GetDirectoryName(this._path));

				//	if logfile already exists, scrap it
				if ( File.Exists ( this._path ) )
				{
					File.SetAttributes(this._path, FileAttributes.Normal);
					File.Delete(this._path);
				}
			}
			catch ( Exception ex )
			{
				ErrorHandler.GetInstance().LogException("TextLog.Initialize", ex);
			}
		}

		public bool WriteLog(string Text)
		{
			return this.WriteLog(Text, EventLogEntryType.Information, null );
		}
		public bool WriteLog(string Text, System.Diagnostics.EventLogEntryType LogType)
		{
			return this.WriteLog(Text, LogType, null);
		}
		public bool WriteLog(string Text, System.Diagnostics.EventLogEntryType LogType, byte[] Data)
		{
			try
			{
				//	prepare the log line
				string logLine = DateTime.Now.ToString();
				logLine += Constants.TabSpaces + "{" + LogType.ToString() + "}";
				logLine += Constants.TabSpaces + Text;
				if (Data != null)
				{
					logLine += Constants.NewLine;
					logLine += Constants.TabSpaces + "Data: ";
					logLine += Utilities.ByteArrayToString(Data, true);
				}

				//	 flush anything pending from previous calls
				this._file = new StreamWriter(this._path, true);
				this._file.WriteLine(logLine);
				this._file.Flush();
				this._file.Close();
				return true;
			}
			catch
			{
				return false;
			}
		}
		public bool WriteLog(LogEntryType Criticality, string Text)
		{
			return this.WriteLog(Text, EventLogEntryType.Information, null);
		}
		public bool WriteLog(LogEntryType Criticality, string Text, System.Diagnostics.EventLogEntryType LogType)
		{
			return this.WriteLog(Text, LogType, null);
		}
		public bool WriteLog(LogEntryType Criticality, string Text, System.Diagnostics.EventLogEntryType LogType, byte[] Data)
		{
			bool rtnVal = true;
			byte EntryValue = Convert.ToByte(Criticality);
			byte LogValue = Convert.ToByte(TextSettings.GetInstance()[Medivators.Common.Enums.SettingTypes.Application, "LoggingLevel"]);
			if (EntryValue > LogValue || LogValue == 0)
				rtnVal = false;
			else
				rtnVal = this.WriteLog(Text, LogType, Data);
			return rtnVal;
		}

		#endregion

		public string LogfilePath 
		{
			get { return this._path; }
			set { this._path = value; }
		}
	}
}
