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

using System;
using System.Web.Mail;
using CommunityServer;
using CommunityServer.Components;

namespace CommunityServer.Files.Components
{
	internal class FileEmails : Emails
	{
		private FileEmails()
		{
		}

		public static void EntryTracking(EntryComment post, Entry entry)
		{
			SectionTracking(post, new EnquePostEmails(CreateEntryCommentMessage), CreateEntryCommentFormatList((Folder) entry.Section, post, entry) );
		}

		public static void FolderCommentNotification(EntryComment post, Entry entry)
		{
			if (entry.EnableCommentNotification && !Globals.IsNullorEmpty(entry.Section.Owners))
			{
				// Loop through each owner and send the notification email if that owner was 
				// not the one who posted the feedback item.
				foreach(string username in entry.Section.OwnerArray)
				{
					User owner = Users.FindUserByUsername(username);
					if (!Globals.IsNullorEmpty(owner.Email) && (owner.Username != post.Username))
					{
						MailMessage email = CreateFolderCommentNotification(post, owner, CreateEntryCommentFormatList((Folder) entry.Section, post, entry));
						if(email != null && email.To != null && email.To.Trim().Length > 0)
							EmailQueueProvider.Instance().QueueEmail(email);
					}
				}
			}
		}

		public static void FolderEntryNotification(Entry entry)
		{
			if (entry.EnableEntryNotification && !Globals.IsNullorEmpty(entry.Section.Owners))
			{
				// Loop through each owner and send the notification email if that owner was 
				// not the one who posted the feedback item.
				foreach(string username in entry.Section.OwnerArray)
				{
					User owner = Users.FindUserByUsername(username);
					if (!Globals.IsNullorEmpty(owner.Email) && (owner.Username != entry.Username))
					{
						MailMessage email = CreateFolderEntryNotification(entry, owner, CreateEntryFormatList((Folder) entry.Section, entry));
						if(email != null && email.To != null && email.To.Trim().Length > 0)
							EmailQueueProvider.Instance().QueueEmail(email);
					}
				}
			}
		}

		private static FormatList CreateEntryCommentFormatList(Folder folder, EntryComment post, Entry parent)
		{
			FormatList list = new FormatList();
			CSContext cntx = CSContext.Current;
			list.Add("FolderName", folder.Name);
			list.Add("FolderLink", Globals.FullPath(FileGalleryUrls.Instance().ViewFolder(folder.ApplicationKey)));

			if (!Globals.IsNullorEmpty(post.DisplayName))
				list.Add("PostedBy", post.DisplayName); 

			list.Add("PostedDate", post.UserTime.ToString("f"));
			list.Add("Title", post.Subject);
			list.Add("adminemailfrom", string.Format( ResourceManager.GetString("AutomatedEmail").Trim(), cntx.SiteSettings.SiteName, cntx.SiteSettings.AdminEmailAddress));

			if(parent != null)
			{
				list.Add("EntryLink", Globals.FullPath(FileGalleryUrls.Instance().ViewEntry(folder.ApplicationKey,parent.PostID) ));
				list.Add("EntryName",parent.Subject);
				list.Add("SubscriptionLink", Globals.FullPath(FileGalleryUrls.Instance().ViewEntry(folder.ApplicationKey,parent.PostID) ));
			}

			if (post.IsApproved)
				list.Add("PublishedStatus", ResourceManager.GetString("Post_IsApproved"));
			else
				list.Add("PublishedStatus", ResourceManager.GetString("Post_NotApproved"));

			return list;
		}

		private static FormatList CreateEntryFormatList(Folder folder, Entry entry)
		{
			FormatList list = new FormatList();
			CSContext cntx = CSContext.Current;
			list.Add("FolderName", folder.Name);
			list.Add("FolderLink", Globals.FullPath(FileGalleryUrls.Instance().ViewFolder(folder.ApplicationKey)));

			list.Add("PostedBy", entry.DisplayName); 
			list.Add("PostedDate", entry.UserTime.ToString("f"));
			list.Add("EntryName", entry.Subject);
			list.Add("EntryLink", Globals.FullPath(FileGalleryUrls.Instance().ViewEntry(folder.ApplicationKey,entry.PostID) ));
			list.Add("adminemailfrom", string.Format( ResourceManager.GetString("AutomatedEmail").Trim(), cntx.SiteSettings.SiteName, cntx.SiteSettings.AdminEmailAddress));

			if (entry.IsApproved)
				list.Add("PublishedStatus", ResourceManager.GetString("Post_IsApproved"));
			else
				list.Add("PublishedStatus", ResourceManager.GetString("Post_NotApproved"));

			return list;
		}

		private static MailMessage CreateEntryCommentMessage(Post post, User user, FormatList formats)
		{
			MailMessage email = GenericEmail("FileGalleryCommentNotification",user,null,null,false,user.EnableHtmlEmail);

			if(email == null)
				return null;

			email.From = formats.Format(email.From);
			email.Body = formats.Format(email.Body);
			email.Subject = formats.Format(email.Subject);
			email.To = user.Email;
			
			string postbody = null;

			// if the user doesn't want HTML and the post is HTML, then strip it
			if (!user.EnableHtmlEmail && post.PostType == PostContentType.HTML) 
				postbody = Emails.FormatHtmlAsPlainText(post.RenderedBody(PostTarget.Other));
			else
				postbody =  post.RenderedBody(PostTarget.Other);
				
			postbody.Trim();

			email.Body = FormatList.Format(email.Body, "postbody", postbody);
		
			return email;
		}


		private static MailMessage CreateFolderCommentNotification(Post post, User user, FormatList formats)
		{
			MailMessage email = GenericEmail("FileGalleryNewComment", user, null, null, false, user.EnableHtmlEmail);

			if(email == null)
				return null;

			email.From = formats.Format(email.From);
			email.Body = formats.Format(email.Body);
			email.Subject = formats.Format(email.Subject);
			email.To = user.Email;
			
			string postbody = null;

			// if the user doesn't want HTML and the post is HTML, then strip it
			if (!user.EnableHtmlEmail && post.PostType == PostContentType.HTML) 
				postbody = Emails.FormatHtmlAsPlainText(post.RenderedBody(PostTarget.Other));
			else
				postbody =  post.RenderedBody(PostTarget.Other);
				
			postbody.Trim();

			email.Body = FormatList.Format(email.Body, "postbody", postbody);
		
			return email;
		}


		private static MailMessage CreateFolderEntryNotification(Post post, User user, FormatList formats)
		{
			MailMessage email = GenericEmail("FileGalleryNewEntry", user, null, null, false, user.EnableHtmlEmail);

			if(email == null)
				return null;

			email.From = formats.Format(email.From);
			email.Body = formats.Format(email.Body);
			email.Subject = formats.Format(email.Subject);
			email.To = user.Email;
			
			string postbody = null;

			// if the user doesn't want HTML and the post is HTML, then strip it
			if (!user.EnableHtmlEmail && post.PostType == PostContentType.HTML) 
				postbody = Emails.FormatHtmlAsPlainText(post.RenderedBody(PostTarget.Other));
			else
				postbody =  post.RenderedBody(PostTarget.Other);
				
			postbody.Trim();

			email.Body = FormatList.Format(email.Body, "postbody", postbody);
		
			return email;
		}


	}
}
