// --------------------------------------------------------------------------------------------------------------------
// <copyright file="TextCodePageConverter.cs" company="Microsoft Corporation">
//   Copyright (c) 2008, 2009, 2010 All Rights Reserved, Microsoft Corporation
//
//   This source is subject to the Microsoft Permissive License.
//   Please see the License.txt file for more information.
//   All other rights reserved.
//
//   THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY 
//   KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
//   IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
//   PARTICULAR PURPOSE.
//
// </copyright>
// <summary>
//    Text converter for the code page.
// </summary>

namespace Microsoft.Exchange.Data.TextConverters.Internal.Text
{
    using System;
    using System.IO;
    using System.Text;

    /// <summary>
    /// Encodes the text for the code page.
    /// </summary>
    internal class TextCodePageConverter : IProducerConsumer, IDisposable
    {
        ////////////////////////////////////////////////////////////

        #region Private Variables
        /// <summary>
        /// Input to be converted.
        /// </summary>
        private ConverterInput input;

        /// <summary>
        /// End of file reached flag.
        /// </summary>
        private bool endOfFile;

        /// <summary>
        /// Indicates if text was found after buffer is read.
        /// </summary>
        private bool gotAnyText;

        /// <summary>
        /// Converted output.
        /// </summary>
        private ConverterOutput output;

        #endregion

        /// <summary>
        /// Initializes a new instance of the <see cref="TextCodePageConverter"/> class.
        /// </summary>
        /// <param name="input">The input.</param>
        /// <param name="output">The output.</param>
        public TextCodePageConverter(ConverterInput input, ConverterOutput output)
        {
            this.input = input;
            this.output = output;
        }

        /// <summary>
        /// Runs this instance.
        /// </summary>
        public void Run()
        {
            if (this.endOfFile)
            {
                return;
            }

            char[] buffer = null;
            int start = 0;
            int current = 0;
            int end = 0;

            if (!this.input.ReadMore(ref buffer, ref start, ref current, ref end))
            {
                // cannot decode more data until next input chunk is available
                return;
            }

            if (this.input.EndOfFile)
            {
                this.endOfFile = true;
            }

            if (end - start != 0)
            {
                if (!this.gotAnyText)
                {
                    if (this.output is ConverterEncodingOutput)
                    {
                        ConverterEncodingOutput encodingOutput = this.output as ConverterEncodingOutput;

                        if (encodingOutput.CodePageSameAsInput)
                        {
                            if (this.input is ConverterDecodingInput)
                            {
                                encodingOutput.Encoding = (this.input as ConverterDecodingInput).Encoding;
                            }
                            else
                            {
                                encodingOutput.Encoding = Encoding.UTF8;
                            }
                        }
                    }

                    this.gotAnyText = true;
                }

                this.output.Write(buffer, start, end - start);

                this.input.ReportProcessed(end - start);
            }

            if (this.endOfFile)
            {
                this.output.Flush();
            }
        }

        /// <summary>
        /// Flushes this instance.
        /// </summary>
        /// <returns>
        /// True is end of file reached.
        /// </returns>
        public bool Flush()
        {
            if (!this.endOfFile)
            {
                this.Run();
            }

            return this.endOfFile;
        }

        /// <summary>
        /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
        /// </summary>
        void IDisposable.Dispose()
        {
            if (this.input != null /*&& this.input is IDisposable*/)
            {
                ((IDisposable)this.input).Dispose();
            }

            if (this.output != null /*&& this.output is IDisposable*/)
            {
                ((IDisposable)this.output).Dispose();
            }

            this.input = null;
            this.output = null;

            GC.SuppressFinalize(this);
        }
    }
}
