using System;
using System.Collections.Generic;
using System.Text;
using Telerik.Reporting.Processing;
using System.IO;
using Telerik.Reporting.Interfaces.Attributes;

namespace MyTextRenderingExtension
{
    [ExtensionName("MYTEST")]
    [ExtensionLocalizedDescription("My Test Rendering Extension")]
    public class MyTextRenderingExtension : IRenderingExtension
    {
        #region IRenderingExtension Members

        bool IRenderingExtension.Render(Telerik.Reporting.Processing.Report report
            , System.Collections.Hashtable renderingContext
            , System.Collections.Hashtable deviceInfo
            , CreateStream createStreamCallback
            , EvaluateHeaderFooterExpressions evalHeaderFooterCallback)
        {
            // Create a new stream for the text file that this rendering extension
            // generates; We will use a StreamWriter to output the text.
            StreamWriter sw1 = new StreamWriter(createStreamCallback("Test", "txt", null, "text/plain"));
            // Output the report hierarchy
            Write(sw1, report, 0);
            // Do not close/dispose the stream writer because it will close the stream.
            sw1.Flush(); 
            return true;
        }

        static void Write(StreamWriter writer, ProcessingElement element, int level)
        {
            WriteIndent(writer, level);
            writer.WriteLine(element.GetType().Name);
            for (int i = 0; i < element.ChildElements.Count; i++)
            {
                Write(writer, element.ChildElements[i], level + 1);
            }
        }

        static void WriteIndent(StreamWriter writer, int level)
        {
            writer.Write(new string('.', level));
        }

        #endregion

        #region IExtension Members

        void Telerik.Reporting.IExtension.Initialize(System.Collections.Hashtable deviceInfo)
        {
        }

        #endregion

        #region IDisposable Members

        void IDisposable.Dispose()
        {
        }

        #endregion
    }
}
