using System.Text.RegularExpressions;
using Blazorise;

namespace FoodsharingSiegen.Shared.Helper
{
    /// <summary>
    /// The validation helper class (a. beging, 20.05.2022)
    /// </summary>
    public static class ValidationHelper
    {
        #region Public Method ValidateMail

        /// <summary>
        /// Validates the mail using the specified args (a. beging, 11.04.2022)
        /// </summary>
        /// <param name="args">The args</param>
        public static void ValidateMail(ValidatorEventArgs args)
        {
            var email = Convert.ToString(args.Value);

            if (string.IsNullOrWhiteSpace(email))
            {
                args.Status = ValidationStatus.None;
                return;
            }

            var isMatch = Regex.IsMatch(email, "^[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,6}$", RegexOptions.IgnoreCase);

            args.Status = isMatch ? ValidationStatus.Success : ValidationStatus.Error;
        }

        #endregion

        #region Public Method ValidatePassword

        /// <summary>
        /// Validates the password using the specified args (a. beging, 11.04.2022)
        /// </summary>
        /// <param name="args">The args</param>
        public static void ValidatePassword(ValidatorEventArgs args)
        {
            var password = Convert.ToString(args.Value);
            if (string.IsNullOrWhiteSpace(password))
            {
                args.Status = ValidationStatus.None;
                return;
            }

            var isValid = password.Length >= 8 && password.Any(char.IsDigit);

            args.Status = isValid ? ValidationStatus.Success : ValidationStatus.Error;
        }

        #endregion
    }
}