using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.Text.RegularExpressions;

namespace EnterpriseDesktop.Common.Validation
{
    public static class CommonRules
    {
        /// <summary>
        /// Pass in the business object you wish to validate and the property you wish to validate against.
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="propertyName"></param>
        /// <returns></returns>
        public static bool StringRequired(object entity, string propertyName)
        {
            string value = null;
            try
            {
                value = (string)entity.GetType().GetProperty(propertyName).GetValue(entity, null);
            }
            catch
            {
                return false;
            }
            if (String.IsNullOrEmpty(value))
            {
                return false;
            }
            return true;
        }

        #region ValidateEmailAddress
        /// <summary>
        /// Validates that an email address is a well formed email address.
        /// </summary>
        /// <param name="email"></param>
        /// <returns>Boolean</returns>
        public static bool ValidateEmailAddress(object entity, string propertyName)
        {
            string value = null;
            try
            {
                value = (string)entity.GetType().GetProperty(propertyName).GetValue(entity, null);
            }
            catch
            {
                return false;
            }
            bool isValid = false;
            if (String.IsNullOrEmpty(value))
            {
                return false;
            }
            if (value.Length == 0)
            {
                return false;
            }

            isValid = Regex.IsMatch(value, @"^[\w-]+(?:\.[\w-]+)*@(?:[\w-]+\.)+[a-zA-Z]{2,7}$");
            if (isValid)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        #endregion

        #region ValidateName
        /// <summary>
        /// Makes sure a name has at least 2 characters in it.  Also validates the name 
        /// field explicitly, handles apostrophes.
        /// </summary>
        /// <param name="ctl"></param>
        /// <param name="errorProvider"></param>
        /// <returns></returns>
        public static bool ValidateName(object entity, string propertyName)
        {
            string value = null;
            try
            {
                value = (string)entity.GetType().GetProperty(propertyName).GetValue(entity, null);
            }
            catch
            {
                return false;
            }
            bool isValid = false;
            if (String.IsNullOrEmpty(value))
            {
                return false;
            }
            if (value.Length == 0)
            {
                return false;
            }

            isValid = Regex.IsMatch(value, @"^(?:[A-Z])(?:[a-zA-Z\.\'\`])(?:[a-zA-Z\.\,\-\'\ ]|)+$", RegexOptions.Compiled);

            if (isValid)
            {
                return true;
            }
            return false;
        }
        #endregion
    }
}
