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

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

namespace CommunityServer.ControlPanel.Tools
{
	/// <summary>
	/// Summary description for ManageCensorships.
	/// </summary>
	public class ManageDisallowedNames : BaseToolsPage
	{
		protected CommunityServer.ControlPanel.Controls.ControlPanelSelectedNavigation SelectedNavigation1;
		protected CommunityServer.ControlPanel.Controls.ResourceControl RegionTitle;
		protected CommunityServer.Controls.MPContent DescriptionRegion;
		protected CommunityServer.ControlPanel.Controls.ResourceControl Resourcecontrol1;
		protected CommunityServer.Controls.MPContent TaskRegion;
		protected CommunityServer.Controls.MPContainer MPContainer;
		#region Members

		protected System.Web.UI.WebControls.DataList ManageNames;

		#endregion

		override protected void OnInit(EventArgs e)
		{

			ManageNames.EditCommand += new DataListCommandEventHandler( ManageNames_EditCommand );
			ManageNames.CancelCommand += new DataListCommandEventHandler( ManageNames_CancelCommand );
			ManageNames.DeleteCommand += new DataListCommandEventHandler( ManageNames_DeleteCommand );
			ManageNames.UpdateCommand += new DataListCommandEventHandler( ManageNames_UpdateCommand );
			ManageNames.ItemCommand += new DataListCommandEventHandler( ManageNames_ItemCommand );
			ManageNames.ItemDataBound += new DataListItemEventHandler( ManageNames_ItemDataBound );
			this.Load += new System.EventHandler(this.Page_Load);

			base.OnInit(e);
		}


		private void Page_Load(object sender, System.EventArgs e)
		{
			ManageNames.DataSource = DisallowedNames.GetNames();

			if ( !Page.IsPostBack ) 
			{
				this.Bind();
			}
		}

		protected void Bind()
		{
			ManageNames.DataBind();
		}

		#region ManageNames List Event Handlers
		private void ManageNames_ItemDataBound( Object sender, DataListItemEventArgs e ) 
		{
			switch ( e.Item.ItemType ) 
			{
				case ListItemType.Item:
				case ListItemType.AlternatingItem:
				case ListItemType.SelectedItem:
					
					Label nameLabel = (Label)e.Item.FindControl( "Name" );
					nameLabel.Text =  e.Item.DataItem.ToString();

					LinkButton edit = (LinkButton)e.Item.FindControl( "EditName" );
					edit.Text = ResourceManager.GetString( "Edit" );

					LinkButton delete = (LinkButton)e.Item.FindControl( "DeleteName" );
					delete.Text = ResourceManager.GetString( "Delete" );

					break;
				case ListItemType.EditItem:
					
					TextBox name = (TextBox)e.Item.FindControl( "DisallowedName" );
					name.Text = e.Item.DataItem.ToString();

					LinkButton update = (LinkButton)e.Item.FindControl( "UpdateName" );
					update.Text = ResourceManager.GetString( "Update" );

					LinkButton cancel = (LinkButton)e.Item.FindControl( "CancelName" );
					cancel.Text = ResourceManager.GetString( "Cancel" );

					break;
				case ListItemType.Footer:

					LinkButton createName = (LinkButton)e.Item.FindControl( "CreateName" );
					createName.Text = ResourceManager.GetString( "Create" );

					break;
			}
		}

		private void ManageNames_EditCommand(object source, DataListCommandEventArgs e) 
		{
			ManageNames.EditItemIndex = e.Item.ItemIndex;
			ManageNames.DataBind();
		}

		private void ManageNames_CancelCommand(object source, DataListCommandEventArgs e) 
		{
			ManageNames.EditItemIndex = -1;
			ManageNames.DataBind();
		}

		private void ManageNames_DeleteCommand(object source, DataListCommandEventArgs e) 
		{
			string name = ((ArrayList)ManageNames.DataSource)[e.Item.ItemIndex].ToString();

			DeleteName( name );

			ManageNames.EditItemIndex = -1;
			ManageNames.DataSource = DisallowedNames.GetNames();
			ManageNames.DataBind();
		}

		private void ManageNames_UpdateCommand(object source, DataListCommandEventArgs e) 
		{
			string name = ((ArrayList)ManageNames.DataSource)[e.Item.ItemIndex].ToString();

			UpdateName( name, e.Item );

			ManageNames.EditItemIndex = -1;
			ManageNames.DataSource = DisallowedNames.GetNames();
			ManageNames.DataBind();
		}

		private void ManageNames_ItemCommand(object source, DataListCommandEventArgs e) 
		{
			if( e.Item.ItemType == ListItemType.Footer ) 
			{
				string name = string.Empty;

				AddName( name, e.Item );

				ManageNames.EditItemIndex = -1;
				ManageNames.DataSource = DisallowedNames.GetNames();
				ManageNames.DataBind();
			}
		}

		#endregion

		protected virtual void DeleteName( string name ) 
		{
			DisallowedNames.DeleteName(name);
		}

		protected virtual void UpdateName( string name, DataListItem item ) 
		{
			TextBox txtName = (TextBox)item.FindControl("DisallowedName");

			if( txtName == null ) 
			{
				return;
			}

			DisallowedNames.UpdateName( txtName.Text, name );
		}

		private void InitializeComponent()
		{
		
		}

		protected virtual void AddName( string name, DataListItem item ) 
		{
			TextBox txtName = (TextBox)item.FindControl("NewName");
			
			if( txtName == null ) 
			{
				return;
			}

			DisallowedNames.CreateName( txtName.Text );
		}
	}
}
