using System;
using System.Diagnostics;
using Interfaces;

namespace CardReaderService.DefaultComponents
{
    public class DefaultLogger : ILogger
    {
        private string _eventLogSource = "CardReaderDefaultLogger";
        private string _logName = "CardReaderService";
        private EventLog _eventLog;

        public DefaultLogger()
        {
            if (!EventLog.SourceExists(_eventLogSource))
            {
                EventLog.CreateEventSource(_eventLogSource, _logName);
            }
        }
        public bool IsDebugEnabled { get { return true; } }
        public bool IsErrorEnabled { get { return true; } }
        public bool IsFatalEnabled { get { return true; } }
        public bool IsInfoEnabled { get { return true; } }
        public bool IsTraceEnabled { get { return true; } }
        public bool IsWarnEnabled { get { return true; } }

        public void Debug(Exception exception)
        {
            CheckEventLog();
            _eventLog.WriteEntry(exception.ToString(), EventLogEntryType.Information);
        }

        public void Debug(string format, params object[] args)
        {
            CheckEventLog();
            _eventLog.WriteEntry(string.Format(format, args), EventLogEntryType.Information);
        }

        public void Debug(Exception exception, string format, params object[] args)
        {
            CheckEventLog();
            _eventLog.WriteEntry(string.Format(exception + ", " + format, args),
                EventLogEntryType.Information);
        }

        public void Error(Exception exception)
        {
            CheckEventLog();
            _eventLog.WriteEntry(exception.ToString(), EventLogEntryType.Error);
        }

        public void Error(string format, params object[] args)
        {
            CheckEventLog();
            _eventLog.WriteEntry(string.Format(format, args), EventLogEntryType.Error);
        }

        public void Error(Exception exception, string format, params object[] args)
        {
            CheckEventLog();
            _eventLog.WriteEntry(string.Format(exception + ", " + format, args),
                 EventLogEntryType.Error);
        }

        public void Fatal(Exception exception)
        {
            CheckEventLog();
            _eventLog.WriteEntry(exception.ToString(), EventLogEntryType.Error);
        }

        public void Fatal(string format, params object[] args)
        {
            CheckEventLog();
            _eventLog.WriteEntry(string.Format(format, args), EventLogEntryType.Error);
        }

        public void Fatal(Exception exception, string format, params object[] args)
        {
            CheckEventLog();
            _eventLog.WriteEntry(string.Format(exception + ", " + format, args),
                EventLogEntryType.Error);
        }

        public void Info(Exception exception)
        {
            CheckEventLog();
            _eventLog.WriteEntry(exception.ToString(), EventLogEntryType.Information);
        }

        public void Info(string format, params object[] args)
        {
            CheckEventLog();
            _eventLog.WriteEntry(string.Format(format, args), EventLogEntryType.Information);
        }

        public void Info(Exception exception, string format, params object[] args)
        {
            CheckEventLog();
            _eventLog.WriteEntry(string.Format(exception + ", " + format, args),
                EventLogEntryType.Information);
        }

        public void Trace(Exception exception)
        {
            CheckEventLog();
            _eventLog.WriteEntry(exception.ToString(), EventLogEntryType.Information);
        }

        public void Trace(string format, params object[] args)
        {
            CheckEventLog();
            _eventLog.WriteEntry(string.Format(format, args), EventLogEntryType.Information);
        }

        public void Trace(Exception exception, string format, params object[] args)
        {
            CheckEventLog();
            _eventLog.WriteEntry(string.Format(exception + ", " + format, args),
                EventLogEntryType.Information);
        }

        public void Warn(Exception exception)
        {
            CheckEventLog();
            _eventLog.WriteEntry(exception.ToString(), EventLogEntryType.Warning);
        }

        public void Warn(string format, params object[] args)
        {
            CheckEventLog();
            _eventLog.WriteEntry(string.Format(format, args), EventLogEntryType.Warning);
        }

        public void Warn(Exception exception, string format, params object[] args)
        {
            CheckEventLog();
            _eventLog.WriteEntry(string.Format(exception + ", " + format, args),
                EventLogEntryType.Warning);
        }

        private void CheckEventLog()
        {
            if (_eventLog == null)
            {
                _eventLog = new EventLog();
                _eventLog.Source = _eventLogSource;
                _eventLog.Log = _logName;
            }
        }
    }
}
