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

using System.Collections;
using CommunityServer.Components;

namespace CommunityServer.Blogs.Components
{
	/// <summary>
	/// Summary description for AggregateRssWriter.
	/// </summary>
	public class AggregateRssWriter : BaseRssWriter
	{
        protected Group CurrentGroup = null;

        protected bool HasGroup
        {
            get
            {
                return CurrentGroup != null;
            }
        }
        public AggregateRssWriter(ArrayList posts, Group group, string baseUrl):base(posts,null, baseUrl)
        {
			CurrentGroup = group;
        }

        protected override string Title
        {
            get
            {
                return CSContext.Current.SiteSettings.SiteName;
            }
        }

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

            Weblog blog = Weblogs.GetWeblog(pa.SectionID);

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

        protected override void PostBody(Post p)
        {
            WeblogPost wp = p as WeblogPost;
            if(wp == null)
                base.PostBody(p);
            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 = p.RenderedBody(PostTarget.Syndication);
                }
            
				//agg view on the aggregate post?

				if(Weblogs.GetWeblog(wp.SectionID).EnableAggBugs)
					desc = string.Format("{0}<img src=\"{1}\" width=\"1\" height=\"1\">",desc,FormatUrl(Globals.GetSiteUrls().AggView(wp.PostID)));

                base.WriteElementString("description",desc);
            }
           
        }


        protected override void WriteItem(Post p)
        {
            base.WriteItem (p);
            BaseWeblogRssWriter.AddCategoriesToWriter(p,p.Section as Weblog,this.BaseUrl,this);
        }


        protected override void WriteChannel()
        {
            string url = null;
            string desc = null;
            if(HasGroup)
            {
                url = BlogUrls.Instance().GroupHome(CurrentGroup.GroupID);
                desc = CurrentGroup.Name;
            }
            else
            {
                url = BlogUrls.Instance().Home;
                desc = CSContext.Current.SiteSettings.SiteDescription;

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

		/// <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);
		}
	}
}
