using System;
using System.IO;
using EonaCat.Logger.EonaCatCoreLogger.Internal;

namespace EonaCat.Logger.EonaCatCoreLogger
{
    // 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>
    /// Options for file logging.
    /// </summary>
    public class FileLoggerOptions : BatchingLoggerOptions
    {
        private int _fileSizeLimit = 200 * 1024 * 1024;
        private int _retainedFileCountLimit = 50;
        private int _maxRolloverFiles = 10;
        private int _maxTries = 3;

        public static string DefaultPath => AppDomain.CurrentDomain.RelativeSearchPath ?? AppDomain.CurrentDomain.BaseDirectory;

        /// <summary>
        /// Gets or sets a strictly positive value representing the maximum log size in bytes or null for no limit.
        /// Once the log is full, no more messages will be appended.
        /// Defaults to <c>200MB</c>.
        /// </summary>
        public int FileSizeLimit
        {
            get => _fileSizeLimit;

            set
            {
                if (value <= 0)
                {
                    throw new ArgumentOutOfRangeException(nameof(value), $"{nameof(FileSizeLimit)} must be positive.");
                }
                _fileSizeLimit = value;
            }
        }

        /// <summary>
        /// Gets or sets a strictly positive value representing the maximum retained file count or null for no limit.
        /// Defaults to <c>50</c>.
        /// </summary>
        public int RetainedFileCountLimit
        {
            get => _retainedFileCountLimit;

            set
            {
                if (value <= 0)
                {
                    throw new ArgumentOutOfRangeException(nameof(value), $"{nameof(RetainedFileCountLimit)} must be positive.");
                }
                _retainedFileCountLimit = value;
            }
        }

        /// <summary>
        /// Gets or sets the max times to try to write to the file.
        /// Defaults 3.
        /// </summary>
        public int MaxWriteTries
        {
            get => _maxTries;

            set
            {
                _maxTries = value;
            }
        }

        /// <summary>
        /// Determines if we need to use the local time in the logging or UTC (default:false)
        /// </summary>
        public bool UseLocalTime { get; set; }

        /// <summary>
        /// Gets or sets a strictly positive value representing the maximum retained file rollovers or null for no limit.
        /// Defaults to <c>10</c>.
        /// </summary>
        public int MaxRolloverFiles
        {
            get => _maxRolloverFiles;

            set
            {
                if (value <= 0)
                {
                    throw new ArgumentOutOfRangeException(nameof(value), $"{nameof(MaxRolloverFiles)} must be positive.");
                }
                _maxRolloverFiles = value;
            }
        }

        /// <summary>
        /// Gets or sets the filename prefix to use for log files.
        /// Defaults to <c>EonaCat_</c>.
        /// </summary>
        public string FileNamePrefix { get; set; } = "EonaCat";

        /// <summary>
        /// The directory in which log files will be written, relative to the app process.
        /// Default to <c>executablePath\logs</c>
        /// </summary>
        /// <returns></returns>
        public string LogDirectory { get; set; } = Path.Combine(DefaultPath, "logs");
    }
}