using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Text;

namespace iSTP_DataProcessing
{
    /// <summary>
    /// csv writer.
    /// </summary>
    public class CSVwriter
    {
        private static readonly Byte[] DefaultSeparationCharacter = Encoding.ASCII.GetBytes(",");
        private static readonly Byte[] DefaultNewLineCharacter = Encoding.ASCII.GetBytes("\n");
        private static readonly Encoding DefaultEncoding = Encoding.ASCII;
        private Stream MyStream;
        private Byte[] MySeparationCharacter;
        private Byte[] MyNewLineCharacter;
        private Byte[] Buffer = new Byte[1024];
        private Encoding MyEncoding;
        private Boolean IsFirstValue;

        /// <summary>
        /// Initialize components, saves current data stream
        /// </summary>
        /// <param name="OutStream">Current data stream</param>
        /// <param name="TextEncoding">Encoding status</param>
        public CSVwriter(Stream OutStream, Encoding TextEncoding = null)
        {
            MyStream = OutStream;
            IsFirstValue = true;
            if (TextEncoding == null)
            {
                MyEncoding = DefaultEncoding;
                MySeparationCharacter = DefaultSeparationCharacter;
                MyNewLineCharacter = DefaultNewLineCharacter;
            }
            else
            {
                MyEncoding = TextEncoding;
                MySeparationCharacter = MyEncoding.GetBytes(",");
                MyNewLineCharacter = MyEncoding.GetBytes("\n");
            }
        }

        /// <summary>
        /// Write string value in file
        /// </summary>
        /// <param name="Value">Value to write in file</param>
        public void WriteValue(String Value)
        {
            try
            {
                if (false == IsFirstValue)
                {
                    MyStream.Write(MySeparationCharacter, 0, MySeparationCharacter.Length);
                }
                else
                {
                    IsFirstValue = false;
                }
                MyStream.Write(Buffer, 0, MyEncoding.GetBytes(Value, 0, Value.Length, Buffer, 0));
            }
            catch (Exception e) 
            {
                DataProcessingError.Add("CSVwriter.cs", "Can't write value in file", e.ToString());
            }
        }

        /// <summary>
        /// Write end of line char in file
        /// </summary>
        public void WriteNewLine()
        {
            try
            {
                MyStream.Write(MyNewLineCharacter, 0, MyNewLineCharacter.Length);
                IsFirstValue = true;
            }
            catch (Exception e) 
            {
                DataProcessingError.Add("CSVwriter.cs", "Can't write new line in file", e.ToString());
            }
        }

        /// <summary>
        /// Gets or Sets internal data stream
        /// </summary>
        public Stream BaseStream
        {
            get
            {
                return MyStream;
            }
            set
            {
                MyStream = value;
                IsFirstValue = true;
            }
        }

        /// <summary>
        /// Gets or Sets separation character
        /// </summary>
        public String SeparationCharacter
        {
            get
            {
                return MyEncoding.GetString(MySeparationCharacter, 0, MySeparationCharacter.Length);
            }
            set
            {
                MySeparationCharacter = MyEncoding.GetBytes(value);
            }
        }

        /// <summary>
        /// Gets or sets new line character
        /// </summary>
        public String NewLineCharacter
        {
            get
            {
                return MyEncoding.GetString(MyNewLineCharacter, 0, MyNewLineCharacter.Length);
            }
            set
            {
                MyNewLineCharacter = MyEncoding.GetBytes(value);
            }
        }

    }
}
