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

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

        /// <summary>
        /// Opens a report 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 report file.</param>
        /// <returns>A <see cref="ReportFileWriter"/> instance to use for writing the report file.</returns>
        /// <inheritdoc cref="ReportFileWriter(string, string[])" path="/exception"/>
        public static ReportFileWriter OpenWrite(string path, string[] headers)
        {
            DsvThrowHelper.ThrowIfHeadersNotValid(headers);
            return new ReportFileWriter(path, headers);
        }

        /// <summary>
        /// Opens an existing report 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="ReportFileWriter"/> instance to use for writing the report file.</returns>
        /// <exception cref="FileNotFoundException">The file cannot be found.</exception>
        /// <inheritdoc cref="StreamWriter(string, bool)" path="/exception"/>
        public static ReportFileWriter OpenAppend(string path)
        {
            DsvThrowHelper.ThrowIfFileNotFound(path);
            return new ReportFileWriter(path, null);
        }

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

    }
}
