//------------------------------------------------------------------------------
// <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.UI;
using CommunityServer.Controls;
using ResourceLinkButton = CommunityServer.ControlPanel.Controls.ResourceLinkButton;
using ResourceManager = CommunityServer.ControlPanel.Components.ResourceManager;
using StatusMessage = CommunityServer.ControlPanel.Controls.StatusMessage;

namespace CommunityServer.ControlPanel.Membership
{
	/// <summary>
	/// Summary description for AvatarOptions.
	/// </summary>
	public class AvatarOptions : BaseMembershipPage
	{

		#region Members

		protected YesNoRadioButtonList optEnableAvatars;
		protected YesNoRadioButtonList optEnableRemoteAvatars;
		protected TextBox txtAvatarHeight;
		protected TextBox txtAvatarWidth;

		protected StatusMessage formStatus;
		protected ResourceLinkButton btnSave;

		#endregion

		override protected void OnInit(EventArgs e)
		{
			btnSave.Click += new EventHandler(Save_Click);
			this.Load += new EventHandler(this.Page_Load);

			base.OnInit(e);
		}
		

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

		
		public override void DataBind()
		{
			formStatus.Visible = false;

			SiteSettings settings = CSContext.Current.SiteSettings;
			optEnableAvatars.SelectedValue = settings.EnableAvatars;
			optEnableRemoteAvatars.SelectedValue = settings.EnableRemoteAvatars;
			txtAvatarHeight.Text = settings.AvatarHeight.ToString();
			txtAvatarWidth.Text = settings.AvatarWidth.ToString();
	
		}
	

		private void Save_Click(Object sender, EventArgs e) 
		{

			if (!Page.IsValid)
				return;
			
			SaveSettings();

			formStatus.Visible = true;
			formStatus.Success = true;
			formStatus.Text = string.Format(ResourceManager.GetString("CP_Membership_Cache_Warning"), CSContext.Current.SiteSettings.SiteSettingsCacheWindowInMinutes).Replace("'", @"\'");
		}

		
		protected virtual void SaveSettings() 
		{
			SiteSettings settings = CSContext.Current.SiteSettings; 

			settings.EnableAvatars = optEnableAvatars.SelectedValue;
			settings.EnableRemoteAvatars = optEnableRemoteAvatars.SelectedValue;
			settings.AvatarHeight = Int32.Parse( txtAvatarHeight.Text);
			settings.AvatarWidth = Int32.Parse( txtAvatarWidth.Text);

			SiteSettingsManager.Save(settings);
		}


	}
}
