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

using System.Collections;
using System.IO;
using System.Web.Caching;
using System.Web.Mail;
using System.Xml;
using CommunityServer.Configuration;

namespace CommunityServer.Components
{
	/// <summary>
	/// Summary description for XmlEmailTemplateProvider.
	/// </summary>
	public class XmlEmailTemplateProvider : EmailTemplateProvider
	{
		public XmlEmailTemplateProvider() { }

		public override MailMessage GetTemplate(string emailType, User userEmailTo)
		{
			MailMessage email = new MailMessage();
			Hashtable emailTemplates = null;

			// First, load the override emails file
			emailTemplates = LoadEmailTemplates(null);

			// Next, try to load the templates in the user language
			if(!emailTemplates.ContainsKey( emailType) && (userEmailTo != null) && !Globals.IsNullorEmpty(userEmailTo.Profile.Language)) 
				emailTemplates = LoadEmailTemplates(userEmailTo.Profile.Language);
			
			// if the user language templates are not found, then load the system defaults
			if((emailTemplates == null) || !emailTemplates.ContainsKey( emailType ) ) 
			{
				emailTemplates = LoadEmailTemplates( CSConfiguration.GetConfig().DefaultLanguage );

				// if they still are not found, then load the en-US templates
				if((emailTemplates == null) || !emailTemplates.ContainsKey( emailType )) 
					emailTemplates = LoadEmailTemplates( "en-US" );
			}

			// Get the template;
			EmailTemplate template =  emailTemplates[emailType] as EmailTemplate;

			// If it is null, it doesn't exist, so return null
			if(template == null)
				return null;

			email.Subject = template.Subject;
			email.Priority = template.Priority;
			email.From = template.From;
			email.Body = template.Body;
			
			return email;
		}

		protected Hashtable LoadEmailTemplates(string language) 
		{
			Hashtable emailTemplates;
			FileInfo f;
			string cacheKey = "emailTemplates-" + language;
			CSContext csContext = CSContext.Current;

			// Try and get the templates from cache
			emailTemplates = CSCache.Get(cacheKey) as Hashtable;
			if(emailTemplates == null) 
			{
				emailTemplates = new Hashtable();

				try 
				{
					if(Globals.IsNullorEmpty(language))
					{
						if(File.Exists(csContext.PhysicalPath("emails.xml")))
							f = new FileInfo(csContext.PhysicalPath("emails.xml"));
						else
							return new Hashtable();
					}
					else
						f = new FileInfo(csContext.PhysicalPath("Languages\\" + language + "\\emails\\emails.xml"));
				} 
				catch 
				{
					throw new CSException(CSExceptionType.EmailTemplateNotFound, "No email templates found for language: " + language);
				}


				// Read in the file
				//
				FileStream reader = f.OpenRead();
				XmlDocument d = new XmlDocument();
				d.Load(reader);
				reader.Close();

				// Loop through all contained emails
				//
				foreach(XmlNode node in d.GetElementsByTagName("email")) 
				{
					// Create a new email template
					//
					EmailTemplate t = new EmailTemplate(node);

					// Add to the lookup table
					//
					emailTemplates.Add(t.EmailType, t);
				}

				// Terry Denham 7/26/2004
				// changing default caching duration to 2 hours, intead of forever
				// ScottW 1/24/2005 adding file dependency
				CacheDependency dep = new CacheDependency(f.FullName);
				CSCache.Insert(cacheKey,emailTemplates,dep,2 * CSCache.HourFactor);
			}

			return emailTemplates;
		}

	}
}
