//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Text.RegularExpressions;

namespace CommunityServer.Components
{
	/// <summary>
	/// Summary description for InkModule.
	/// </summary>
	public class InkModule : ICSModule
	{
		public InkModule()
		{
			//
			// TODO: Add constructor logic here
			//
		}
		#region ICSModule Members

		public void Init(CSApplication csa, System.Xml.XmlNode node)
		{
			csa.PreProcessPost +=new CSPostEventHandler(csa_PreProcessPost);
		}

		#endregion

		private void csa_PreProcessPost(IContent content, CSPostEventArgs e)
		{
			Post post = content as Post;

			if(e.State != ObjectState.Delete && post != null)
			{
				if(post.HasInk)
				{
					TextToken token = new TextToken();
					token.Token = "Ink";

					if(!Regex.IsMatch(post.FormattedBody,token.Pattern,RegexOptions.IgnoreCase))
					{
						post.FormattedBody += "<p>[Ink]</p>";
					}

					InkData ink = Ink.Get(post.InkID);
					if(ink != null && !ink.IsEmpty)
					{

						token.Text = string.Format("<img src=\"{0}\" />", ink.Url);
						post.FormattedBody = token.FormatText(post.FormattedBody);
					}
				}
			}
		}
	}
}
