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

using System.Collections;
using System.IO;
using System.Xml;

namespace CommunityServer.Components
{
	/// <summary>
	/// The base class for all syndication (RSS, Atom, etc.) writers.
	/// </summary>
	public abstract class BaseSyndicationWriter : XmlTextWriter
	{
        protected const string styleElement = "<?xml-stylesheet type=\"text/xsl\" href=\"{0}\" media=\"screen\"?>";

        protected string BaseUrl = null;

        private string _styleSheet = Globals.GetSiteUrls().RssStyleSheet();
        /// <summary>
        /// Property StyleSheet (string)
        /// </summary>
        public string StyleSheet
        {
            get {  return this._styleSheet; }
            set {  this._styleSheet = value; }
        }

        protected virtual void WriteStyleSheet(string path)
        {
				this.WriteRaw(string.Format(styleElement,string.Concat(this.BaseUrl,path)));
        }
		/// <summary>
		/// The StringWriter that we'll be using to store the output.
		/// </summary>
		private StringWriter sw = null;

        private Section section;
		/// <summary>
		/// The configuration for the blog this feed is for.
		/// </summary>
        protected Section CurrentSection
        {
            get{return section;}
        }

		/// <summary>
		/// The entries contained in this feed.
		/// </summary>
		protected ArrayList posts;

		/// <summary>
		/// Has the feed been built?
		/// </summary>
		private bool isBuilt = false;

		/// <summary>
		/// Constructs a new BaseSyndicationWriter for a set of entries and a StringWriter.
		/// </summary>
		/// <param name="sw">The StringWriter to use for the feed.</param>
		private BaseSyndicationWriter(ArrayList posts, Section s, string baseUrl, StringWriter sw) : base(sw)
		{
			this.posts = posts;
			this.sw = sw;
			section = s;
            BaseUrl = baseUrl;
			sw.WriteLine("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>");
		}

		/// <summary>
		/// Constructs a new BaseSyndicationWriter for a set of entries.
		/// </summary>
		public BaseSyndicationWriter(ArrayList posts, Section s, string baseUrl) : this(posts, s, baseUrl, new StringWriter())
		{
		}

		/// <summary>
		/// Get the XML feed.
		/// </summary>
		public string GetXml()
		{
			if (!isBuilt)
			{
				Build();
				isBuilt = true;
			}
			return this.sw.ToString();
		}

		public override string ToString()
		{
			return GetXml();
		}

        protected abstract string EnclosureUrl(PostAttachmentMetaData pa);

		/// <summary>
		/// Whether the feed writer should emit webbugs to count aggregator views.
		/// </summary>
		protected virtual bool UseAggBugs
		{
			get { return false; }
		}

		/// <summary>
		/// Whether the feed writer should emit comment information for entries.
		/// </summary>
		protected virtual bool AllowComments
		{
			get { return true; }
		}

		/// <summary>
		/// Builds the feed.
		/// </summary>
		protected abstract void Build();

        /// <summary>
        /// Usually used to fully qualify Urls
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        protected virtual string FormatUrl(string url)
        {
            if (url.IndexOf("http://", 0) >= 0) { return url; }
            return string.Concat(BaseUrl, url);
        }
	}
}
