using System;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;

namespace EonaCat.Logger.EonaCatCoreLogger.Extensions
{
    // This file is part of the EonaCat project(s) which is released under the Apache License.
    // See the LICENSE file or go to https://EonaCat.com/License for full license details.

    /// <summary>
    /// Extensions for adding the <see cref="FileLoggerProvider" /> to the <see cref="ILoggingBuilder" />
    /// </summary>
    public static class FileLoggerFactoryExtensions
    {
        private static ILoggingBuilder AddEonaCatFileLogger(this ILoggingBuilder builder)
        {
            builder.Services.AddSingleton<ILoggerProvider, FileLoggerProvider>();
            return builder;
        }

        /// <summary>
        /// Adds the EonaCat File Logger named 'EonaCatFileLogger' to the factory.
        /// </summary>
        /// <param name="builder">The <see cref="ILoggingBuilder"/> to use.</param>
        /// <param name="filenamePrefix">Sets the filename prefix to use for log files (optional)</param>
        /// <param name="fileLoggerOptions">the options for the fileLogger that needs to be used (optional)</param>
        public static ILoggingBuilder AddEonaCatFileLogger(this ILoggingBuilder builder, string filenamePrefix = null, FileLoggerOptions fileLoggerOptions = null)
        {
            if (fileLoggerOptions == null)
            {
                fileLoggerOptions = new FileLoggerOptions();
            }

            if (!string.IsNullOrWhiteSpace(filenamePrefix))
            {
                fileLoggerOptions.FileNamePrefix = filenamePrefix;
            }
            builder.AddEonaCatFileLogger(options =>
            {
                options.FileNamePrefix = fileLoggerOptions.FileNamePrefix;
                options.FlushPeriod = fileLoggerOptions.FlushPeriod;
                options.RetainedFileCountLimit = fileLoggerOptions.RetainedFileCountLimit;
                options.MaxWriteTries = fileLoggerOptions.MaxWriteTries;
                options.FileSizeLimit = fileLoggerOptions.FileSizeLimit;
                options.LogDirectory = fileLoggerOptions.LogDirectory;
                options.BatchSize = fileLoggerOptions.BatchSize;
                options.FileSizeLimit = fileLoggerOptions.FileSizeLimit;
                options.IsEnabled = fileLoggerOptions.IsEnabled;
                options.MaxRolloverFiles = fileLoggerOptions.MaxRolloverFiles;
                options.UseLocalTime = fileLoggerOptions.UseLocalTime;
            }

            );
            return builder;
        }

        /// <summary>
        /// Adds the EonaCat File Logger named 'EonaCatFileLogger' to the factory.
        /// </summary>
        /// <param name="builder">The <see cref="ILoggingBuilder"/> to use.</param>
        /// <param name="configure">Configure an instance of the <see cref="FileLoggerOptions" /> to set logging options</param>
        public static ILoggingBuilder AddEonaCatFileLogger(this ILoggingBuilder builder, Action<FileLoggerOptions> configure)
        {
            if (configure == null)
            {
                throw new ArgumentNullException(nameof(configure));
            }
            builder.AddEonaCatFileLogger();
            builder.Services.Configure(configure);

            return builder;
        }
    }
}