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

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

namespace CommunityServer.Blogs.Controls
{
	/// <summary>
	/// Displays a list of PostCategories by the specificed Category Type
	/// </summary>
	public class ArchiveList : WeblogThemedControl 
	{
        /// <summary>
        /// Default Cnst. 
        /// </summary>
		public ArchiveList()
		{
		
		}

        private Repeater Months = null;
        /// <summary>
        /// Bind the Categories repeater.
        /// </summary>
        public override void DataBind()
        {
            base.DataBind ();

          
            
            ArrayList items = WeblogPosts.GetPostsByMonths(CurrentWeblog.SectionID);
            if(items != null && items.Count > 0)
            {
                Months.DataSource = items;
                Months.DataBind();
            }
            else
            {
                this.Visible = false;
            }
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad (e);

//            if ( !Page.IsPostBack || !EnableViewState ) 
//            {
                DataBind();
//            }

        }



        /// <summary>
        /// Attach Categories Repeater and Resource Label
        /// </summary>
        protected override void AttachChildControls()
        {
            Months = FindControl( "Categories" ) as Repeater;
            Months.ItemCreated +=new RepeaterItemEventHandler(Categories_ItemCreated);

        }

        private void Categories_ItemCreated(object sender, RepeaterItemEventArgs e)
        {
            switch ( e.Item.ItemType ) 
            {
                case ListItemType.Item:
                case ListItemType.AlternatingItem:
                case ListItemType.SelectedItem:
                    
                    ArchiveDataItem ad = e.Item.DataItem as ArchiveDataItem;
                    HyperLink link = e.Item.FindControl( "Link" ) as HyperLink;
                    

                    if(ad != null && link != null)
                    {
                        link.Text = string.Format("{0} ({1})",ad.Date.ToString("MMMM yyyy"),ad.Count);
                        link.NavigateUrl = BlogUrls.Instance().MonthPage(CurrentWeblog.ApplicationKey,ad.Date);
                    }
                break;           
            }
        }
    }
}
