//------------------------------------------------------------------------------
// <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 CommunityServer.Blogs.Components;
using CommunityServer.Components;
using CommunityServer.Controls;

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

        private ITrackedThread threadSubButton;
        private HyperLink CommentRssLink;
        private HyperLink RegisterLink;


	    /// <summary>
	    /// Override this method to attach templated or external skin controls to local references.
	    /// </summary>
	    /// <remarks>
	    /// This will only be called if the non-default skin is used.
	    /// </remarks>
	    protected override void AttachChildControls()
	    {
	        threadSubButton = FindControl("BlogThreadButton") as ITrackedThread;
            CommentRssLink = FindControl("CommentRssLink") as HyperLink;
            RegisterLink = FindControl("RegisterLink") as HyperLink;
	        Control Anonymous = FindControl("Anonymous");
            Control Registered = FindControl("Registered");

            if(Anonymous != null)
                Anonymous.Visible = CurrentUser.IsAnonymous;

            if(Registered != null)
                Registered.Visible = !CurrentUser.IsAnonymous;

	    }

        public override void DataBind()
        {
            base.DataBind ();
			CSContext cntx = CSContext.Current;

            if(CurrentPost != null && CurrentWeblog.EnableNewComments(CurrentPost,CurrentUser))
            {
                if(threadSubButton != null)
                {

					
                    if(!CurrentUser.IsAnonymous)
                    {
                        threadSubButton.IsTracked = CurrentPost.GetIsTracked(CurrentUser.UserID);
                        threadSubButton.ThreadID = CurrentPost.ThreadID;
                    }
                    else
                    {
                        threadSubButton.Visible = false;
                    }
                }

				if(RegisterLink != null)
				{
					RegisterLink.Text = ResourceManager.GetString("Weblog_DetailedPostSubscription_Here");
					RegisterLink.NavigateUrl = Globals.GetSiteUrls().UserRegister;
				}

				if(CommentRssLink != null)
				{
					bool isAvailableToPublic = Permissions.ValidatePermissions(CurrentWeblog, Permission.View, Users.GetAnonymousUser(true));

					if (isAvailableToPublic)
						CommentRssLink.NavigateUrl = BlogUrls.Instance().CommentRss(this.CurrentWeblog.ApplicationKey, CurrentPost.PostID);
					else if (cntx.SiteSettings.EnableSecureSyndication)
						CommentRssLink.NavigateUrl = SiteUrls.FormatUrlWithUserNameToken(BlogUrls.Instance().CommentRss(this.CurrentWeblog.ApplicationKey, CurrentPost.PostID), cntx.User, true);
					else
						CommentRssLink.Visible = false;

					CommentRssLink.Text = ResourceManager.GetString("Weblog_DetailedPostSubscription_CommentRss");
				}
            }
            else
                this.Visible = false;
        }


        private WeblogPost _currentPost;
        /// <summary>
        /// Property CurrentPost (WeblogPost)
        /// </summary>
        public WeblogPost CurrentPost
        {
            get {  return this._currentPost; }
            set {  this._currentPost = value; }
        }

	}
}
