using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace EnterpriseDesktop.Common.WinForm.Validation
{
    /// <summary>
    /// This class utilizes the common library of CommonRules to validate information.
    /// This class should be utilized on winforms where you need to validate the value
    /// of a control with a textchanged event.
    /// </summary>
    public class ControlValidation
    {
        /// <summary>
        /// Validates that an email address in a control is a well formed email address.
        /// </summary>
        /// <param name="email"></param>
        /// <returns>Boolean</returns>
        public static bool ValidateEmailAddressControl(Control ctl, ErrorProvider errorProvider)
        {
            bool isValid = false;
            if (ctl.Text.Length == 0)
            {
                // a blank email is a valid email.
                errorProvider.SetError(ctl, "");
                return true;
            }

            isValid = Common.Validation.CommonRules.ValidateEmailAddress(ctl, "Text");
            if (isValid)
            {
                errorProvider.SetError(ctl, "");
                return true;
            }
            else
            {
                errorProvider.SetError(ctl, "Please enter a valid email address.  Example:  asdf@asdf.com.");
                return false;
            }
        }

        /// <summary>
        /// This method will validate the name of person via any control that has a Text property.
        /// It makes sure the name has at least 2 characters in it and is upper cased properly.  Also validates the name 
        /// field explicitly, handles apostrophes.  Use on controls where the name is required.
        /// </summary>
        /// <param name="ctl"></param>
        /// <param name="errorProvider"></param>
        /// <returns></returns>
        public static bool ValidateNameControl(Control ctl, ErrorProvider errorProvider)
        {
            bool isValid = Common.Validation.CommonRules.ValidateName(ctl, "Text");
            if (isValid)
            {
                errorProvider.SetError(ctl, "");
                return true;
            }
            errorProvider.SetError(ctl, "Please enter a valid name. Each name should contain at least 2 characters and be well formed including upper and lower case.");
            return false;
        }
    }
}
