using AyCode.Core.Consts;

namespace AyCode.Core.Helpers
{
    public static class AcCharsGenerator
    {
        public static readonly char[] Letters;
        public static readonly char[] Numbers;
        public static readonly char[] LettersAndNumbers;

        static AcCharsGenerator()
        {
            //"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"

            Numbers = "0123456789".ToCharArray();
            Letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray();

            LettersAndNumbers = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".ToCharArray();
        }

        private static char[] GetRandomChars() => GetRandomChars(LettersAndNumbers, 8, 12);
        private static char[] GetRandomChars(int length) => GetRandomChars(LettersAndNumbers, length, length);
        private static char[] GetRandomChars(int minLength, int maxLength) => GetRandomChars(LettersAndNumbers, minLength, maxLength);
        private static char[] GetRandomChars(char[] sourceChars, int minLength, int maxLength)
        {
            if (minLength < 1) throw new ArgumentOutOfRangeException(nameof(minLength), "must have length greater than or equal to 1");
            var random = new Random();
            return Enumerable.Repeat(sourceChars, random.Next(minLength, maxLength)).Select(s => s[random.Next(s.Length)]).ToArray();
        }

        public static string NewToken()
        {
            return new string(GetRandomChars(AcConst.MinUserTokenLength, AcConst.MaxUserTokenLength));
        }

        public static string NewPassword(int minLength = AcConst.MinPasswordLength, int maxLength = AcConst.MaxPasswordLength)
        {
            return new string(GetRandomChars(AcConst.MinPasswordLength, AcConst.MaxPasswordLength));
        }
    }
}