﻿using System;
using System.IO;

namespace ZeroLog.Appenders
{
    public class ConsoleAppender : AppenderBase<DefaultAppenderConfig>
    {
        public const string DefaultPrefixPattern = "%time - %level - %logger || ";

        private readonly Stream _output;

        public ConsoleAppender()
            : this(DefaultPrefixPattern)
        {
        }

        public ConsoleAppender(string prefixPattern)
        {
            _output = Console.OpenStandardOutput();
            Configure(prefixPattern);
        }

        public override void Configure(DefaultAppenderConfig parameters)
        {
            Configure(parameters?.PrefixPattern ?? DefaultPrefixPattern);
        }

        public override void WriteEvent(ILogEventHeader logEventHeader, byte[] messageBytes, int messageLength)
        {
            Console.BackgroundColor = ConsoleColor.Black;
            switch (logEventHeader.Level)
            {
                case Level.Fatal:
                    Console.ForegroundColor = ConsoleColor.Black;
                    Console.BackgroundColor = ConsoleColor.White;
                    break;
                case Level.Error:
                    Console.ForegroundColor = ConsoleColor.Red;
                    break;
                case Level.Warn:
                    Console.ForegroundColor = ConsoleColor.Yellow;
                    break;
                case Level.Info:
                    Console.ForegroundColor = ConsoleColor.White;
                    break;
                case Level.Debug:
                    Console.ForegroundColor = ConsoleColor.Gray;
                    break;
                case Level.Verbose:
                    Console.ForegroundColor = ConsoleColor.Gray;
                    break;
                case Level.Finest:
                    Console.ForegroundColor = ConsoleColor.Gray;
                    break;
                default:
                    Console.ForegroundColor = ConsoleColor.White;
                    break;
            }

            WriteEventToStream(_output, logEventHeader, messageBytes, messageLength);
        }
    }
}
