using PlcSiemens.Core.Extension;

namespace PlcSiemens.Core.Common
{
    /// <summary>
    /// 参数检查
    /// </summary>
    public static class Argument
    {
        /// <summary>
        /// 非法Guid
        /// </summary>
        /// <param name="argument">Guid变量</param>
        /// <param name="argumentName">Guid变量名</param>
        /// <exception cref="ArgumentException">非法参数</exception>
        /// <example>Argument.IsNotEmptyGuid(argument,argumentName)</example>
        public static void IsNotEmptyGuid(Guid argument, string argumentName)
        {
            if (argument.IsEmpty())
            {
                throw new ArgumentException("\"{0}\" cannot be empty guid.".FormatWith(argumentName), argumentName);
            }
        }

        /// <summary>
        /// 非法string
        /// </summary>
        /// <param name="argument">Guid变量</param>
        /// <param name="argumentName">Guid变量名</param>
        /// <exception cref="ArgumentException">非法参数</exception>
        /// <example>Argument.IsNotEmpty(argument,argumentName)</example>
        public static void IsNotEmpty(string argument, string argumentName)
        {
            if (argument.IsNullOrEmpty())
            {
                throw new ArgumentException("\"{0}\" cannot be blank.".FormatWith(argumentName), argumentName);
            }
        }

        /// <summary>
        /// string超出长度
        /// </summary>
        /// <param name="argument">Guid变量</param>
        /// <param name="length">最大长度</param>
        /// <param name="argumentName">Guid变量名</param>
        /// <exception cref="ArgumentException">非法参数</exception>
        /// <example>Argument.IsNotOutOfLength(argument,length,argumentName)</example>
        public static void IsNotOutOfLength(string argument, int length, string argumentName)
        {
            IsNotEmpty(argument, argumentName);
            if (argument.Trim().Length > length)
            {
                throw new ArgumentOutOfRangeException(
                    "\"{0}\" cannot be more than {1} character.".FormatWith(argumentName, length), argumentName);
            }
        }

        /// <summary>
        /// 检查对象不为空
        /// </summary>
        /// <param name="argument"></param>
        /// <param name="argumentName"></param>
        /// <exception cref="ArgumentNullException">非法参数</exception>
        /// <example>Argument.IsNotNull(argument,argumentName)</example>
        public static void IsNotNull(object argument, string argumentName)
        {
            if (argument == null)
            {
                throw new ArgumentNullException(argumentName);
            }
        }

        /// <summary>
        /// 检查int对象不为负数
        /// </summary>
        /// <param name="argument"></param>
        /// <param name="argumentName"></param>
        /// <exception cref="ArgumentOutOfRangeException">非法参数</exception>
        /// <example>Argument.IsNotNegative(argument,argumentName)</example>
        public static void IsNotNegative(int argument, string argumentName)
        {
            if (argument < 0)
            {
                throw new ArgumentOutOfRangeException(argumentName);
            }
        }

        /// <summary>
        /// 检查int对象大于0
        /// </summary>
        /// <param name="argument"></param>
        /// <param name="argumentName"></param>
        /// <exception cref="ArgumentOutOfRangeException">非法参数</exception>
        /// <example>Argument.IsNotNegativeOrZero(argument,argumentName)</example>
        public static void IsNotNegativeOrZero(int argument, string argumentName)
        {
            if (argument <= 0)
            {
                throw new ArgumentOutOfRangeException(argumentName);
            }
        }

        /// <summary>
        /// 检查long对象不为负数
        /// </summary>
        /// <param name="argument"></param>
        /// <param name="argumentName"></param>
        /// <exception cref="ArgumentOutOfRangeException">非法参数</exception>
        /// <example>Argument.IsNotNegative(argument,argumentName)</example>
        public static void IsNotNegative(long argument, string argumentName)
        {
            if (argument < 0)
            {
                throw new ArgumentOutOfRangeException(argumentName);
            }
        }

        /// <summary>
        /// 检查long对象大于0
        /// </summary>
        /// <param name="argument"></param>
        /// <param name="argumentName"></param>
        /// <exception cref="ArgumentOutOfRangeException">非法参数</exception>
        /// <example>Argument.IsNotNegativeOrZero(argument,argumentName)</example>
        public static void IsNotNegativeOrZero(long argument, string argumentName)
        {
            if (argument <= 0)
            {
                throw new ArgumentOutOfRangeException(argumentName);
            }
        }

        /// <summary>
        /// 检查float对象不为负数
        /// </summary>
        /// <param name="argument"></param>
        /// <param name="argumentName"></param>
        /// <exception cref="ArgumentOutOfRangeException">非法参数</exception>
        /// <example>Argument.IsNotNegative(argument,argumentName)</example>
        public static void IsNotNegative(float argument, string argumentName)
        {
            if (argument < 0)
            {
                throw new ArgumentOutOfRangeException(argumentName);
            }
        }

        /// <summary>
        /// 检查float对象大于0
        /// </summary>
        /// <param name="argument"></param>
        /// <param name="argumentName"></param>
        /// <exception cref="ArgumentOutOfRangeException">非法参数</exception>
        /// <example>Argument.IsNotNegativeOrZero(argument,argumentName)</example>
        public static void IsNotNegativeOrZero(float argument, string argumentName)
        {
            if (argument <= 0)
            {
                throw new ArgumentOutOfRangeException(argumentName);
            }
        }

        /// <summary>
        /// 检查decimal对象不为负数
        /// </summary>
        /// <param name="argument"></param>
        /// <param name="argumentName"></param>
        /// <exception cref="ArgumentOutOfRangeException">非法参数</exception>
        /// <example>Argument.IsNotNegative(argument,argumentName)</example>
        public static void IsNotNegative(decimal argument, string argumentName)
        {
            if (argument < 0)
            {
                throw new ArgumentOutOfRangeException(argumentName);
            }
        }

        /// <summary>
        /// 检查decimal对象大于0
        /// </summary>
        /// <param name="argument"></param>
        /// <param name="argumentName"></param>
        /// <exception cref="ArgumentOutOfRangeException">非法参数</exception>
        /// <example>Argument.IsNotNegativeOrZero(argument,argumentName)</example>
        public static void IsNotNegativeOrZero(decimal argument, string argumentName)
        {
            if (argument <= 0)
            {
                throw new ArgumentOutOfRangeException(argumentName);
            }
        }

        /// <summary>
        /// 检查时间是否有效
        /// </summary>
        /// <param name="argument"></param>
        /// <param name="argumentName"></param>
        /// <exception cref="ArgumentOutOfRangeException">非法参数</exception>
        /// <example>Argument.IsNotInvalidDate(argument,argumentName)</example>
        public static void IsNotInvalidDate(DateTime argument, string argumentName)
        {
            if (!argument.IsValid())
            {
                throw new ArgumentOutOfRangeException(argumentName);
            }
        }

        /// <summary>
        /// 检查时间过期
        /// </summary>
        /// <param name="argument"></param>
        /// <param name="argumentName"></param>
        /// <exception cref="ArgumentOutOfRangeException">非法参数</exception>
        /// <example>Argument.IsNotInPast(argument,argumentName)</example>
        public static void IsNotInPast(DateTime argument, string argumentName)
        {
            if (argument < SystemTime.Now())
            {
                throw new ArgumentOutOfRangeException(argumentName);
            }
        }

        /// <summary>
        /// 检查时间超期
        /// </summary>
        /// <param name="argument"></param>
        /// <param name="argumentName"></param>
        /// <exception cref="ArgumentOutOfRangeException">非法参数</exception>
        /// <example>Argument.IsNotInFuture(argument,argumentName)</example>
        public static void IsNotInFuture(DateTime argument, string argumentName)
        {
            if (argument > SystemTime.Now())
            {
                throw new ArgumentOutOfRangeException(argumentName);
            }
        }

        /// <summary>
        /// 检查TimeSpan是否有效
        /// </summary>
        /// <param name="argument"></param>
        /// <param name="argumentName"></param>
        /// <exception cref="ArgumentOutOfRangeException">非法参数</exception>
        /// <example>Argument.IsNotNegative(argument,argumentName)</example>
        public static void IsNotNegative(TimeSpan argument, string argumentName)
        {
            if (argument < TimeSpan.Zero)
            {
                throw new ArgumentOutOfRangeException(argumentName);
            }
        }

        /// <summary>
        /// 检查TimeSpan是否有效
        /// </summary>
        /// <param name="argument"></param>
        /// <param name="argumentName"></param>
        /// <exception cref="ArgumentOutOfRangeException">非法参数</exception>
        /// <example>Argument.IsNotNegativeOrZero(argument,argumentName)</example>
        public static void IsNotNegativeOrZero(TimeSpan argument, string argumentName)
        {
            if (argument <= TimeSpan.Zero)
            {
                throw new ArgumentOutOfRangeException(argumentName);
            }
        }

        /// <summary>
        /// 检查ICollection不为空
        /// </summary>
        /// <param name="argument"></param>
        /// <param name="argumentName"></param>
        /// <exception cref="ArgumentException">非法参数</exception>
        /// <example>Argument.IsNotEmpty(argument,argumentName)</example>
        public static void IsNotEmpty<T>(ICollection<T> argument, string argumentName)
        {
            IsNotNull(argument, argumentName);

            if (argument.Count == 0)
            {
                throw new ArgumentException("Collection cannot be empty.", argumentName);
            }
        }

        /// <summary>
        /// 检查int值是否超出范围
        /// </summary>
        /// <param name="argument"></param>
        /// <param name="min"></param>
        /// <param name="max"></param>
        /// <param name="argumentName"></param>
        /// <exception cref="ArgumentOutOfRangeException"></exception>
        /// <example>Argument.IsNotOutOfRange(argument,min,max,argumentName)</example>
        public static void IsNotOutOfRange(int argument, int min, int max, string argumentName)
        {
            if ((argument < min) || (argument > max))
            {
                throw new ArgumentOutOfRangeException(argumentName,
                    "{0} must be between \"{1}\"-\"{2}\".".FormatWith(argumentName, min, max));
            }
        }

        /// <summary>
        /// 非法邮箱地址
        /// </summary>
        /// <param name="argument"></param>
        /// <param name="argumentName"></param>
        /// <exception cref="ArgumentException"></exception>
        /// <example>Argument.IsNotOutOfRange(argument ,argumentName)</example>
        public static void IsNotInvalidEmail(string argument, string argumentName)
        {
            IsNotEmpty(argument, argumentName);

            if (!argument.IsEmail())
            {
                throw new ArgumentException("\"{0}\" is not a valid email address.".FormatWith(argumentName),
                    argumentName);
            }
        }

        /// <summary>
        /// 非法Web地址
        /// </summary>
        /// <param name="argument"></param>
        /// <param name="argumentName"></param>
        /// <exception cref="ArgumentException"></exception>
        /// <example>Argument.IsNotOutOfRange(argument ,argumentName)</example>
        public static void IsNotInvalidWebUrl(string argument, string argumentName)
        {
            IsNotEmpty(argument, argumentName);

            if (!argument.IsWebUrl())
            {
                throw new ArgumentException("\"{0}\" is not a valid web url.".FormatWith(argumentName), argumentName);
            }
        }
    }
}
