//------------------------------------------------------------------------------
// <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 ManageCensorships : 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 ManageCensors;

		#endregion

		override protected void OnInit(EventArgs e)
		{

			ManageCensors.EditCommand += new DataListCommandEventHandler( ManageCensors_EditCommand );
			ManageCensors.CancelCommand += new DataListCommandEventHandler( ManageCensors_CancelCommand );
			ManageCensors.DeleteCommand += new DataListCommandEventHandler( ManageCensors_DeleteCommand );
			ManageCensors.UpdateCommand += new DataListCommandEventHandler( ManageCensors_UpdateCommand );
			ManageCensors.ItemCommand += new DataListCommandEventHandler( ManageCensors_ItemCommand );
			ManageCensors.ItemDataBound += new DataListItemEventHandler( ManageCensors_ItemDataBound );
			this.Load += new System.EventHandler(this.Page_Load);

			base.OnInit(e);
		}


		private void Page_Load(object sender, System.EventArgs e)
		{
			ManageCensors.DataSource = Censors.GetCensors();

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

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

		#region ManageCensors List Event Handlers
		private void ManageCensors_ItemDataBound( Object sender, DataListItemEventArgs e ) 
		{
			switch ( e.Item.ItemType ) 
			{
				case ListItemType.Item:
				case ListItemType.AlternatingItem:
				case ListItemType.SelectedItem:
					
					Label wordLabel = (Label)e.Item.FindControl( "Word" );
					wordLabel.Text =  Convert.ToString( DataBinder.Eval( e.Item.DataItem, "Word" ) );

					Label replacementLabel = (Label)e.Item.FindControl( "Replacement" );
					replacementLabel.Text = Convert.ToString( DataBinder.Eval( e.Item.DataItem, "Replacement" ) );

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

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

					break;
				case ListItemType.EditItem:
					
					TextBox word = (TextBox)e.Item.FindControl( "CensorWord" );
					word.Text = Convert.ToString( DataBinder.Eval( e.Item.DataItem, "Word" ) );

					TextBox replacement = (TextBox)e.Item.FindControl( "CensorReplacement" );
					replacement.Text = Convert.ToString( DataBinder.Eval( e.Item.DataItem, "Replacement" ) );

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

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

					break;
				case ListItemType.Footer:

					LinkButton createCensor = (LinkButton)e.Item.FindControl( "CreateCensor" );
					createCensor.Text = ResourceManager.GetString( "Create" );

					break;
			}
		}

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

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

		private void ManageCensors_DeleteCommand(object source, DataListCommandEventArgs e) 
		{
			Censor censor = (Censor)((ArrayList)ManageCensors.DataSource)[e.Item.ItemIndex];

			DeleteCensor( censor );

			ManageCensors.EditItemIndex = -1;
			ManageCensors.DataSource = Censors.GetCensors();
			ManageCensors.DataBind();
		}

		private void ManageCensors_UpdateCommand(object source, DataListCommandEventArgs e) 
		{
			Censor censor = (Censor)((ArrayList)ManageCensors.DataSource)[e.Item.ItemIndex];

			UpdateCensor( censor, e.Item );

			ManageCensors.EditItemIndex = -1;
			ManageCensors.DataSource = Censors.GetCensors();
			ManageCensors.DataBind();
		}

		private void ManageCensors_ItemCommand(object source, DataListCommandEventArgs e) 
		{
			if( e.Item.ItemType == ListItemType.Footer ) 
			{
				Censor censor = new Censor();

				AddCensor( censor, e.Item );

				ManageCensors.EditItemIndex = -1;
				ManageCensors.DataSource = Censors.GetCensors();
				ManageCensors.DataBind();
			}
		}

		#endregion

		protected virtual void DeleteCensor( Censor censor ) 
		{
			Censors.DeleteCensor( censor );
		}

		protected virtual void UpdateCensor( Censor censor, DataListItem item ) 
		{
			TextBox txtWord = (TextBox)item.FindControl("CensorWord");
			TextBox txtReplacement = (TextBox)item.FindControl("CensorReplacement");

			if( txtWord == null || txtReplacement == null ) 
			{
				return;
			}

			censor.Word = txtWord.Text;
			censor.Replacement = txtReplacement.Text;

			Censors.UpdateCensor( censor );
		}

		private void InitializeComponent()
		{
		
		}

		protected virtual void AddCensor( Censor censor, DataListItem item ) 
		{
			TextBox txtWord = (TextBox)item.FindControl("NewCensorWord");
			TextBox txtReplacement = (TextBox)item.FindControl("NewCensorReplacement");

			if( txtWord == null || txtReplacement == null ) 
			{
				return;
			}

			censor.Word = txtWord.Text;
			censor.Replacement = txtReplacement.Text;

			Censors.CreateCensor( censor );
		}


	}
}
