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

using System;
using CommunityServer.Components;
using CommunityServer.ControlPanel.UI;
using CommunityServer.Blogs.Components;
using CommunityServer.ControlPanel.Controls;
using CommunityServer.ControlPanel.Components;
using ResourceManager = CommunityServer.ControlPanel.Components.ResourceManager;


namespace CommunityServer.ControlPanel.Blogs
{
	/// <summary>
	/// Summary description for BlogEditorHomePage.
	/// </summary>
	public class BlogEditorHomePage : BaseBlogPage
	{

		#region Child Controls
		
		protected System.Web.UI.WebControls.Repeater Owners;
		protected System.Web.UI.WebControls.Literal DateCreated;
		protected System.Web.UI.WebControls.Literal PostCount;
		protected System.Web.UI.WebControls.HyperLink MostRecentPostLink;
		protected System.Web.UI.WebControls.Literal MostRecentPostDate;
		protected System.Web.UI.WebControls.HyperLink MostRecentPostUser;
		protected System.Web.UI.WebControls.Literal ArticleCount;
		protected System.Web.UI.WebControls.HyperLink MostRecentArticleLink;
		protected System.Web.UI.WebControls.Literal MostRecentArticleDate;
		protected System.Web.UI.WebControls.HyperLink MostRecentArticleUser;
		protected System.Web.UI.WebControls.HyperLink CommentCount;
		protected System.Web.UI.WebControls.HyperLink CommentModerateCount;
		protected System.Web.UI.WebControls.HyperLink TrackbackCount;
		protected System.Web.UI.Control MostRecentPostSpan;
		protected CommunityServer.ControlPanel.Controls.ResourceControl CP_Blog_Home_TotalTrackbacks;
		protected System.Web.UI.Control MostRecentArticleSpan;

		#endregion


		override protected void OnInit(EventArgs e)
		{
			this.Load += new System.EventHandler(this.Page_Load);
			this.Owners.ItemDataBound += new System.Web.UI.WebControls.RepeaterItemEventHandler(Owners_ItemDataBound);
			base.OnInit(e);
		}

		private void Page_Load(object sender, System.EventArgs e)
		{
			CSContext cntx = CSContext.Current;
			// General Blog Info
			Owners.DataSource = CurrentWeblog.OwnerArray;
			Owners.DataBind();
			DateCreated.Text = String.Format(ResourceManager.GetString("CP_Blog_Home_CreatedFormatString"), CurrentWeblog.DateCreated.ToString(cntx.User.Profile.DateFormat));
			
			// Post Info
			PostCount.Text = CurrentWeblog.PostCount.ToString();
			if (CurrentWeblog.PostCount > 0)
			{
				MostRecentPostLink.Text = CurrentWeblog.MostRecentPostSubject;
				MostRecentPostLink.NavigateUrl = BlogUrls.Instance().PostEditor(CurrentWeblog.SectionID, CurrentWeblog.MostRecentPostID);
				MostRecentPostDate.Text = CurrentWeblog.MostRecentPostDate.ToString(cntx.User.Profile.DateFormat);
				MostRecentPostUser.Text = GetSafeDisplayName(CurrentWeblog.MostRecentPostAuthor);
				MostRecentPostUser.NavigateUrl = Globals.GetSiteUrls().UserProfile(CurrentWeblog.MostRecentPostAuthor);
			}
			else
			{
				MostRecentPostSpan.Visible = false;
			}

			// Article Info
			ArticleCount.Text = CurrentWeblog.ArticleCount.ToString();
			if (CurrentWeblog.ArticleCount > 0)
			{
				MostRecentArticleLink.Text = CurrentWeblog.MostRecentArticleSubject;
				MostRecentArticleLink.NavigateUrl = BlogUrls.Instance().PostEditor(CurrentWeblog.SectionID, CurrentWeblog.MostRecentArticleID);
				MostRecentArticleDate.Text = CurrentWeblog.MostRecentArticleDate.ToString(cntx.User.Profile.DateFormat);
				MostRecentArticleUser.Text = GetSafeDisplayName(CurrentWeblog.MostRecentArticleAuthor);
				MostRecentArticleUser.NavigateUrl = Globals.GetSiteUrls().UserProfile(CurrentWeblog.MostRecentArticleAuthor);
			}
			else
			{
				MostRecentArticleSpan.Visible = false;
			}

			// Comment Info
			CommentCount.Text = CurrentWeblog.CommentCount.ToString();
			CommentCount.NavigateUrl = BlogUrls.Instance().AdminFeedback(CurrentWeblog.SectionID, BlogPostType.Comment);

			// Moderation Count
			CommentModerateCount.Text = CurrentWeblog.PostsToModerate.ToString();
			CommentModerateCount.NavigateUrl = BlogUrls.Instance().AdminFeedback(CurrentWeblog.SectionID);

			// Trackback/Pingback Info
			TrackbackCount.Text = CurrentWeblog.TrackbackCount.ToString();
			TrackbackCount.NavigateUrl = BlogUrls.Instance().AdminFeedback(CurrentWeblog.SectionID, BlogPostType.Trackback);
		
		}
		
		private string GetSafeDisplayName(string username)
		{
			User u = Users.FindUserByUsername(username);
			if (u != null && u.UserID > 0)
				return u.DisplayName;
			else
				return username;
		}

		private void Owners_ItemDataBound(object sender, System.Web.UI.WebControls.RepeaterItemEventArgs e)
		{
			System.Web.UI.WebControls.HyperLink ownerLink = e.Item.FindControl("owner") as System.Web.UI.WebControls.HyperLink;
			if (ownerLink != null)
			{
				string owner = e.Item.DataItem.ToString();
				User u = Users.FindUserByUsername(owner);
				if (u != null && u.UserID > 0)
				{
					ownerLink.Text = u.DisplayName;
					ownerLink.NavigateUrl = Globals.GetSiteUrls().UserProfile(u.UserID);
				}
				else
					ownerLink.Text = owner;
			}
		}
	}
}
