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

using System.Collections;

namespace CommunityServer.Components
{
	/// <summary>
	/// The base syndication writer for a RSS feed.
	/// </summary>
	public class SearchRssWriter : BaseSyndicationWriter
	{
        private string baseGuid = null;
        private string title = null;
		/// <summary>
		/// Constructs a writer for an RSS feed.
		/// </summary>
		/// <param name="posts">The entries to write as part of the feed.</param>
		public SearchRssWriter(ArrayList posts, Section s, string baseUrl, string title) : base(posts,s, baseUrl)
		{
            this.title = title;
            baseGuid = CSContext.Current.SiteSettings.PublicSiteKey.ToString();
            //this.StyleSheet = null;
		}

		protected virtual string Title
		{
			get
			{
				return title;
			}
		}


        protected override string EnclosureUrl(PostAttachmentMetaData pa)
        {
            return null;
        }

        
		/// <summary>
		/// Build the feed.
		/// </summary>
		protected override void Build()
		{
            if(!Globals.IsNullorEmpty(this.StyleSheet))
            {
                this.WriteStyleSheet(StyleSheet);
            }

			StartDocument();
			SetNamespaces();
			StartChannel();
			WriteChannel();
			foreach(IndexPost p in this.posts)
			{
				StartItem();
				WriteItem(p);
				EndItem();
			}
			EndChannel();
			EndDocument();
		}

		/// <summary>
		/// Output the rss base element.
		/// </summary>
		protected virtual void StartDocument()
		{
			this.WriteStartElement("rss");
			this.WriteAttributeString("version", "2.0");
		}

		/// <summary>
		/// Output the RSS namespaces used in the feed.
		/// </summary>
        protected virtual void SetNamespaces()
        {
            this.WriteAttributeString("xmlns:dc", "http://purl.org/dc/elements/1.1/");
            this.WriteAttributeString("xmlns:slash", "http://purl.org/rss/1.0/modules/slash/");
			this.WriteAttributeString("xmlns:wfw", "http://wellformedweb.org/CommentAPI/");
        }


		/// <summary>
		/// Output the channel element.
		/// </summary>
		protected void StartChannel()
		{
			this.WriteStartElement("channel");
		}

		/// <summary>
		/// Output the header contents of the channel element.
		/// </summary>
		protected void WriteChannel()
		{
		    BuildChannel(Globals.GetSiteUrls().EmptySearchAdvanced + CSContext.Current.CurrentUri.Query, this.Title);
		}
//		{
//			//BuildChannel(config.FullyQualifiedUrl, config.SubTitle, config.Language);
//		}

        protected void BuildChannel(string link, string description)
        {
            BuildChannel(link,description,"en-US");
        }

		/// <summary>
		/// Actually build the contents of the channel element.
		/// </summary>
		/// <param name="link">The link element of the channel.</param>
		/// <param name="description">The description element of the channel.</param>
		/// <param name="lang">The language element of the channel.</param>
		protected void BuildChannel(string link, string description, string lang)
		{
			this.WriteElementString("title", Title);			
			this.WriteElementString("link", FormatUrl(link));
			this.WriteElementString("description", description);
			this.WriteElementString("dc:language", lang);
			this.WriteElementString("generator", SiteStatistics.CommunityServerVersionVersionInfo);
		}

		/// <summary>
		/// Output an item element.
		/// </summary>
		protected void StartItem()
		{
			this.WriteStartElement("item");
		}

        protected string BuildLink(IndexPost p)
        {
            return string.Empty;
        }

        protected virtual void PostComments(IndexPost p)
        {
            //this.WriteElementString("slash:comments", p.Replies.ToString());
        }

        protected virtual void WritePostEnclosure(IndexPost p)
        {
//            PostAttachmentMetaData pa = p.Attachment;
//            if(pa != null)
//            {
//                this.WriteStartElement("enclosure");
//                this.WriteAttributeString("url",EnclosureUrl(pa));
//                this.WriteAttributeString("length",pa.Length.ToString());
//                this.WriteAttributeString("type",pa.ContentType);
//                this.WriteEndElement();
//            }
        }

        

        protected virtual void PostBody(IndexPost p)
        {
            this.WriteElementString("description",p.RenderedBody(PostTarget.Syndication));
            
        }

        protected virtual string WritePubDate(IndexPost p)
        {
            return p.PostDate.ToUniversalTime().ToString("r");
        }

		protected virtual string GetUserName(IndexPost p)
		{
			return p.UserName;
		}

		/// <summary>
		/// Actually write the contents of an item element.
		/// </summary>
		protected virtual void WriteItem(IndexPost p)
		{
			// core
			this.WriteElementString("title", p.Title);
			// core
			this.WriteElementString("link", FormatUrl(p.Url));
			
			// core
            ;
			this.WriteElementString("pubDate",WritePubDate(p));
            
            this.WriteStartElement("guid");
            this.WriteAttributeString("isPermaLink","false");
            this.WriteString(string.Format("{0}:{1}",baseGuid,p.PostID));
            this.WriteEndElement();

            this.WriteElementString("dc:creator",GetUserName(p));

			//this.WriteElementString("guid", BuildLink(p));

            //PostComments(p);
            PostBody(p);

//            if(p.HasAttachment)
//                WritePostEnclosure(p);
                




//			// core
//			this.WriteElementString(
//				"description", // Tag
//				string.Format(
//					"{0}{1}", // tag def
//					entry.SyndicateDescriptionOnly ? entry.Description : entry.Body,  //use desc or full post
//					(UseAggBugs && settings.Tracking.EnableAggBugs) ? TrackingUrls.AggBugImage(uformat.AggBugkUrl(entry.EntryID)) : null //use aggbugs
//				)
//			);

		}

		/// <summary>
		/// Write the end tag for an item element.
		/// </summary>
		protected void EndItem()
		{
			this.WriteEndElement();
		}

		/// <summary>
		/// Write the end tag for a channel element.
		/// </summary>
		protected void EndChannel()
		{
			this.WriteEndElement();
		}

		/// <summary>
		/// Write the end tag for a rss element.
		/// </summary>
		protected void EndDocument()
		{
			this.WriteEndElement();
		}
	}
}
