//------------------------------------------------------------------------------
// <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 ManageRedirects.
	/// </summary>
	public class ManageUrlRedirects : 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 ManageRedirects;

		#endregion

		override protected void OnInit(EventArgs e)
		{

			ManageRedirects.EditCommand += new DataListCommandEventHandler( ManageRedirects_EditCommand );
			ManageRedirects.CancelCommand += new DataListCommandEventHandler( ManageRedirects_CancelCommand );
			ManageRedirects.DeleteCommand += new DataListCommandEventHandler( ManageRedirects_DeleteCommand );
			ManageRedirects.UpdateCommand += new DataListCommandEventHandler( ManageRedirects_UpdateCommand );
			ManageRedirects.ItemCommand += new DataListCommandEventHandler( ManageRedirects_ItemCommand );
			ManageRedirects.ItemDataBound += new DataListItemEventHandler( ManageRedirects_ItemDataBound );
			this.Load += new System.EventHandler(this.Page_Load);

			base.OnInit(e);
		}


		private void Page_Load(object sender, System.EventArgs e)
		{
			ManageRedirects.DataSource = UrlRedirects.GetUrlRedirects();

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

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

		#region ManageRedirects List Event Handlers
		private void ManageRedirects_ItemDataBound( Object sender, DataListItemEventArgs e ) 
		{
			switch ( e.Item.ItemType ) 
			{
				case ListItemType.Item:
				case ListItemType.AlternatingItem:
				case ListItemType.SelectedItem:

					Label idLabel = (Label)e.Item.FindControl( "ID" );
					idLabel.Text =  Convert.ToString( DataBinder.Eval( e.Item.DataItem, "Base36ID" ) );

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

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

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

					HyperLink view = (HyperLink)e.Item.FindControl( "ViewUrlRedirect" );
					view.Text = ResourceManager.GetString( "View" );
					view.NavigateUrl = string.Format("javascript:Telligent_Modal.Open('UrlRedirectDetails.aspx?UrlID={0}', 500, 200, null);", DataBinder.Eval(e.Item.DataItem, "ID"));

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

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

					break;
				case ListItemType.EditItem:

					Label id = (Label)e.Item.FindControl( "ID" );
					id.Text =  Convert.ToString( DataBinder.Eval( e.Item.DataItem, "Base36ID" ) );

					TextBox url = (TextBox)e.Item.FindControl( "UrlRedirectUrl" );
					url.Text = Convert.ToString( DataBinder.Eval( e.Item.DataItem, "Url" ) );

					TextBox description = (TextBox)e.Item.FindControl( "UrlRedirectDescription" );
					description.Text = Convert.ToString( DataBinder.Eval( e.Item.DataItem, "Description" ) );

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

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

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

					break;
				case ListItemType.Footer:

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

					break;
			}
		}

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

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

		private void ManageRedirects_DeleteCommand(object source, DataListCommandEventArgs e) 
		{
			UrlRedirect urlRedirect = (UrlRedirect)((ArrayList)ManageRedirects.DataSource)[e.Item.ItemIndex];

			DeleteUrlRedirect( urlRedirect );

			ManageRedirects.EditItemIndex = -1;
			ManageRedirects.DataSource = UrlRedirects.GetUrlRedirects();
			ManageRedirects.DataBind();
		}

		private void ManageRedirects_UpdateCommand(object source, DataListCommandEventArgs e) 
		{
			UrlRedirect urlRedirect = (UrlRedirect)((ArrayList)ManageRedirects.DataSource)[e.Item.ItemIndex];

			UpdateUrlRedirect( urlRedirect, e.Item );

			ManageRedirects.EditItemIndex = -1;
			ManageRedirects.DataSource = UrlRedirects.GetUrlRedirects();
			ManageRedirects.DataBind();
		}

		private void ManageRedirects_ItemCommand(object source, DataListCommandEventArgs e) 
		{
			if( e.Item.ItemType == ListItemType.Footer ) 
			{
				UrlRedirect urlRedirect = new UrlRedirect();

				AddUrlRedirect( urlRedirect, e.Item );

				ManageRedirects.EditItemIndex = -1;
				ManageRedirects.DataSource = UrlRedirects.GetUrlRedirects();
				ManageRedirects.DataBind();
			}
		}

		#endregion

		protected virtual void DeleteUrlRedirect( UrlRedirect urlRedirect ) 
		{
			UrlRedirects.DeleteUrlRedirect( urlRedirect );
		}

		protected virtual void UpdateUrlRedirect( UrlRedirect urlRedirect, DataListItem item ) 
		{
			TextBox txtUrl = (TextBox)item.FindControl("UrlRedirectUrl");
			TextBox txtDescription = (TextBox)item.FindControl("UrlRedirectDescription");

			if( txtUrl == null || txtDescription == null ) 
			{
				return;
			}

			urlRedirect.Url = txtUrl.Text;
			urlRedirect.Description = txtDescription.Text;

			UrlRedirects.UpdateUrlRedirect( urlRedirect );
		}

		private void InitializeComponent()
		{
		}

		protected virtual void AddUrlRedirect( UrlRedirect urlRedirect, DataListItem item ) 
		{
			TextBox txtUrl = (TextBox)item.FindControl("NewUrlRedirectUrl");
			TextBox txtDescription = (TextBox)item.FindControl("NewUrlRedirectDescription");

			if( txtUrl == null || txtDescription == null ) 
			{
				return;
			}

			urlRedirect.Url = txtUrl.Text;
			urlRedirect.Description = txtDescription.Text;

			UrlRedirects.CreateUrlRedirect( urlRedirect );
		}
	}
}
