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

using System.Web.UI.WebControls;
using CommunityServer.Components;

namespace CommunityServer.Controls
{
	/// <summary>
	/// A list of FeedLink items (feed icons linked to feeds, with titles linked to the main content)
    /// (like a blogroll)
	/// </summary>
	public class FeedLinkList : TemplatedWebControl
	{
        Repeater feedList = null;
	    Literal listTitle = null;

        private FeedLinkSet dataSource;
        public FeedLinkSet DataSource
        {
            get { return dataSource; }
            set
            {
                dataSource = value;
                this.EnsureChildControls();
            }
        }

         ///<Summary>
        /// The title for the list
        ///</Summary>
        string title = string.Empty;
        public string Title
        {
            get { return title; }
            set { title = value; }
        }

        //private object _dataSource;

        ///// <summary>
        ///// Property DataSource (object)
        ///// </summary>
        //public object DataSource
        //{
        //    get { return this._dataSource; }
        //    set { this._dataSource = value; }
        //}

        public override void DataBind()
        {
            base.DataBind();
            if (DataSource != null)
            {
                feedList.DataSource = DataSource.List.Values;
                feedList.DataBind();
            }
        }


        protected override void AttachChildControls()
        {
            listTitle = FindControl("ListTitle") as Literal;
            if (listTitle != null)
                listTitle.Text = this.Title;
            
            feedList = FindControl("FeedList") as Repeater;
            if (feedList != null)
                feedList.ItemDataBound += new RepeaterItemEventHandler(feedList_ItemDataBound);
        }

	    private void feedList_ItemDataBound(object sender, RepeaterItemEventArgs e)
	    {
	        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
	        {
	            FeedLinkItem linkItem = e.Item.DataItem as FeedLinkItem;
	            if(linkItem == null)
	                return;

	            FeedLink link = e.Item.FindControl("FeedLink") as FeedLink;
                if (link != null)
	                link.SetFromFeedLinkItem(linkItem);
	        }
	    }


	}
}
