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

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

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

        }

        private PostReplyButton replyButton;
        private BlogRatePost rateButton;
        private ITrackedThread threadSubButton;
		private HyperLink EditLink = null;
		
		protected override void AttachChildControls()
		{
            SetVisibilityIfExists("Message", (Context.Request.QueryString["CommentPosted"] != null));

			replyButton = FindControl("ReplyButton") as PostReplyButton;
			rateButton = FindControl("RatePost") as BlogRatePost;
			threadSubButton = FindControl("BlogThreadButton") as ITrackedThread;
			EditLink = FindControl("EditLink") as HyperLink;

            if(rateButton != null)
			    rateButton.RatingsChanged +=new System.EventHandler(rateButton_RatingsChanged);
			


			//rateButton.Post

		}

        public override void DataBind()
        {
            base.DataBind ();
            if(CurrentPost != null)
			{

					bool enableComments = CurrentWeblog.EnableNewComments(CurrentPost,CurrentUser);

                    if(replyButton != null)
					    replyButton.Visible = enableComments && DisplayCommentLink;

                    if(threadSubButton != null)
					    threadSubButton.Visible = enableComments && !CurrentUser.IsAnonymous;

                    if(rateButton != null)
					rateButton.Visible = !CurrentUser.IsAnonymous && CurrentWeblog.ValidateRatings(CurrentPost);
					
                
					if(enableComments)
					{
                        if(threadSubButton != null)
                        {
                            threadSubButton.IsTracked = CurrentPost.GetIsTracked(CurrentUser.UserID);
                            threadSubButton.ThreadID = CurrentPost.ThreadID;
                        }

                        if(replyButton != null)
						    replyButton.Post = CurrentPost;
					}
                    
                    if(rateButton != null)
					    rateButton.Post = CurrentPost;

                if(EditLink != null)
                {
                    if(Permissions.ValidatePermissions(this.CurrentWeblog,Permission.Post,this.CurrentUser))
                    {
                        EditLink.NavigateUrl = BlogUrls.Instance().PostEditor(this.CurrentWeblog.ApplicationKey, CurrentPost.PostID);
                        EditLink.Text = ResourceManager.GetString("Edit");
                        EditLink.ToolTip = ResourceManager.GetString("Weblog_EntryList_Edit");
                        EditLink.Visible = true;
                    }
                    else
                    {
                        EditLink.Visible = false;
                        SetVisibilityIfExists("Sep3",false);
                    }
                }
	        }
			else
            {
            	this.Controls.Clear();
				this.Visible = false;
            }
        }

		
		protected override void OnPreRender(System.EventArgs e)
		{

				if(replyButton != null && replyButton.Visible == false)
					SetVisibilityIfExists("Sep1",false);
				

				if(threadSubButton != null && threadSubButton.Visible == false)
					SetVisibilityIfExists("Sep3",false);

				if(rateButton != null && rateButton.Visible == false)
					SetVisibilityIfExists("Sep2",false);

			base.OnPreRender (e);

		}

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

        private bool _displayCommentLink = true;

        public bool DisplayCommentLink
        {
            get{return _displayCommentLink;}
            set{_displayCommentLink = value;}
        }


		private void rateButton_RatingsChanged(object sender, System.EventArgs e)
		{
            SetVisibilityIfExists("Message", true);
		}
	}
}
