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

using System;
using System.Web.UI.WebControls;
using CommunityServer.Components;
using CommunityServer.ControlPanel.Controls;
using CommunityServer.ControlPanel.UI;
using CommunityServer.Controls;
using ResourceControl = CommunityServer.ControlPanel.Controls.ResourceControl;
using StatusMessage = CommunityServer.ControlPanel.Controls.StatusMessage;

namespace CommunityServer.ControlPanel.Tools
{
	/// <summary>
	/// Summary description for ManageSmilies.
	/// </summary>
	public class ManageSmilies : BaseToolsPage
	{
		#region Members

		protected StatusMessage Status;
		protected ControlPanelSelectedNavigation SelectedNavigation1;
		protected ResourceControl RegionTitle;
		protected MPContent DescriptionRegion;
		protected ResourceControl Resourcecontrol1;
		protected MPContent TaskRegion;
		protected MPContainer MPContainer;
		protected DataList SmiliesList;

		#endregion

		override protected void OnInit(EventArgs e)
		{
			SmiliesList.DataKeyField = "SmileyID";

			SmiliesList.ItemDataBound += new DataListItemEventHandler(ManageSmilies_ItemDataBound);
			SmiliesList.EditCommand += new DataListCommandEventHandler(ManageSmilies_EditCommand);
			SmiliesList.CancelCommand += new DataListCommandEventHandler(ManageSmilies_CancelCommand);
			SmiliesList.DeleteCommand += new DataListCommandEventHandler(ManageSmilies_DeleteCommand);
			SmiliesList.UpdateCommand += new DataListCommandEventHandler(ManageSmilies_UpdateCommand);
			SmiliesList.ItemCommand += new DataListCommandEventHandler(ManageSmilies_ItemCommand);

			this.Load += new EventHandler(this.Page_Load);

			base.OnInit(e);
		}


		private void Page_Load(object sender, EventArgs e)
		{
			if ( !Page.IsPostBack ) 
			{
				this.Bind();
			}
		}

		protected void Bind()
		{
			BindSmilies();
		}


		private void BindSmilies() 
		{
			SmiliesList.DataSource = Smilies.GetSmilies(false);
			SmiliesList.DataBind();
		}

		#region Command Handlers

		private void ManageSmilies_EditCommand(object source, DataListCommandEventArgs e) 
		{
			SmiliesList.EditItemIndex = e.Item.ItemIndex;
			
			BindSmilies();
		}

		private void ManageSmilies_CancelCommand(object source, DataListCommandEventArgs e) 
		{
			SmiliesList.EditItemIndex = -1;
			
			BindSmilies();
		}

		private void ManageSmilies_DeleteCommand(object source, DataListCommandEventArgs e) 
		{
			DeleteSmiley( e );
			
			SmiliesList.EditItemIndex = -1;
			BindSmilies();
		}

		private void ManageSmilies_UpdateCommand(object source, DataListCommandEventArgs e) 
		{
			UpdateSmiley( e );

			SmiliesList.EditItemIndex = -1;
			BindSmilies();
		}

		private void ManageSmilies_ItemCommand(object source, DataListCommandEventArgs e) 
		{
			if( e.CommandName == "Create" ) 
			{
				CreateSmiley( e );
			
				BindSmilies();
			}
		}

		#endregion

		#region Command Implementations

		protected virtual void DeleteSmiley( DataListCommandEventArgs e ) 
		{
			Int32 SmileyID = Convert.ToInt32( this.SmiliesList.DataKeys[ e.Item.ItemIndex ] );
			Smiley smiley = GetSmiley( SmileyID );
			
			Smilies.DeleteSmiley( smiley );
		}

		protected virtual void UpdateSmiley( DataListCommandEventArgs e ) 
		{
			TextBox SmileyCode = (TextBox)e.Item.FindControl( "SmileyCodeEdit" );
			TextBox SmileyUrl = (TextBox)e.Item.FindControl( "SmileyUrlEdit" );
			TextBox SmileyText = (TextBox)e.Item.FindControl( "SmileyTextEdit" );

			if ( SmileyCode == null || SmileyUrl == null || SmileyText == null ) 
			{
				return;
			}

			Int32 SmileyID = Convert.ToInt32( this.SmiliesList.DataKeys[ e.Item.ItemIndex ] );
			Smiley smiley = GetSmiley( SmileyID );

			if ( smiley.SmileyCode == SmileyCode.Text && smiley.SmileyUrl == SmileyUrl.Text && smiley.SmileyText == SmileyText.Text ) 
			{
				return;
			}

			smiley.SmileyCode = SmileyCode.Text;
			smiley.SmileyUrl = SmileyUrl.Text;
			smiley.SmileyText = SmileyText.Text;

			Smilies.UpdateSmiley( smiley );
		}

		protected virtual void CreateSmiley( DataListCommandEventArgs e ) 
		{
			TextBox SmileyCode = (TextBox)e.Item.FindControl( "NewSmileyCode" );
			TextBox SmileyUrl = (TextBox)e.Item.FindControl( "NewSmileyUrl" );
			TextBox SmileyText = (TextBox)e.Item.FindControl( "NewSmileyText" );

			if ( SmileyCode == null || SmileyUrl == null || SmileyText == null ) 
			{
				return;
			}

			Smiley smiley = new Smiley( 0, SmileyCode.Text, SmileyUrl.Text, SmileyText.Text, false );
			Smilies.CreateSmiley( smiley );
		}

		#endregion


		private void ManageSmilies_ItemDataBound(object sender, DataListItemEventArgs e) 
		{
			Smiley dataItem = e.Item.DataItem as Smiley;

			switch( e.Item.ItemType ) 
			{
				case ListItemType.Item:
				case ListItemType.AlternatingItem:

					Label SmileyIDLabel = (Label)e.Item.FindControl( "SmileyID" );
					Label SmileyCodeLabel = (Label)e.Item.FindControl( "SmileyCode" );
					Label SmileyUrlLabel = (Label)e.Item.FindControl( "SmileyUrl" );
					Image SmileyImage = (Image)e.Item.FindControl( "SmileyImage" );
					Label SmileyTextLabel = (Label)e.Item.FindControl( "SmileyText" );
					
					LinkButton EditButton = (LinkButton)e.Item.FindControl( "Edit" );
					LinkButton DeleteButton = (LinkButton)e.Item.FindControl( "Delete" );

					SmileyIDLabel.Text = dataItem.SmileyId.ToString();
					SmileyCodeLabel.Text = dataItem.SmileyCode;
					SmileyUrlLabel.Text = dataItem.SmileyUrl;
					SmileyImage.AlternateText = dataItem.SmileyText;
					SmileyImage.ImageUrl = Globals.ApplicationPath + "/" + dataItem.SmileyUrl;
					SmileyTextLabel.Text = dataItem.SmileyText;

					EditButton.Text = ResourceManager.GetString( "Edit" );
					DeleteButton.Text = ResourceManager.GetString( "Delete" );

					break;
				case ListItemType.EditItem:

					Label SmileyID = (Label)e.Item.FindControl( "SmileyIDEdit" );
					TextBox SmileyCode = (TextBox)e.Item.FindControl( "SmileyCodeEdit" );
					TextBox SmileyUrl = (TextBox)e.Item.FindControl( "SmileyUrlEdit" );
					TextBox SmileyText = (TextBox)e.Item.FindControl( "SmileyTextEdit" );
					LinkButton UpdateButton = (LinkButton)e.Item.FindControl( "Update" );
					LinkButton CancelButton = (LinkButton)e.Item.FindControl( "Cancel" );

					if(SmileyID == null)
						throw new Exception("hey!");

					SmileyID.Text = dataItem.SmileyId.ToString();
					SmileyCode.Text = dataItem.SmileyCode;
					SmileyUrl.Text = dataItem.SmileyUrl;
					SmileyText.Text = dataItem.SmileyText;
					UpdateButton.Text = ResourceManager.GetString( "Update" );
					CancelButton.Text = ResourceManager.GetString( "Cancel" );

					break;
				case ListItemType.Footer:

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

					break;
			}
		}

		private void InitializeComponent()
		{
		
		}
	
		private Smiley GetSmiley( Int32 smileyID ) 
		{
			Smiley apiProvidedSmiley = Smilies.GetSmiley( smileyID );
			if ( apiProvidedSmiley != null ) 
			{
				return apiProvidedSmiley;
			}

			foreach( Smiley smiley in Smilies.GetSmilies() ) 
			{
				if ( smiley.SmileyId == smileyID ) 
				{
					return smiley;
				}
			}
			return null;
		}

	}
}
