using AyCode.Utils.Extensions;
using System.Text.RegularExpressions;
using AyCode.Core.Extensions;

namespace AyCode.Core.Consts
{
    public static class AcValidate
    {
        public static bool IsValidEmailAndPasswordFormat(string? email, string? password, out AcErrorCode acErrorCode)
        {
            return IsValidEmailFormat(email, out acErrorCode) && IsValidPasswordFormat(password, out acErrorCode);
        }

        public static bool IsValidUserNameAndPasswordFormat(string username, string password, out AcErrorCode acErrorCode)
        {
            return IsValidUserNameFormat(username, out acErrorCode) && IsValidPasswordFormat(password, out acErrorCode);
        }

        public static bool IsValidUserNameFormat(string userName, out AcErrorCode acErrorCode)
        {
            acErrorCode = AcErrorCode.Unset;

            var isValid = !userName.IsNullOrWhiteSpace() && userName.Length is >= AcConst.MinUserNameLength and <= AcConst.MaxUserNameLength && userName.Trim() == userName;

            if (!isValid) acErrorCode = AcErrorCode.UserNameIsTooShort;
            return isValid;
        }

        public static bool IsValidPlayerNameFormat(string playerName, out AcErrorCode acErrorCode)
        {
            acErrorCode = AcErrorCode.Unset;

            var isValid = !playerName.IsNullOrWhiteSpace() && playerName.Length is >= AcConst.MinPlayerNameLength and <= AcConst.MaxPlayerNameLength && playerName.Trim() == playerName;

            if (!isValid) acErrorCode = AcErrorCode.PlayerNameIsTooShort;
            return isValid;
        }

        public static bool IsValidPasswordFormat(string? password, out AcErrorCode acErrorCode)
        {
            acErrorCode = AcErrorCode.Unset;

            var isValid = !string.IsNullOrWhiteSpace(password) && password.Length is >= AcConst.MinPasswordLength and <= AcConst.MaxPasswordLength;

            if (!isValid) acErrorCode = AcErrorCode.PasswordIsTooShort;
            return isValid;
        }

        public static bool IsValidEmailFormat(string? email, out AcErrorCode acErrorCode)
        {
            acErrorCode = AcErrorCode.Unset;

            //a@a.a -> length == 5
            //var isValid = !email.IsNullOrWhiteSpace() && email.Length >= 5 && email.Contains('@') && email.Contains('.') && !email.Contains(' ');
            var isValid = !string.IsNullOrWhiteSpace(email) && AcRegExpression.EmailRegex().IsMatch(email);

            if (!isValid) acErrorCode = AcErrorCode.EmailFormatIsNotValid;
            return isValid;
        }

        public static bool IsValidPhoneNumberFormat(string phoneNumber, out AcErrorCode acErrorCode)
        {
            acErrorCode = AcErrorCode.Unset;

            var isValid = AcRegExpression.PhoneNumberRegex().IsMatch(phoneNumber);

            if (!isValid) acErrorCode = AcErrorCode.PhoneNumberFormatIsNotValid;
            return isValid;
        }

        public static bool IsValidPlateNumberFormat(string? plateNumber, out AcErrorCode acErrorCode)
        {
            acErrorCode = AcErrorCode.Unset;

            var isValid = !string.IsNullOrWhiteSpace(plateNumber) && AcRegExpression.PlateNumberRegex().IsMatch(plateNumber);

            if (!isValid) acErrorCode = AcErrorCode.PlateNumberFormatIsNotValid;
            return isValid;
        }

        public static bool IsValidCountryCodeAlpha2Format(string? countryCode, out AcErrorCode acErrorCode)
        {
            acErrorCode = AcErrorCode.Unset;

            var isValid = !string.IsNullOrWhiteSpace(countryCode) && AcRegExpression.CountryCodeAlpha2Regex().IsMatch(countryCode);

            if (!isValid) acErrorCode = AcErrorCode.CountryCodeFormatIsNotValid;
            return isValid;
        }

        public static bool IsValidCountryCodeMax3Format(string? countryCode, out AcErrorCode acErrorCode)
        {
            acErrorCode = AcErrorCode.Unset;

            var isValid = !string.IsNullOrWhiteSpace(countryCode) && AcRegExpression.CountryCodeMax3Regex().IsMatch(countryCode);

            if (!isValid) acErrorCode = AcErrorCode.CountryCodeFormatIsNotValid;
            return isValid;
        }

        public static bool IsValidUserTokenFormat(string verificationToken, out AcErrorCode acErrorCode)
        {
            acErrorCode = AcErrorCode.Unset;

            var isValid = !verificationToken.IsNullOrWhiteSpace() && verificationToken.Length is >= AcConst.MinUserTokenLength and <= AcConst.MaxUserTokenLength;

            if (!isValid) acErrorCode = AcErrorCode.UserTokenIsNotValid;
            return isValid;
        }

        public static bool IsValidDomain(string domainName)
        {
            var suffixes = string.Join('|', AcConst.AvailableDomainSuffixes);
            var regexp = new Regex("^[a-z0-9]{3,90}\\.(" + suffixes + ")$"); // a pattern-t vegyük ki konstansba a dll-re - B.
            var match = regexp.IsMatch(domainName);

            return match;
        }
    }
}