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

using System.Collections;
using CommunityServer.Components;

namespace CommunityServer.Files.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;

            //Folder f = Folders.GetFolder(pa.SectionID);

            return this.BaseUrl + FileGalleryUrls.Instance().Download(pa.PostID);
        }

        protected override void PostBody(Post p)
        {
            Entry fgp = p as Entry;
            if(fgp == null)
                base.PostBody(p);
            else
            {
                string desc = null;
//                if(fgp.SyndicateExcerpt)
//                {
//                    if(fgp.HasExcerpt)
//                    {
//                        desc = fgp.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(Folders.GetFolder(fgp.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);
        }


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

            }
            
            BuildChannel(url,desc);
        }
        protected override string BuildLink(Post p)
        {
            return FileGalleryUrls.Instance().ViewEntry(p.Section.ApplicationKey, p.PostID);
        }

		protected override string GetUserName(Post p)
		{
			Entry fgp = p as Entry;
			if (fgp == null)
				return base.GetUserName(p);
			else
				return Globals.HtmlDecode(fgp.DisplayName);
		}

	}
}
