using System;
using System.Reflection;
using System.Text.RegularExpressions;

namespace Mall.Common.Plugin
{
    /// <summary>
    /// 验证类
    /// </summary>
    public static class ValidateHepler
    {
        /// <summary>
        /// 返回ok为参数合法，其余为不合法
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string Validate(this object obj)
        {
            Type type = obj.GetType();
            PropertyInfo[] fields = type.GetProperties();
            foreach (var item in fields)
            {
                if (item.IsDefined(typeof(AbstractValidateAttribute), true))
                {
                    object[] validateAttribute = item.GetCustomAttributes(typeof(AbstractValidateAttribute), true);
                    foreach (var validate in validateAttribute)
                    {
                        AbstractValidateAttribute val = (AbstractValidateAttribute)validate;
                        string message = val.CheckValiadte(item.Name, item.GetValue(obj));
                        if (!message.Equals("ok"))
                        {
                            return message;
                        }
                    }
                }
            }
            return "ok";
        }

        /// <summary>
        /// 验证字符串是否数字
        /// </summary>
        /// <param name="val"></param>
        /// <returns></returns>
        public static bool IsNumber(string val)
        {
            if (val == "0")
                return true;
            if (string.IsNullOrEmpty(val))
                return false;
            return Regex.IsMatch(val, @"^[1-9]\d*\.?[0]*$");
        }

        /// <summary>
        /// 是否Email
        /// </summary>
        /// <param name="email"></param>
        /// <returns></returns>
        public static bool IsEmail(string email)
        {
            return Regex.IsMatch(email, @"^([0-9a-z]+[-._+&])*[0-9a-z]+@([-0-9a-z]+[.])+[a-z]{2,6}$", RegexOptions.IgnoreCase);
        }

        /// <summary>
        /// 是否网页地址
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public static bool IsURL(string url)
        {
            return Regex.IsMatch(url, @"^(http|https|ftp)\://[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(:[a-zA-Z0-9]*)?/?([a-zA-Z0-9\-\._\?\,\'/\\\+&%\$#\=~])*[^\.\,\)\(\s]$");
        }

        /// <summary>
        /// 是否手机号
        /// </summary>
        /// <param name="mobileNumber">手机号</param>
        /// <returns>是否有效的日期</returns>
        public static bool IsMobileNumber(string mobileNumber)
        {
            if (string.IsNullOrWhiteSpace(mobileNumber))
            {
                return false;
            }
            if (Regex.IsMatch(mobileNumber, "^1[3456789]\\d{9}$", RegexOptions.IgnoreCase))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 判断传入的参数是否有效的日期格式
        /// </summary>
        /// <param name="dateTime">需要判断的字符串</param>
        /// <returns>是否有效的日期</returns>
        public static bool IsDateTime(string dateTime)
        {
            try
            {
                DateTime dteConvert = DateTime.Parse(dateTime.ToString());
                return true;
            }
            catch (FormatException)
            {
                return false;
            }
        }
    }
    /// <summary>
    /// 属性特性验证
    /// </summary>
    [AttributeUsage(AttributeTargets.Property)]
    public abstract class AbstractValidateAttribute : System.Attribute
    {
        /// <summary>
        /// 成功提示信息
        /// </summary>
        protected string successMessage = "ok";

        /// <summary>
        /// 显示信息
        /// </summary>
        public string showMessage = "";

        /// <summary>
        /// 验证
        /// </summary>
        /// <param name="name"></param>
        /// <param name="value">值</param>
        /// <returns></returns>
        public abstract string CheckValiadte(string name, object value);
    }

    /// <summary>
    /// 整形数字范围验证
    /// </summary>
    public class IntRangeValidateAttribute : AbstractValidateAttribute
    {
        private int _min = 0;
        private int _max = 0;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="min"></param>
        /// <param name="max"></param>
        public IntRangeValidateAttribute(int min, int max)
        {
            this._min = min;
            this._max = max;
        }

        /// <summary>
        /// 整形数字范围验证
        /// </summary>
        /// <param name="name"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public override string CheckValiadte(string name, object value)
        {
            if (value == null || string.IsNullOrWhiteSpace(value.ToString()))
            {
                return successMessage;
            }
            int _value = (int)value;
            if (_value >= _min && _value <= _max)
            {

                return successMessage;
            }
            else
            {
                if (!string.IsNullOrWhiteSpace(showMessage))
                {
                    return showMessage;
                }
                else
                {
                    return $"参数{name}的值范围为{_min}到{_max}";
                }
            }
        }
    }

    /// <summary>
    /// 字符串必填验证
    /// </summary>
    public class RequiredValidateAttribute : AbstractValidateAttribute
    {
        /// <summary>
        /// 字符串验证
        /// </summary>
        /// <param name="name">提示名称</param>
        /// <param name="value">值</param>
        /// <returns></returns>
        public override string CheckValiadte(string name, object value)
        {
            if (value == null || string.IsNullOrWhiteSpace(value.ToString()))
            {
                if (!string.IsNullOrWhiteSpace(showMessage))
                {
                    return showMessage;
                }
                else
                {
                    return $"参数{name}为必填字段";
                }
            }
            else
            {
                return successMessage;
            }
        }
    }

    /// <summary>
    /// 字符串范围验证
    /// </summary>
    public class StringRangeValidateAttribute : AbstractValidateAttribute
    {
        private int _min = 0;
        private int _max = 0;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="min"></param>
        /// <param name="max"></param>
        public StringRangeValidateAttribute(int min, int max)
        {
            this._min = min;
            this._max = max;
        }

        /// <summary>
        /// 字符串范围验证
        /// </summary>
        /// <param name="name"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public override string CheckValiadte(string name, object value)
        {
            if (value == null || string.IsNullOrWhiteSpace(value.ToString()))
            {
                return successMessage;
            }
            string _value = value.ToString();
            if (_value.Length >= _min && _value.Length <= _max)
            {

                return successMessage;
            }
            else
            {
                if (!string.IsNullOrWhiteSpace(showMessage))
                {
                    return showMessage;
                }
                else
                {
                    return $"参数{name}的长度范围为{_min}到{_max}个字符";
                }
            }
        }
    }

    /// <summary>
    /// 数字校验
    /// </summary>
    public class IsNumberValidateAttribute : AbstractValidateAttribute
    {
        /// <summary>
        /// 数字校验
        /// </summary>
        /// <param name="name"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public override string CheckValiadte(string name, object value)
        {
            if (value == null || string.IsNullOrWhiteSpace(value.ToString()))
            {
                return successMessage;
            }
            string number = value.ToString();
            if (!Regex.IsMatch(number, @"^[0-9]\d*\.?[0]*$"))
            {
                if (!string.IsNullOrWhiteSpace(showMessage))
                {
                    return showMessage;
                }
                else
                {
                    return $"参数{name}必须为数字";
                }
            }
            else
            {
                return successMessage;
            }
        }
    }

    /// <summary>
    /// 邮箱校验
    /// </summary>
    public class IsEmailValidateAttribute : AbstractValidateAttribute
    {
        /// <summary>
        /// 邮箱校验
        /// </summary>
        /// <param name="name"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public override string CheckValiadte(string name, object value)
        {
            if (value == null || string.IsNullOrWhiteSpace(value.ToString()))
            {
                return successMessage;
            }
            string email = value.ToString();
            if (string.IsNullOrEmpty(email))
            {
                return showMessage;
            }
            else
            {
                if (!Regex.IsMatch(email, @"^([0-9a-z]+[-._+&])*[0-9a-z]+@([-0-9a-z]+[.])+[a-z]{2,6}$", RegexOptions.IgnoreCase))
                {
                    if (!string.IsNullOrWhiteSpace(showMessage))
                    {
                        return showMessage;
                    }
                    else
                    {
                        return $"参数{name}必须为合法邮箱";
                    }
                }
                else
                {
                    return successMessage;
                }
            }
        }
    }
    /// <summary>
    /// 时间类型验证
    /// </summary>
    public class IsDateTimeValidateAttribute : AbstractValidateAttribute
    {
        /// <summary>
        /// 时间类型验证
        /// </summary>
        /// <param name="name"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public override string CheckValiadte(string name, object value)
        {
            if (value == null || string.IsNullOrWhiteSpace(value.ToString()))
            {
                return successMessage;
            }
            try
            {
                Convert.ToDateTime(value.ToString());
                return successMessage;
            }
            catch (Exception)
            {
                return $"参数{name}必须为日期格式！";
            }
        }
    }

    /// <summary>
    /// 正则表达式验证
    /// </summary>
    public class RegularValidateAttribute : AbstractValidateAttribute
    {
        private string pattern = "";
        /// <summary>
        ///
        /// </summary>
        /// <param name="pattern">验证规则</param>
        public RegularValidateAttribute(string pattern)
        {
            this.pattern = pattern;
        }

        /// <summary>
        /// 正则验证
        /// </summary>
        /// <param name="name"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public override string CheckValiadte(string name, object value)
        {
            if (value == null || string.IsNullOrWhiteSpace(value.ToString()))
            {
                return successMessage;
            }
            if (!Regex.IsMatch(value.ToString(), pattern, RegexOptions.IgnoreCase))
            {
                if (!string.IsNullOrWhiteSpace(showMessage))
                {
                    return showMessage;
                }
                else
                {
                    return $"参数{name}必不合法";
                }
            }
            else
            {
                return successMessage;
            }
        }
    }
}