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

using System;
using System.Collections;
using System.ComponentModel;
using System.Globalization;
using System.Web.UI;
using System.Web.UI.WebControls;
using CommunityServer.Components;

namespace CommunityServer.Galleries.Controls
{
	/// <summary>
	/// Summary description for CategoryCheckBoxList.
	/// </summary>
	public class CategoryCheckBoxList : CheckBoxList, IRepeatInfoUser
	{

		#region Public Properties

		[DefaultValue( -1 )]
		public virtual int SectionID
		{
			get
			{
				Object state = ViewState["SectionID"];
				if(state != null)
					return (int)state;
				return -1;
			}
			set { ViewState["SectionID"] = value; }
		}

		#endregion

		public CategoryCheckBoxList()
		{
			this.controlToRepeat = new CheckBox();
			this.controlToRepeat.ID = "0";
			//this.controlToRepeat.EnableViewState = false;
			this.Controls.Add(this.controlToRepeat);
		}

		public override void DataBind()
		{
			//throw new Exception("bah!");
				base.DataBind();
				Items.Clear();

				if(SectionID != -1)
					RecursiveAddCategories(0, PostCategories.GetCategories(SectionID, true, 0));
		}

		public void RecursiveAddCategories(int depth, ArrayList categories)
		{
			foreach(PostCategory category in categories)
			{
				ListItem item = new ListItem( category.Name, category.CategoryID.ToString() );
				item.Attributes.Add("depth", depth.ToString());
				Items.Add( item );
				
				ArrayList subCategories = PostCategories.GetCategories(category.SectionID, true, category.CategoryID);
				if(subCategories.Count > 0)
					RecursiveAddCategories(depth + 1, subCategories);
			}
		}

		protected override void OnPreRender(EventArgs e)
		{
			this.controlToRepeat.AutoPostBack = this.AutoPostBack;
			if(this.Page == null)
			{
				return;
			}
			for(int num1 = 0; num1 < this.Items.Count; num1++)
			{
				this.controlToRepeat.ID = num1.ToString(NumberFormatInfo.InvariantInfo);
				this.Page.RegisterRequiresPostBack(this.controlToRepeat);
			}
		}

		protected override void Render(HtmlTextWriter writer)
		{
			this.controlToRepeat.TabIndex = this.TabIndex;
			base.Render(writer);
		}


		private CheckBox controlToRepeat;
		void IRepeatInfoUser.RenderItem(ListItemType itemType, int repeatIndex, RepeatInfo repeatInfo, HtmlTextWriter writer)
		{
			controlToRepeat.ID = repeatIndex.ToString(NumberFormatInfo.InvariantInfo);
			controlToRepeat.Style.Add("margin-left", (1 * int.Parse(this.Items[repeatIndex].Attributes["depth"])) + "em");
			controlToRepeat.Text = this.Items[repeatIndex].Text;
			controlToRepeat.TextAlign = this.TextAlign;
			controlToRepeat.Checked = this.Items[repeatIndex].Selected;
			controlToRepeat.Enabled = this.Enabled;
			controlToRepeat.RenderControl(writer);
		}

	}
}
