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

using System;
using System.Globalization;
using System.Web.UI.WebControls;
using CommunityServer.Components;
using CA = ComponentArt.Web.UI;

namespace CommunityServer.Controls
{
	/// <summary>
	/// Summary description for DatePickerToggle.
	/// </summary>
	public class DatePickerToggle: TemplatedWebControl
	{
		private RadioButton disableDate;
		private RadioButton enableDate;
		private CA.Calendar datePicker;
		private DateTime _selectedDate;
		private bool _dateEnabled;

		public DatePickerToggle()
		{
		}
		
		#region Overridden methods
		protected override void AttachChildControls()
		{
			disableDate = FindControl("DisableDate") as RadioButton;
			enableDate = FindControl("EnableDate") as RadioButton;
			datePicker = FindControl("DatePicker") as CA.Calendar;
		}
		
		protected override void OnLoad(EventArgs e)
		{
			CSContext csContext = CSContext.Current;

			base.OnLoad (e);
			EnsureChildControls();

			if (!Page.IsPostBack)
			{
				//noDate.ResourceName = "Utility_DatePickerToggle_DisplayNoDate";
				if (datePicker != null)
				{
					datePicker.ControlType = CA.CalendarControlType.Picker;
					datePicker.Culture = new CultureInfo(csContext.User.Profile.Language);
					datePicker.PickerFormat = CA.DateTimeFormatType.Custom;
					datePicker.PickerCustomFormat = csContext.User.Profile.DateFormat;
					datePicker.SelectedDate = _selectedDate == DateTime.MinValue ? datePicker.MinDate : _selectedDate;
				}
				if(_dateEnabled)
				{
					enableDate.Checked = true;
				}
				else
				{
					disableDate.Checked = true;
				}
				disableDate.Text = ResourceManager.GetString("Utility_DatePickerToggle_DisplayNoDate");
			}
		}
		#endregion

		#region Properties
		public DateTime SelectedDate
		{
			get
			{
				if(enableDate.Checked)
				{
					return datePicker.SelectedDate;
				}
				else
				{
					return DateTime.MinValue;
				}
			}

			set
			{
				if(value==DateTime.MinValue)
				{
					_dateEnabled = false;
					_selectedDate = DateTime.MinValue;
				}
				else
				{
					_dateEnabled = true;
					_selectedDate = value;
				}
			}
		}
		#endregion
	}
}
