//------------------------------------------------------------------------------
// <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.Galleries.Components
{
	/// <summary>
	/// Summary description for GalleryEmails.
	/// </summary>
	internal class GalleryEmails : Emails
	{
		private GalleryEmails()
		{
			
		}

		public static void GalleryTracking(GalleryPost post, GalleryPost parent)
		{
			if (!post.IsPicture && parent == null)
				return;

			SectionTracking(post, new EnquePostEmails(CreateGalleryPost), CreatePostFormatList(post.Gallery, post, parent) );
		}

		public static void GalleryFeedbackNotification(GalleryPost post, GalleryPost parent)
		{
			if (!post.IsPicture && parent == null)
				return;

			if (parent.EnableAllOwnerNotification && !Globals.IsNullorEmpty(post.Gallery.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 post.Gallery.OwnerArray)
				{
					User owner = Users.FindUserByUsername(username);
					if ( !Globals.IsNullorEmpty(owner.Email) && (owner.Username != post.User.Username) )
					{
						MailMessage email = CreateGalleryFeedbackNotification(post, owner, CreatePostFormatList(post.Gallery, post, parent) );

						if(email != null && email.To != null && email.To.Trim().Length > 0)
							EmailQueueProvider.Instance().QueueEmail(email);
					}
				}
			}
			else
			{
				// Send notification email to the author of the thread if he/she was not the one who posted the feedback item.
				User owner = Users.FindUserByUsername(parent.Username);
				if ( !Globals.IsNullorEmpty(owner.Email) && (owner.Username != post.User.Username) )
				{
					MailMessage email = CreateGalleryFeedbackNotification(post, owner, CreatePostFormatList(post.Gallery, post, parent) );

					if(email != null && email.To != null && email.To.Trim().Length > 0)
						EmailQueueProvider.Instance().QueueEmail(email);
				}
			}

		}

		private static FormatList CreatePostFormatList(Gallery gallery, GalleryPost post, GalleryPost parent)
		{
			FormatList list = new FormatList();
			list.Add("blogName", gallery.Name);
			
			list.Add("blogUrl", Globals.FullPath(GalleryUrls.Instance().ViewGallery(gallery.ApplicationKey)));

			list.Add("postedBy", post.DisplayName);
			list.Add("postedDate", post.UserTime.ToString("f"));
			list.Add("Title", post.Subject);
			list.Add("subscriptionLink", Globals.FullPath(GalleryUrls.Instance().EmailSubscriptions(gallery.ApplicationKey)));
			

			if(parent != null)
			{
				list.Add("postLink", Globals.FullPath(GalleryUrls.Instance().ViewPicture(gallery.ApplicationKey,parent) ));
				list.Add("titleUrl", post.TitleUrl);
				list.Add("originalPostName",parent.Subject);
			}
			else
			{
				list.Add("postLink", Globals.FullPath(GalleryUrls.Instance().ViewPicture(gallery.ApplicationKey,post)));
			}

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

			return list;
		}

		private static MailMessage CreateGalleryPost(Post post, User user, FormatList formats)
		{
			string emailType = null;
			if(post.PostLevel != 0)
			{
				emailType = "GalleryComment";
			}
			else
			{
				emailType = "GalleryNewPost";
			}
			MailMessage email = GenericEmail(emailType,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 CreateGalleryFeedbackNotification(Post post, User user, FormatList formats)
		{

			MailMessage email = GenericEmail("GalleryFeedbackNotification", 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;
		}




	}
}
