using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SqlServer.ReportingServices2005.Execution;
using System.IO;
using System.Globalization;
using Microsoft.SqlServer.NotificationServices;

namespace RSContentFormatter
{
	public class RSContentFormatter: IContentFormatter
	{
		static string Render(string songTitle)
		{
			ReportExecutionService rs = new ReportExecutionService();
			rs.Url = "http://localhost/reportserver/ReportExecution2005.asmx";
			rs.Credentials = System.Net.CredentialCache.DefaultCredentials;

			string format = "HTML4.0";

			ParameterValue[] parameters = new ParameterValue[1];
			parameters[0] = new ParameterValue();
			parameters[0].Name = "SongTitle";
			parameters[0].Value = songTitle;

			string mimeType;
			string encoding;
			string extension;
			Warning[] warnings = null;
			string[] streamIDs = null;

			rs.LoadReport("/Report/Report2", null);
			rs.SetExecutionParameters(parameters, null);

			byte[] result = rs.Render(format, null, out extension, out mimeType, out encoding, out warnings, out streamIDs);

			UTF8Encoding ue = new UTF8Encoding();
			return ue.GetString(result);
		}

		public void Close()
		{
		}

		public string FormatContent(
			string subscriberLocale,
			string deviceTypeName,
			RecipientInfo recipientInfo,
			System.Collections.Hashtable[] rawContent)
		{
			return Render((string)rawContent[0]["SongTitle"]);
		}

		public void Initialize(System.Collections.Specialized.StringDictionary arguments, bool digest)
		{
		}
	}
}
