using System;
using System.Text;

/*
    Copyright (c) 2011 flixmonk. (http://DVDSwarm.codeplex.com)
    All Rights Reserved.
        
    Redistribution and use in source and binary forms, with or without modification,
    are permitted provided that the following conditions are met:
        
    * Redistributions of source code must retain the above copyright notice, this list
      of conditions and the following disclaimer.
        
    * Redistributions in binary form must reproduce the above copyright notice, this
    list of conditions and the following disclaimer in the documentation and/or other
    materials provided with the distribution. 
        
    * Neither the name of The Flixmonk nor the names of its contributors may be used to
    endorse or promote products derived from this software without specific prior written
    permission.
        
    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
    EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
    OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
    SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
    OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
    TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
namespace DVDSwarm
{
    /// <summary>
    /// Manages the creation and updating of a log. The contents of a text buffer are appended to the end
    /// of a log file as UTF-8 encoded text.
    /// </summary>
    sealed public class LogWriter
    {
        #region Fields

        private string _filename = string.Empty;
        private StringBuilder _text = new StringBuilder();

        #endregion

        #region Properties

        /// <summary>
        /// Gets the filename for the log.
        /// </summary>
        public string Filename
        {
            get
            {
                return _filename;
            }
        }
        /// <summary>
        /// Gets the contents of the text buffer.
        /// </summary>
        public string Text
        {
            get
            {
                return _text.ToString();
            }
        }

        #endregion

        #region Methods

        /// <summary>
        /// Creates a new instance of the LogWriter class by writing a new out file with the specified
        /// filename. If the output file already exists, it is overwritten.
        /// </summary>
        /// <param name="filename">The output filename for the log.</param>
        public LogWriter(string filename)
        {
            if (string.IsNullOrEmpty(filename))
            {
                throw new ArgumentException("filename is null or empty.");
            }

            _filename = filename;

            // Delete the log file if it already exists.
            if (System.IO.File.Exists(_filename))
            {
                System.IO.File.Delete(_filename);
            }

            System.IO.File.CreateText(_filename);
        }

        /// <summary>
        /// Creates a new instance of the LogWriter class by writing a new out file with the specified
        /// filename. If the output file already exists, it is overwritten.
        /// </summary>
        /// <param name="filename">The output filename for the log.</param>
        /// <param name="header">The header text to write to the log.</param>
        public LogWriter(string filename, string header)
        {
            if (string.IsNullOrEmpty(filename))
            {
                throw new ArgumentException("filename is null or empty.");
            }

            _filename = filename;

            // Delete the log file if it already exists.
            if (System.IO.File.Exists(_filename))
            {
                System.IO.File.Delete(_filename);
            }

            System.IO.File.WriteAllText(_filename, header);
        }

        /// <summary>
        /// Appends a copy of the specified string to the end of the text buffer.
        /// </summary>
        /// <param name="value">The StringBuilder containing the text to append.</param>
        /// <exception cref="System.ArgumentException">Thrown when value is null or empty.</exception>
        public void Append(string value)
        {
            if (string.IsNullOrEmpty(value))
            {
                throw new ArgumentException("value is null or empty.");
            }

            _text.Append(value);
        }

        /// <summary>
        /// Appends the content of the StringBuilder to the end of the text buffer.
        /// </summary>
        /// <param name="value">The StringBuilder containing the text to append.</param>
        /// <exception cref="System.ArgumentNullException">Thrown when value is null.</exception>
        public void Append(StringBuilder value)
        {
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }
            
            _text.Append(value.ToString());
        }

        /// <summary>
        /// Appends the default line terminator to the end of the text buffer.
        /// </summary>
        public void AppendLine()
        {
            _text.AppendLine();
        }

        /// <summary>
        /// Appends a copy of the specified string followed by the default line terminator to the
        /// end of the text buffer.
        /// </summary>
        /// <param name="value">The text to add.</param>
        /// <exception cref="System.ArgumentException">Thrown when value is null or empty.</exception>
        public void AppendLine(string value)
        {
            if (string.IsNullOrEmpty(value))
            {
                throw new ArgumentException("value is null or empty.");
            }
            
            _text.AppendLine(value);
        }

        /// <summary>
        /// Removes all text from the text buffer.
        /// </summary>
        public void Clear()
        {
            _text.Length = 0;
        }

        /// <summary>
        /// Releases any system resources associated with the writer. 
        /// </summary>
        public void Close()
        {
            _text.Length = 0;
            _text = null;
        }

        /// <summary>
        /// Appends the contents of the text buffer to the end of the log file. The text buffer is cleared
        /// after being written.
        /// </summary>
        public void UpdateLog()
        {
            System.IO.File.AppendAllText(_filename, _text.ToString(), System.Text.Encoding.UTF8);
            _text.Length = 0;
        }

        #endregion
    }
}
