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

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

namespace CommunityServer.Blogs.Components
{
	/// <summary>
	/// Summary description for BaseWeblogRssWriter.
	/// </summary>
	public abstract class BaseWeblogRssWriter : BaseRssWriter
	{
        public BaseWeblogRssWriter(ArrayList posts, Section s, string baseUrl):base(posts,s,baseUrl)
        {
            
        }

		/// <summary>
		/// First looks for a custom rss.xsl in the theme directory; then defaults back to the 
		/// passed in path
		/// </summary>
		/// <param name="path"></param>
		protected override void WriteStyleSheet(string path)
		{
			string themePath = string.Format("{0}Blogs/{1}/rss.xsl", SiteUrls.Instance().Locations["themes"], CurrentWeblog.Theme);

            CSContext context = CSContext.Current;

			if(File.Exists(context.MapPath(themePath)))
			{
				string styleSheetURL = Globals.FullPath(this.BaseUrl, themePath) ;
				this.WriteRaw(string.Format(styleElement,styleSheetURL)) ;
			}
			else
			{
				base.WriteStyleSheet(path);
			}
		}
		
		protected override string EnclosureUrl(PostAttachmentMetaData pa)
        {
            if(pa.IsRemote)
                return pa.FileName;

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

		protected bool EnableAggBugs
		{
			get{ return CurrentWeblog.EnableAggBugs;}
		}

        protected Weblog CurrentWeblog
        {
            get{ return CurrentSection as Weblog;}
        }

        protected override string BuildLink(Post p)
        {
            return BlogUrls.Instance().Post(p as WeblogPost, CurrentWeblog);
        }

		protected override void WriteItem(Post p)
		{
			base.WriteItem (p);

		    AddCategoriesToWriter(p,CurrentWeblog,this.BaseUrl,this);


		}

	    internal static void AddCategoriesToWriter(Post p, Weblog w, string baseUrl, BaseRssWriter writer)
	    {
	        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)
	                {
	                    writer.WriteStartElement("category");
	                    writer.WriteAttributeString("domain", baseUrl + urls.TagsBrowser(w.ApplicationKey, new string[] {tag.Name}));
						writer.WriteString(tag.Name);
	                    writer.WriteEndElement();
	                }
	            }
	        }
	    }

	    /// <summary>
		/// Override GetuserName to allow the blog specific SubmittedUserName property to be sued
		/// SubmittedUserName is HtmlDecoded because it will be re-encoded when written to XML
		/// </summary>
		protected override string GetUserName(Post p)
		{
			WeblogPost wp = p as WeblogPost;
			if(wp == null)
				return base.GetUserName(p);
			else
				return  Globals.HtmlDecode(wp.DisplayName);
		}


	}
}
