using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

[ValidationPropertyAttribute("Text")]
public partial class controls_Date : System.Web.UI.UserControl
{
    // TODO: 
    // Add property to customize error message
    // Handle width property

    /// <summary>
    /// Expose the value of the control
    /// </summary>
    public string Text
    {
        get
        {
            return txtDateTime.Text;
        }

        set
        {
            txtDateTime.Text = value;
        }
    }

    /// <summary>
    /// Control whether the field is required or not
    /// </summary>
    public bool FieldRequired
    {
        get
        {
            return DateRequiredFieldValidator.Enabled;
        }

        set
        {
            DateRequiredFieldValidator.Enabled = value;
        }
    }

    /// <summary>
    /// Control width
    /// </summary>
    public Unit Width
    {
        get
        {
            return Cal.Width;
        }

        set
        {
            Cal.Width = value;
            Unit ControlWidth = value;
            switch (ControlWidth.Type)
            {
                case UnitType.Pixel:
                    break;
                case UnitType.Percentage:
                    break;
    
            }
            // TODO: Set textbox width
        }
    }


    protected void Page_Load(object sender, EventArgs e)
    {
        
    }

    /// <summary>
    /// Toggle Calendar visibility each time the "..." button is clicked
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnCalendar_Click(object sender, EventArgs e)
    {
        Cal.Visible = !Cal.Visible;
    }

    /// <summary>
    /// Set textbox to selected date from calendar
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Cal_SelectionChanged(object sender, EventArgs e)
    {
        txtDateTime.Text = Cal.SelectedDate.ToString();
        Cal.Visible = false;
    }

    /// <summary>
    /// Server side validation of Date field
    /// </summary>
    /// <param name="source"></param>
    /// <param name="args"></param>
    protected void ValidateDate(object source, 
                                ServerValidateEventArgs args)
    {
        // Validate date only if the field is required OR is not empty
        if ((!string.IsNullOrEmpty(args.Value)) ||
             (FieldRequired))
        {
            DateTime dt;
            args.IsValid = DateTime.TryParse(args.Value, out dt);
        }

        
    }
}
