using System;
using System.IO;
using DsvThrowHelper = Volpe.Cafe.IO.DSV.ThrowHelper;

namespace Volpe.Cafe.Logging
{
    /// <summary>
    /// Provides static methods for the creation and opening of a single log file.
    /// </summary>
    [Serializable]
    public static class LogFile
    {
        /// <summary>
        /// Defines a new-line string used when writing log files.
        /// </summary>
        public const string NewLine = "\n";

        /// <summary>
        /// Defines a character used for delimiting the columns in a log file.
        /// </summary>
        public const char Delimiter = '\t';

        /// <summary>
        /// Opens a log file at the specified <paramref name="path"/> for writing. If the file exists, it will be
        /// overwritten; otherwise, a new file will be created. The specified <paramref name="headers"/> will be
        /// written to the file upon successful opening.
        /// </summary>
        /// <param name="path">The complete file path to write to.</param>
        /// <param name="headers">An array of column headers to write to the log file.</param>
        /// <returns>A <see cref="LogFileWriter"/> instance to use for writing the log file.</returns>
        /// <inheritdoc cref="LogFileWriter(string, string[])" path="/exception"/>
        public static LogFileWriter OpenWrite(string path, string[] headers)
        {
            DsvThrowHelper.ThrowIfHeadersNotValid(headers);
            return new LogFileWriter(path, headers);
        }

        /// <summary>
        /// Opens an existing log file at the specified <paramref name="path"/> for writing. If the file exists, it
        /// will be appended to; otherwise, an exception will be thrown.
        /// </summary>
        /// <param name="path">The complete file path to write to.</param>
        /// <returns>A <see cref="LogFileWriter"/> instance to use for writing the log file.</returns>
        /// <exception cref="FileNotFoundException">The file cannot be found.</exception>
        /// <inheritdoc cref="StreamWriter(string, bool)" path="/exception"/>
        public static LogFileWriter OpenAppend(string path)
        {
            DsvThrowHelper.ThrowIfFileNotFound(path);
            return new LogFileWriter(path, null);
        }

        /// <summary>
        /// Opens a log file at the specified <paramref name="path"/> for reading.
        /// </summary>
        /// <param name="path">The complete file path to read.</param>
        /// <returns>A <see cref="LogFileReader"/> instance to use for reading the log file.</returns>
        /// <inheritdoc cref="LogFileReader(string)" path="/exception"/>
        public static LogFileReader OpenRead(string path)
        {
            return new LogFileReader(path);
        }

    }
}
