//------------------------------------------------------------------------------
// <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 HelpIcon = CommunityServer.ControlPanel.Controls.HelpIcon;
using ResourceControl = CommunityServer.ControlPanel.Controls.ResourceControl;
using ResourceLinkButton = CommunityServer.ControlPanel.Controls.ResourceLinkButton;
using StatusMessage = CommunityServer.ControlPanel.Controls.StatusMessage;

namespace CommunityServer.ControlPanel.Settings
{
	/// <summary>
	/// Summary description for DateTime.
	/// </summary>
	public class DateTime : BaseSettingsPage
	{
		#region Members

		protected TimezoneDropDownList Timezone;
		protected DateFormatDropDownList DateFormat;
		protected TimeFormatDropDownList TimeFormat;
		protected DateFilter ThreadDateFilter;

		protected StatusMessage Status;
		protected ControlPanelSelectedNavigation SelectedNavigation1;
		protected ResourceControl RegionTitle;
		protected MPContent DescriptionRegion;
		protected HelpIcon Helpicon4;
		protected ResourceControl ResourceControl14;
		protected HelpIcon Helpicon1;
		protected ResourceControl Resourcecontrol111;
		protected HelpIcon Helpicon2;
		protected ResourceControl Resourcecontrol113;
		protected MPContent TaskRegion;
		protected MPContainer MPContainer;
		protected ResourceLinkButton SaveButton;

		#endregion

		override protected void OnInit(EventArgs e)
		{

			this.SaveButton.Click += new EventHandler(SaveButton_Click);
			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()
		{
			SiteSettings settings = CSContext.Current.SiteSettings;
			Status.Visible = false;
			this.SaveButton.Text = ResourceManager.GetString( "Save" );

			Timezone.SelectedValue = settings.TimezoneOffset.ToString();
			if ( DateFormat.Items.FindByValue( settings.DateFormat ) != null ) 
			{
				ListItem item = DateFormat.Items.FindByValue( settings.DateFormat );
				DateFormat.SelectedIndex = DateFormat.Items.IndexOf(item);
			}
			TimeFormat.SelectedValue = settings.TimeFormat;

			ThreadDateFilter.SelectedValue = settings.DefaultThreadDateFilter;
		}

		private void SaveButton_Click(object sender, EventArgs e) 
		{
			SiteSettings settings = CSContext.Current.SiteSettings;

			SaveSettings( settings );

			if(Page.IsValid)
			{
				Status.Success = true;
				Status.ResourceName = "Admin_SiteSettings_StatusSuccess";

				DataBind();
			}
			else
			{
				Status.Success = false;
				Status.ResourceName = "Admin_SiteSettings_StatusFailed";
			}

			Status.Visible = true;
		}

		private void InitializeComponent()
		{
		
		}


		protected virtual void SaveSettings( SiteSettings settings ) 
		{

			settings.TimezoneOffset = Int32.Parse( Timezone.SelectedValue );
			settings.DateFormat = DateFormat.SelectedValue;
			settings.TimeFormat = TimeFormat.SelectedValue;
			settings.DefaultThreadDateFilter = ThreadDateFilter.SelectedValue;

			if(Page.IsValid)
				SiteSettingsManager.Save(settings);

		}

	}
}
