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

using System;
using System.Web;
using System.Web.Caching;
using System.IO;
using System.Xml;

// CS
using CommunityServer.Components;
using CommunityServer.Galleries.Components;

namespace CommunityServer.Galleries.Components
{
	/// <summary>
	/// Summary description for CommentHandler.
	/// </summary>
	public class RssCommentHandler : IHttpHandler
	{
		public RssCommentHandler() { }
 
		#region IHttpHandler Members
 
		public void ProcessRequest(HttpContext context)
		{
			HttpRequest request = context.Request;
			if(request.RequestType == "POST" && request.ContentType == "text/xml")
			{
				XmlDocument doc = new XmlDocument();
				doc.Load(request.InputStream);
 
				User user = Users.GetUser();
				int postID = int.Parse(request.QueryString["postid"]); // getPostIDFromUrl(request.RawUrl);
 
				GalleryPost commentedEntry = GalleryPosts.GetPicture(postID);
				Gallery gallery = commentedEntry.Section as Gallery;
				 
				Permissions.AccessCheck(gallery, Permission.View, user);
 
				// if comments aren't enabled, throw an http forbidden exception
				if (!gallery.EnableCommentsDefault)
				{
					throw new HttpException(403, "Comments are not enabled.");
				}

				// if rss comments aren't enabled, throw an http forbidden exception
				if (!gallery.EnableRssCommentsDefault)
				{
					throw new HttpException(403, "Rss Comments are not enabled.");
				}
 
				string name = doc.SelectSingleNode("//item/author").InnerText;
				if(name.IndexOf("<") != -1)
				{
					name = name.Substring(0,name.IndexOf("<"));
				}
 
				GalleryPost post = new GalleryPost();
				post.SubmittedUserName = name.Trim();
				post.GalleryPostType = GalleryPostType.Comment;
				post.SectionID = gallery.SectionID;
				post.ParentID = postID;
				post.Body = doc.SelectSingleNode("//item/description").InnerText;
				post.Subject = doc.SelectSingleNode("//item/title").InnerText;
				post.TitleUrl = checkForUrl(doc.SelectSingleNode("//item/link").InnerText);
				post.IsApproved = !gallery.IsPostModerated(commentedEntry, user); ;
				post.PostDate = DateTime.Now;
				post.UserTime = DateTime.Now;
				GalleryPosts.Add(post, user);
			}
			else
			{
                       
			}
		}

		private string checkForUrl(string text)
		{
			if (text == null || text.Trim().Length == 0)
			{
				return string.Empty;
			}

			if (text.Trim().ToLower().StartsWith("http://") || text.Trim().ToLower().StartsWith("https://"))
			{
				return text;
			}
			return "http://" + text;
		}
 
		//private int getPostIDFromUrl(string uri)
		//{
		//    try
		//    {
		//        return Int32.Parse(getReqeustedFileName(uri));
		//    }
		//    catch (FormatException)
		//    {
		//        throw new ArgumentException("Invalid Post ID.");
		//    }
		//}
 
		//private string getReqeustedFileName(string uri)
		//{
		//    return Path.GetFileNameWithoutExtension(uri);
		//}
 
		public bool IsReusable
		{
			get
			{
				return true;
			}
		}
 
		#endregion
	}
}