//------------------------------------------------------------------------------
// <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 WeblogRssWriter.
    /// </summary>
    public class WeblogRssWriter : BaseWeblogRssWriter
    {
        public WeblogRssWriter(ArrayList posts, Weblog w, string baseUrl):base(posts,w, baseUrl)
        {
			
        }


        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),wp.ExcerptSize) ;
                    }

                    desc = string.Format("{0}...(<a href=\"{1}\">read more</a>)",desc,FormatUrl(BuildLink(p)));
                }
                else
                {
                    desc = wp.RenderedBody(PostTarget.Syndication);
                }
            
				if(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 bool AllowComments
        {
            get
            {
                return base.AllowComments && CurrentWeblog.EnableCommentsDefault;
            }
        }

		protected override void PostComments(Post p)
		{
			base.PostComments (p);
			CSContext context = CSContext.Current;
			bool isAvailableToPublic = Permissions.ValidatePermissions(this.CurrentWeblog, Permission.View, Users.GetAnonymousUser(true));
			
			this.WriteElementString("comments", FormatUrl(BlogUrls.Instance().CommentsPage(CurrentWeblog.ApplicationKey,p.PostID)));

			if (isAvailableToPublic)
				this.WriteElementString("wfw:commentRss", FormatUrl(BlogUrls.Instance().CommentRss(CurrentWeblog.ApplicationKey, p.PostID)));
			else if (context.SiteSettings.EnableSecureSyndication)
				this.WriteElementString("wfw:commentRss", FormatUrl(SiteUrls.FormatUrlWithUserNameToken(BlogUrls.Instance().CommentRss(CurrentWeblog.ApplicationKey, p.PostID), context.User, true)));
			
			if (CurrentWeblog.EnableCommentsDefault && CurrentWeblog.EnableRssCommentsDefault)
			{
				if (isAvailableToPublic)
					this.WriteElementString("wfw:comment", FormatUrl(BlogUrls.Instance().RssComments(CurrentWeblog.ApplicationKey, p.PostID)));
				else
					this.WriteElementString("wfw:comment", FormatUrl(SiteUrls.FormatUrlWithUserNameToken(BlogUrls.Instance().RssComments(CurrentWeblog.ApplicationKey, p.PostID), context.User, true)));
			}
		}

        protected override void WriteChannel()
        {
            BuildChannel(BlogUrls.Instance().HomePage(CurrentSection.ApplicationKey),CurrentSection.Description, CurrentWeblog.DefaultLanguage);
        }


    }
}
