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

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

namespace CommunityServer.Blogs.Controls 
{
	/// <summary>
	/// Displays a list of blog groups, and blogs for a CS site.  Only blogs that are marked
	/// to be Commuity Aggregated will be included.
	/// </summary>
	public class AggregateBlogList : WeblogBaseTemplatedWebControl
	{
		CSContext csContext = CSContext.Current;
		public int PageSize = 20;
		bool pageOnPostBackOnly = false;

		Repeater GroupList = null;

		public bool PageOnPostBackOnly
		{
			get{ return pageOnPostBackOnly;}
			set{ pageOnPostBackOnly = value; }
		}


		protected override void AttachChildControls()
		{
			GroupList = FindControl( "GroupList" ) as Repeater;
			GroupList.ItemCreated +=new RepeaterItemEventHandler(Groups_ItemCreated);

			DataBind();
		}

		public override void DataBind()
		{
			base.DataBind ();

			int groupID =  Globals.SafeInt(Context.Request.QueryString["GroupID"],-1);
            

			if(groupID > 0)
			{

				Group g = WeblogGroups.GetWeblogGroup(groupID,true,false);
                if(g != null)
                {
                    GroupList.DataSource = new Group[]{g};

                    Head.AddSiteNameTitle(g.Name,Context);

                    if(WeblogGroups.IsPublic(g.GroupID))
                    {
                        UsersOnline.SetLocation(g.Name);
                    }
                    else
                    {
                        UsersOnline.PrivateLocation();
                    }
                }
			}
			else
			{
                Head.AddSiteNameTitle("Weblog Groups",Context);
			    UsersOnline.SetLocation("Weblog Groups");
				GroupList.DataSource = WeblogGroups.GetWeblogGroups(true,false,false);
			}

			GroupList.DataBind();			
		}



		private void Groups_ItemCreated(object sender, RepeaterItemEventArgs e)
		{
			switch ( e.Item.ItemType ) 
			{
				case ListItemType.Item:
				case ListItemType.AlternatingItem:
				case ListItemType.SelectedItem:

					Group g = e.Item.DataItem as Group;
					ArrayList blogs = Weblogs.GetWeblogsByGroupID(g.GroupID,true,false,false);

					// Remove blogs that should not be community aggregated
					blogs = Sections.FilterByAggregated(blogs, true);
					
					IPagedControl postPager = e.Item.FindControl("Pager") as IPagedControl;
					if (postPager != null)
					{
						postPager.PageIndex = !PageOnPostBackOnly || Page.IsPostBack ? csContext.PageIndex : 0;
						postPager.PageSize = this.PageSize;
						postPager.TotalRecords = blogs.Count;

						if (blogs.Count > this.PageSize)
						{
							int start = postPager.PageIndex * this.PageSize;
							int length = this.PageSize;

							if (start < blogs.Count)
							{
								if (start + length > blogs.Count)
									length = blogs.Count - start;
							
								blogs = blogs.GetRange(start, length);
							}
							else
							{
								blogs = new ArrayList();
							}
						}
					}

					Repeater BlogList = e.Item.FindControl( "BlogList" ) as Repeater;
					BlogList.ItemCreated +=new RepeaterItemEventHandler(BlogList_ItemCreated);
					BlogList.DataSource = blogs;
					BlogList.DataBind();

					Literal groupName = e.Item.FindControl("GroupName") as Literal;
					if(groupName != null)
					    groupName.Text = g.Name;


					break;
			}
		}

        protected virtual void BlogList_ItemCreated(object sender, RepeaterItemEventArgs e)
		{
			switch ( e.Item.ItemType ) 
			{
				case ListItemType.Item:
				case ListItemType.AlternatingItem:
				case ListItemType.SelectedItem:

					Weblog blog = e.Item.DataItem as Weblog;

					HyperLink name = e.Item.FindControl("BlogName") as HyperLink;
					PlaceHolder authors = e.Item.FindControl("Author") as PlaceHolder;
					Literal desc = e.Item.FindControl("BlogDesc") as Literal;
					HyperLink recentPost = e.Item.FindControl("recentPost") as HyperLink;
					Literal pc = e.Item.FindControl("PostCount") as Literal;
					Literal ac = e.Item.FindControl("ArticleCount") as Literal;
					Literal cc = e.Item.FindControl("CommentCount") as Literal;
					Literal tc = e.Item.FindControl("TrackbackCount") as Literal;

					if (name != null)
					{
						name.NavigateUrl = BlogUrls.Instance().HomePage(blog.ApplicationKey);
						name.Text = blog.Name;
					}

					if (desc != null)
						desc.Text = blog.Description;

					if (recentPost != null)
					{
						recentPost.NavigateUrl = BlogUrls.Instance().ShortLink(blog.MostRecentPostID);
						recentPost.Text = blog.MostRecentPostSubject;
					}

					if (pc != null)
						pc.Text = blog.PostCount.ToString();

					if (ac != null)
						ac.Text = blog.ArticleCount.ToString();

					if (cc != null)
						cc.Text = blog.CommentCount.ToString();

					if (tc != null)
						tc.Text = blog.TrackbackCount.ToString();

					if (authors != null)
					{
						string[] owners = blog.OwnerArray;
						for (int i = 0; i<owners.Length; i++) 
						{
							authors.Controls.Add(new LiteralControl( "<a href=\"" + SiteUrls.Instance().UserProfile(owners[i].Trim()) + " \">" + owners[i].Trim() + "</a>" ));

							if (i+1 != owners.Length)
								authors.Controls.Add(new LiteralControl( ", " ));
						}
					}

					break;
			}
		}
	}
}
