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

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

namespace CommunityServer.Files
{
	/// <summary>
	/// Summary description for Emails.
	/// </summary>
	public class FileGalleryEmails : Emails
	{

		public static void SendToFriend(string applicationKey, int categoryID, int pictureID, string to, string from, string subject, string body)
		{
			// Get the information
			Folder folder = Folders.GetFolder(applicationKey);
			PostCategory category = null;
			Entry entry = Entries.GetEntry(folder.SectionID, pictureID);

			if(categoryID != -1)
				category = PostCategories.GetCategory(categoryID, folder.SectionID);

			// Get the email template we're going to use
			MailMessage email = GenericEmail("GallerySendToFriend", new User(), null, null);

			// Replace the generic email items
			email.From = GenericEmailFormatter(email.From, null, null);
			email.Body = GenericEmailFormatter(email.Body, null, null);
			email.Subject = GenericEmailFormatter(email.Subject, null, null);

			// Input our data
			email.To = to;
			email.From = email.From.Replace("[EmailFrom]", from);

			email.Subject = email.Subject.Replace("[Subject]", subject);

			// In case we have no category
			if(category != null)
				email.Body = email.Body.Replace("[CategoryName]", category.Name);
			else
				email.Body = email.Body.Replace("[CategoryName]", FileGalleryResourceManager.GetString( "NoTitle" ));

			email.Body = email.Body.Replace("[EmailFrom]", from);
			email.Body = email.Body.Replace("[PictureName]", entry.Subject);
			email.Body = email.Body.Replace("[PictureAddress]", Globals.HostPath(CSContext.Current.Context.Request.Url) + FileGalleryUrls.Instance().ViewEntry(applicationKey, pictureID));
			email.Body = email.Body.Replace("[Comments]", body);

			EnqueuEmail(email);
		}

		public static void NewComment(string applicationKey, int categoryID, int pictureID, Post comment)
		{
			// Get the information
			Folder folder = Folders.GetFolder(applicationKey);
			Entry entry = Entries.GetEntry(folder.SectionID, pictureID);
			User user = Users.GetUser(entry.AuthorID, false);

			// Make sure we can email the user
			if(!CanSend(user))
				return;

			// Get the email template we're going to use
			MailMessage email = GenericEmail("GalleryNewComment", user, null, null);

			// Replace the generic email items
			email.From = GenericEmailFormatter(email.From, null, null);
			email.Body = GenericEmailFormatter(email.Body, null, null);
			email.Subject = GenericEmailFormatter(email.Subject, null, null);

			// Gallery name
			email.Subject = email.Subject.Replace("[GalleryName]", folder.Name);
			email.Body = email.Body.Replace("[GalleryName]", folder.Name);

			// If we have a categoryID, do some replacing.
			if(categoryID != -1)
			{
				PostCategory category = PostCategories.GetCategory(categoryID, folder.SectionID);
				email.Subject = email.Subject.Replace("[CategoryName]", category.Name);
				email.Body = email.Body.Replace("[CategoryName]", category.Name);
			}
			else
			{
				email.Subject = email.Subject.Replace("[CategoryName]", FileGalleryResourceManager.GetString( "None" ));
				email.Body = email.Body.Replace("[CategoryName]", FileGalleryResourceManager.GetString( "None" ));
			}

			// Input our data
			email.To = user.Email;
			if(entry.Subject != string.Empty)
				email.Subject = email.Subject.Replace("[PictureName]", entry.Subject);
			else
				email.Subject = email.Subject.Replace("[PictureName]", FileGalleryResourceManager.GetString( "NoTitle" ) );

			// Fill the body
			if(entry.Body != string.Empty)
				email.Body = email.Body.Replace("[PictureName]", entry.Subject);
			else
				email.Body = email.Body.Replace("[PictureName]", FileGalleryResourceManager.GetString( "NoTitle" ));

			// Enter in user data
			if(!comment.User.IsAnonymous)
			{
				email.From = email.From.Replace("[EmailName]", comment.User.DisplayName);
				email.Body = email.Body.Replace("[EmailName]", comment.User.DisplayName);
			}
			else
			{
				email.From = email.From.Replace("[EmailName]", string.Format("{0} ({1})", comment.GetExtendedAttribute("Name"), comment.GetExtendedAttribute("Email")));
				email.Body = email.Body.Replace("[EmailName]", string.Format("{0} ({1})", comment.GetExtendedAttribute("Name"), comment.GetExtendedAttribute("Email")));
			}

			email.Body = email.Body.Replace("[PictureAddress]", Globals.HostPath(CSContext.Current.Context.Request.Url) + FileGalleryUrls.Instance().ViewEntry(applicationKey, pictureID));
			email.Body = email.Body.Replace("[Comment]", comment.Body);

			EnqueuEmail(email);
		}

	}
}
