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

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using CommunityServer.Blogs.Components;
using CommunityServer.Components;

namespace CommunityServer.Blogs.Controls
{
	/// <summary>
	/// Summary description for Subscriptions.
	/// </summary>
	public class Subscriptions : WeblogThemedControl
	{
		public Subscriptions()
		{
	
		}

		protected override void AttachChildControls()
		{
            //NOTE: CS-Core will ship with this control using Blog Link controls.

			HyperLink rssLink = FindControl("rssLink") as HyperLink;
            if(rssLink != null)
            {
                if(CurrentWeblog.EnableRss)
                {
					if (!Globals.IsNullorEmpty(CurrentWeblog.ExternalFeedUrl))
						rssLink.NavigateUrl = CurrentWeblog.ExternalFeedUrl;
					else
						rssLink.NavigateUrl = BlogUrls.Instance().Rss(this.CurrentWeblog.ApplicationKey);
                    rssLink.Text = ResourceManager.GetString("Weblog_SubscriptionList_Rss");
                }
                else
                    rssLink.Visible = false;
            }

			HyperLink atomLink = FindControl("atomLink") as HyperLink;
            if(atomLink != null)
            {
                if(CurrentWeblog.EnableAtom)
                {
					if (!Globals.IsNullorEmpty(CurrentWeblog.ExternalFeedUrl))
						atomLink.NavigateUrl = CurrentWeblog.ExternalFeedUrl;
					else
						atomLink.NavigateUrl = BlogUrls.Instance().Atom(this.CurrentWeblog.ApplicationKey);
                    atomLink.Text = ResourceManager.GetString("Weblog_SubscriptionList_Atom");
                }
                else
                    atomLink.Visible = false;
            }

			HyperLink email = FindControl("emailLink") as HyperLink;
            if(email != null)
            {
                if(Context.Request.IsAuthenticated)
                {
                    email.NavigateUrl = BlogUrls.Instance().EmailSubscriptions(this.CurrentWeblog.ApplicationKey);
                    email.Text = ResourceManager.GetString("Weblog_SubscriptionList_Email");
                }
                else
                    email.Visible = false;
            }

            HyperLink contact = FindControl("contactLink") as HyperLink;
            if(contact != null)
            {
                contact.Visible = false;
            }
		}

        //If there are no visible links or BaseBlogLinks in
        //this control, we will hide it.
        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender (e);

            bool hide = true;

            foreach(Control skin in this.Controls)
            {
                foreach(Control sc in skin.Controls)
                {
                    if(sc is HyperLink || sc is BaseBlogLink)
                    {
                        if(sc.Visible)
                            hide = false;
                    }
                }
            }

            if(hide)
                this.Visible = false;


        }

	}
}
