using System.Text.RegularExpressions;

namespace QYZH.InteractiveMagazine.Common.Helpers
{
    /// <summary>
    /// 参数校验工具类
    /// </summary>
    public static class ValidateHelper
    {
        /// <summary>
        /// 邮箱正则表达式
        /// </summary>
        private const string EmailPattern = @"^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$";

        /// <summary>
        /// 手机号正则表达式(中国大陆)
        /// </summary>
        private const string PhonePattern = @"^1[3-9]\d{9}$";

        /// <summary>
        /// 身份证号正则表达式(中国大陆)
        /// </summary>
        private const string IdCardPattern = @"^(^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$)|(^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])((\d{4})|\d{3}[Xx])$)$";

        /// <summary>
        /// 校验邮箱格式
        /// </summary>
        /// <param name="email">邮箱地址</param>
        /// <returns>是否有效</returns>
        public static bool IsEmail(string email)
        {
            if (string.IsNullOrWhiteSpace(email))
            {
                return false;
            }

            return Regex.IsMatch(email, EmailPattern);
        }

        /// <summary>
        /// 校验手机号格式
        /// </summary>
        /// <param name="phone">手机号</param>
        /// <returns>是否有效</returns>
        public static bool IsPhone(string phone)
        {
            if (string.IsNullOrWhiteSpace(phone))
            {
                return false;
            }

            return Regex.IsMatch(phone, PhonePattern);
        }

        /// <summary>
        /// 校验身份证号格式
        /// </summary>
        /// <param name="idCard">身份证号</param>
        /// <returns>是否有效</returns>
        public static bool IsIdCard(string idCard)
        {
            if (string.IsNullOrWhiteSpace(idCard))
            {
                return false;
            }

            return Regex.IsMatch(idCard, IdCardPattern);
        }

        /// <summary>
        /// 校验URL格式
        /// </summary>
        /// <param name="url">URL地址</param>
        /// <returns>是否有效</returns>
        public static bool IsUrl(string url)
        {
            if (string.IsNullOrWhiteSpace(url))
            {
                return false;
            }

            const string urlPattern = @"^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$";
            return Regex.IsMatch(url, urlPattern);
        }

        /// <summary>
        /// 校验邮政编码格式
        /// </summary>
        /// <param name="zipCode">邮政编码</param>
        /// <returns>是否有效</returns>
        public static bool IsZipCode(string zipCode)
        {
            if (string.IsNullOrWhiteSpace(zipCode))
            {
                return false;
            }

            const string zipPattern = @"^\d{6}$";
            return Regex.IsMatch(zipCode, zipPattern);
        }
    }
}
