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

using System.ComponentModel;
using System.Web.UI.WebControls;
using CommunityServer.Components;

namespace CommunityServer.Controls
{
	/// <summary>
	/// Displays an RSS feed icon linked to the feed, along with the feed title linked to the feed source
	/// </summary>
	public class FeedLink : HyperLink
	{
		//private HtmlImage image;
        private RssButton rssButton;

		#region Public Properties

	    /// <summary>
	    /// The Icon to show as the link to the feed
	    /// </summary>
		[DefaultValue( "The FeedIcon in SiteUrls" )]
		public virtual string FeedImageUrl
		{
			get{ return rssButton.ImageUrl; }
            set { rssButton.ImageUrl = value; }
		}

	    /// <summary>
	    /// The xml feed link
	    /// </summary>
		[DefaultValue( "" )]
		public virtual string FeedNavigateUrl
		{
			get{ return rssButton.NavigateUrl; }
            set { rssButton.NavigateUrl = value; }
		}

	    /// <summary>
	    /// Includes this feed in the header for auto discovery
	    /// This work is done by the rss button control
	    /// </summary>
        [DefaultValue(true)]
        public virtual bool IncludeAutodiscovery
        {
            get { return rssButton.IncludeAutodiscovery; }
            set { rssButton.IncludeAutodiscovery = value; }
        }
	    
	    /// <summary>
	    /// The title to display for the feed in IE7 Auto Feed Discovery
	    /// </summary>
	    public override string Text
	    {
            get{ return base.Text; }
            set { rssButton.AutoDiscoveryTitle = base.Text = value; }
        }

		#endregion

		#region Constructor

        public FeedLink()
		{
            rssButton = new RssButton();
            rssButton.ImageUrl = Globals.GetSiteUrls().FeedIcon();
            rssButton.IncludeAutodiscovery = true;
            rssButton.AutoDiscoveryTitle = base.Text;
		}

	    public FeedLink(FeedLinkItem feedLinkItem)
	    {
            rssButton = new RssButton();
            rssButton.ImageUrl = Globals.GetSiteUrls().FeedIcon();
            rssButton.IncludeAutodiscovery = true;
            rssButton.AutoDiscoveryTitle = feedLinkItem.Title;
            rssButton.NavigateUrl = feedLinkItem.FeedUrl;
            this.NavigateUrl = feedLinkItem.SiteUrl;
        }
		#endregion

		protected override void Render(System.Web.UI.HtmlTextWriter writer)
		{
            if (rssButton.NavigateUrl == string.Empty && rssButton.Visible)
                rssButton.Style.Add("visibility", "hidden");
		    //write the feed icon
            rssButton.RenderControl(writer);
		    
		    //give us a space
            writer.Write("&nbsp;");
		    
		    //write the main link
            base.Render(writer);
		}

	    public void SetFromFeedLinkItem(FeedLinkItem feedLinkItem)
	    {
            rssButton = new RssButton();
            rssButton.ImageUrl = Globals.GetSiteUrls().FeedIcon();
            rssButton.IncludeAutodiscovery = true;
            rssButton.AutoDiscoveryTitle = feedLinkItem.Title;
            rssButton.NavigateUrl = feedLinkItem.FeedUrl;
            this.NavigateUrl = feedLinkItem.SiteUrl;
            this.Text = feedLinkItem.Title;
        }
	}
}
