using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.RegularExpressions;
using System.Web;

namespace PushPlatform.Common.Util
{
    /// <summary>
    /// 正则验证辅助类
    /// </summary>
    public class RegexHelper
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public RegexHelper()
        {

        }

        #region 验证email
        /// <summary>
        /// 验证email
        /// </summary>
        /// <param name="email">要验证的字符</param>
        /// <returns>true验证通过，false验证失败</returns>
        public static bool IsEmail(string input)
        {
            return Regex.IsMatch(input, @"^([w-.]+)@(([[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.)|(([w-]+.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(]?)$");
        }
        #endregion

        #region 验证纯英文A-Z，a-z
        /// <summary>
        /// 验证纯英文A-Z，a-z
        /// </summary>
        /// <param name="input">要验证的字符</param>
        /// <returns>true验证通过，false验证失败</returns>
        public static bool IsEnglish(string input)
        {
            return Regex.IsMatch(input, @"^[A-Za-z]+$");
        }
        #endregion

        #region 纯汉字验证
        /// <summary>
        /// 纯汉字验证
        /// </summary>
        /// <param name="input">要验证的字符</param>
        /// <returns>true验证通过，false验证失败</returns>
        public static bool IsChinese(string input)
        {
            return Regex.IsMatch(input, @"^[\u4e00-\u9fa5]{0,}$");
        }
        #endregion

        #region 验证URL地址
        /// <summary>
        /// 验证URL地址
        /// </summary>
        /// <param name="input">要验证的字符</param>
        /// <returns>true验证通过，false验证失败</returns>
        public static bool IsUrl(string input)
        {
            return Regex.IsMatch(input, @"^http://([\w-]+\.)+[\w-]+(/[\w-./?%&=]*)?$");
        }
        #endregion

        #region 验证IP地址
        /// <summary>
        /// 验证ip地址
        /// </summary>
        /// <param name="input">要验证的ip</param>
        /// <returns></returns>
        public static bool IsIp(string input)
        {
            return Regex.IsMatch(input, @"((25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d)))\.){3}(25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d)))");
        }
        #endregion

        #region 验证日期格式
        /// <summary>
        /// 验证日期格式
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static bool IsDate(string input)
        {
            return Regex.IsMatch(input, "\\d{4}(\\-|\\/|.)\\d{1,2}\\1\\d{1,2}");
        }
        #endregion

        #region 替换HTML标签
        /// <summary>
        /// 替换HTML标签
        /// </summary>
        /// <param name="input">包含HTML的字符串</param>
        /// <returns></returns>
        public static string ReplaceHtml(string input)
        {
            //input.Replace("\\r\\n", "%r%n").Replace("<br>","%br%").Replace("<br/>","%br&%").Replace("\\n","%n");
            //删除脚本
            input = Regex.Replace(input, @"<script[^>]*?>.*?</script>", "",
              RegexOptions.IgnoreCase);
            //删除HTML
            input = Regex.Replace(input, @"<(.[^>]*)>", "",
              RegexOptions.IgnoreCase);

            input = Regex.Replace(input, @"-->", "", RegexOptions.IgnoreCase);
            input = Regex.Replace(input, @"<!--.*", "", RegexOptions.IgnoreCase);
            input = Regex.Replace(input, @"&(quot|#34);", "\"",
              RegexOptions.IgnoreCase);
            input = Regex.Replace(input, @"&(amp|#38);", "&",
              RegexOptions.IgnoreCase);
            input = Regex.Replace(input, @"&(lt|#60);", "<",
              RegexOptions.IgnoreCase);
            input = Regex.Replace(input, @"&(gt|#62);", ">",
              RegexOptions.IgnoreCase);
            input = Regex.Replace(input, @"&(nbsp|#160);", " ",
              RegexOptions.IgnoreCase);
            input = Regex.Replace(input, @"&(iexcl|#161);", "\xa1",
              RegexOptions.IgnoreCase);
            input = Regex.Replace(input, @"&(cent|#162);", "\xa2",
              RegexOptions.IgnoreCase);
            input = Regex.Replace(input, @"&(pound|#163);", "\xa3",
              RegexOptions.IgnoreCase);
            input = Regex.Replace(input, @"&(copy|#169);", "\xa9",
              RegexOptions.IgnoreCase);
            input = Regex.Replace(input, @"&#(\d+);", "",
              RegexOptions.IgnoreCase);

            input.Replace("<", "");
            input.Replace(">", "");
            input = HttpUtility.HtmlEncode(input);
            input = Regex.Replace(input, @"((\r\n))", "<br/>");
            input = Regex.Replace(input, @"(\r|\n)", "<br/>");
            input = Regex.Replace(input, @"(\s)", " ");
            return input;
        }
        #endregion

        #region 替换指定字符
        /// <summary>
        /// 替换指定字符
        /// </summary>
        /// <param name="regExpression">正则表达式</param>
        /// <param name="input">要替换字符串</param>
        /// <param name="value">替换值</param>
        /// <returns></returns>
        public static string Replace(string regExpression, string input, string value)
        {
            Regex reg = new Regex(regExpression, RegexOptions.IgnoreCase);

            return reg.Replace(input, value);
        }
        #endregion
    }
}
