//------------------------------------------------------------------------------
// <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 CategoryList : WeblogThemedControl 
	{
        /// <summary>
        /// Default Cnst. 
        /// </summary>
		public CategoryList()
		{
		
		}

        private Repeater Categories = null;
        private BlogUrls siteUrls = BlogUrls.Instance();

        /// <summary>
        /// Bind the Categories repeater.
        /// </summary>
        public override void DataBind()
        {
            base.DataBind ();

            
            ArrayList items = PostCategories.GetCategories(CurrentWeblog.SectionID,true,false);
            if(items != null && items.Count > 0)
            {
                Categories.DataSource = items;
                Categories.DataBind();
            }
            else
            {
                this.Visible = false;
            }
        }


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

            ResourceControl title = FindControl("CategoryTitle") as ResourceControl;
            if(title != null && ResourceTitleName != null)
                title.ResourceName = ResourceTitleName;

            DataBind();

        }

        private string _resourceTitleName;
        /// <summary>
        /// Property ResourceTitleName (string)
        /// </summary>
        public string ResourceTitleName
        {
            get {  return this._resourceTitleName; }
            set {  this._resourceTitleName = value; }
        }

  
        private void Categories_ItemCreated(object sender, RepeaterItemEventArgs e)
        {
            switch ( e.Item.ItemType ) 
            {
                case ListItemType.Item:
                case ListItemType.AlternatingItem:
                case ListItemType.SelectedItem:
                    
                    PostCategory pc = e.Item.DataItem as PostCategory;
                    HyperLink link = e.Item.FindControl( "Link" ) as HyperLink;
                    HyperLink rss = e.Item.FindControl( "rss" ) as HyperLink;
                    
                    if(rss != null) rss.Visible = false;

                    if(pc != null && link != null)
                    {
                        link.Text = pc.Name + " (" + pc.TotalThreads.ToString() + ")";

                        //if(this.CategoryType == CategoryType.BlogArticle)
                        //{
                        //    link.NavigateUrl = siteUrls.IndividualCategoryPage(CurrentWeblog.ApplicationKey,this.CategoryType,pc.CategoryID);
                        //}
                        //else if(this.CategoryType == CategoryType.BlogPost)
                        {
                            //rss.Visible = true;
                            //rss.NavigateUrl = siteUrls.Rss(this.CurrentWeblog.ApplicationKey,pc.CategoryID);
                            link.NavigateUrl = siteUrls.IndividualCategoryPage(CurrentWeblog.ApplicationKey,pc.CategoryID);
                        }       
                         
                    }
            
                    break;
            }
        }
    }
}
