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

using System;
using System.Collections;
using CommunityServer.Components;

namespace CommunityServer.Blogs.Components
{
	/// <summary>
	/// Creates the XML for an Atom feed based on a single weblog
	/// </summary>
	public class WeblogAtomWriter : BaseAtomWriter
	{
		public WeblogAtomWriter(ArrayList posts, Section s, string baseUrl) : base(posts,s, baseUrl)
		{
		
		}

		/// <summary>
		/// Helper to cast the CurrenSection a Welbog
		/// </summary>
		protected Weblog CurrentWeblog
		{
			get{ return this.CurrentSection as Weblog;}
		}


		/// <summary>
		/// Returns the time of the last modified post. Might be able to move this to the base
		/// </summary>
		protected override DateTime Modified
		{
			get
			{
				if(posts != null && posts.Count > 0)
				{
					return ((WeblogPost) (posts[posts.Count -1])).UserTime;
				}
				else
					return new DateTime(2004,1,1);
			}
		}

		/// <summary>
		/// Return a link to the weblog home page
		/// </summary>
		protected override string HomeLink
		{
			get { return  FormatUrl(BlogUrls.Instance().HomePage(CurrentWeblog.ApplicationKey)); }
		}

		/// <summary>
		/// Writes the channel. This is an abstract member and must be implemented
		/// </summary>
		protected override void WriteChannel()
		{
			BuildChannel(CurrentWeblog.Name,HomeLink,CurrentWeblog.Description);
		}

		/// <summary>
		/// Blog store a preferred language, so lets use it
		/// </summary>
		protected override string Language
		{
			get
			{
				return CurrentWeblog.DefaultLanguage;
			}
		}

        protected override string FeedId
        {
            get {return SiteGuid + ":" + this.CurrentWeblog.SectionID.ToString();}
        }

        protected override string SelfUrl
        {
            get { return this.BaseUrl + BlogUrls.Instance().Atom(this.CurrentWeblog.ApplicationKey);}
        }

        protected override string EnclosureUrl(PostAttachmentMetaData pa)
        {
            if(pa.IsRemote)
                return pa.FileName;

            return this.BaseUrl + BlogUrls.Instance().AttachmentUrl(this.CurrentWeblog,pa);
        }


		/// <summary>
		/// Enables Excerpt only syndiation as well as AggBugs
		/// </summary>
		/// <param name="p"></param>
		/// <returns></returns>
		protected override string PostBody(Post p)
		{
			WeblogPost wp = p as WeblogPost;
			if(wp == null)
				return p.RenderedBody(PostTarget.Syndication);
			else
			{
				string desc = null;
				if(wp.SyndicateExcerpt)
				{
					if(wp.HasExcerpt)
					{
						desc = wp.Excerpt;
					}
					else
					{
						desc = Formatter.RemoveHtml(p.RenderedBody(PostTarget.Syndication),500) ;
					}

					desc = string.Format("{0}...(<a href=\"{1}\">read more</a>)",desc,FormatUrl(BuildLink(p)));
				}
				else
				{
					desc = wp.RenderedBody(PostTarget.Syndication);
				}
            
				if(CurrentWeblog.EnableAggBugs)
					desc = string.Format("{0}<img src=\"{1}\" width=\"1\" height=\"1\">",desc,FormatUrl(Globals.GetSiteUrls().AggView(wp.PostID)));

				return desc;
			}
           
		}


		//ScottW: Need to include the bloggers timzezone for other Atom date elements
		protected override void DateElements(Post p)
		{
			base.DateElements (p);
						//this.WriteElementString("issued", W3UTC(p.PostDate.AddHours((-1) * CurrentWeblog.t), TimeZone(p.User.Profile.Timezone)));
		}


		/// <summary>
		/// Builds a link to the post. This item is abstract and must be implemented
		/// </summary>
		/// <param name="p"></param>
		/// <returns></returns>
		protected override string BuildLink(Post p)
		{
			return FormatUrl(BlogUrls.Instance().Post(p as WeblogPost, CurrentWeblog));
		}
	

		/// <summary>
		/// Adds tags/categories to Entry element
		/// </summary>
		/// <param name="p"></param>
		protected override void ExtendendElements(Post p)
		{
			// Write Categories (tags)
			WeblogPost post = p as WeblogPost;
			if(post != null && post.HasCategories)
			{
				ArrayList postTags = WeblogPosts.GetCategoryTags(post);
				if(postTags != null && postTags.Count > 0)
				{
					BlogUrls urls = BlogUrls.Instance();
					foreach(CategoryTag tag in postTags)
					{
						this.WriteStartElement("category");
						this.WriteAttributeString("term", tag.Name);
						this.WriteAttributeString("scheme", FormatUrl(urls.TagsBrowser(CurrentWeblog.ApplicationKey, new string[] {tag.Name})));
						this.WriteEndElement();
					}
				}
			}

		}


	}
}
