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

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

namespace CommunityServer.RollerBlogs.Controls 
{
	/// <summary>
	/// Extends the Aggregate Blog List by showing any feeds that are being pulled into the blog
	/// </summary>
    public class AggregateRollerBlogList : AggregateBlogList
	{

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

					Weblog blog = e.Item.DataItem as Weblog;


                    FeedLinkList RollerList = e.Item.FindControl("RollerList") as FeedLinkList;
                    if (RollerList != null)
                    {

                        RollerList.DataSource = RollerBlogFeeds.GetEnabledFeedLinkSet(blog.SectionID);
                        RollerList.DataBind();

                        RollerList.Visible = RollerList.DataSource.List.Count > 0;
                    }

                    FeedLinkList AuthorList = e.Item.FindControl("AuthorList") as FeedLinkList;
                    if (AuthorList != null)
                    {

                        string[] owners = blog.OwnerArray;
                        FeedLinkSet authors = new FeedLinkSet();
                        for (int i = 0; i < owners.Length; i++)
                        {
                            if (owners[i].Trim() != string.Empty)
                                authors.Add(new FeedLinkItem(owners[i].Trim(), "", SiteUrls.Instance().UserProfile(owners[i].Trim())));
                        }

                        AuthorList.DataSource = authors;
                        AuthorList.DataBind();

                        AuthorList.Visible = AuthorList.DataSource.List.Count > 0;
                    }

                    Literal AuthorSeperator = e.Item.FindControl("AuthorSeperator") as Literal;
                    if (AuthorSeperator != null)
                    {
                        AuthorSeperator.Visible = (AuthorList != null
                                                   && RollerList != null
                                                   && AuthorList.DataSource.List.Count > 0
                                                   && RollerList.DataSource.List.Count > 0);
                    }

                    break;
			}
		}

    }
}
