//
// LogFactory.cs
//
// Author:
//       Carsten Sonne Larsen <cs@innolan.dk>
//
// Copyright (c) 2013-2016 Carsten Sonne Larsen
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

using System;
using System.Collections.Generic;

namespace Ntp.Log
{
    public static class LogFactory
    {
        public static string ApplicationName;
        private static readonly List<LogBase> Logs = new List<LogBase>();

        public static void Suspend()
        {
            foreach (LogBase log in Logs)
                log.Suspend();
        }

        public static void Resume()
        {
            foreach (LogBase log in Logs)
                log.Resume();
        }

        public static void Close()
        {
            foreach (LogBase log in Logs)
                log.Close();
        }

        public static void Cleanup()
        {
            Close();
            Logs.Clear();
        }

        public static LogGroup CreateErrorLog(string name)
        {
            var log = new LogGroup
            {
                new ConsoleLog(Severity.Error)
                {
                    ShowSeverity = false,
                    ShowTimeStamp = false
                }
            };
            return log;
        }

        public static LogBase CreateSysLog(string name)
        {
            LogBase log = new SysLog(name, Severity.Notice);
            Logs.Add(log);
            return log;
        }

        public static ActivityLog CreateActivityLog()
        {
            return new ActivityLog();
        }

        public static LogGroup CreateGroupLog()
        {
            return new LogGroup();
        }

        public static LogBase CreateLog(LogConfiguration config)
        {
            LogBase log = CreateLogInternal(config);
            Logs.Add(log);
            return log;
        }

        public static LogBase CreateLog(IEnumerable<LogConfiguration> configs)
        {
            var group = new LogGroup();

            foreach (LogConfiguration config in configs)
            {
                LogBase log = CreateLogInternal(config);
                group.Add(log);
                Logs.Add(log);
            }

            return group;
        }

        private static LogBase CreateLogInternal(LogConfiguration config)
        {
            switch (config.LogType)
            {
                case LogType.UnknownLog:
                case LogType.ConsoleLog:
                    return new ConsoleLog(config.Treshold, config.TimeFormat)
                    {
                        ShowSeverity = config.ShowSeverity,
                        ShowTimeStamp = config.ShowTimestamp
                    };
                case LogType.SysLog:
                    return new SysLog(ApplicationName, config.Treshold);
                case LogType.FileLog:
                    return new FileLog(config.File, config.Treshold, config.TimeFormat)
                    {
                        ShowSeverity = config.ShowSeverity,
                        ShowTimeStamp = config.ShowTimestamp
                    };
                default:
                    throw new ArgumentOutOfRangeException(LogConfigurationKeyword.Type);
            }
        }
    }
}