﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ChatApp
{
    public enum CharacterSets
    {
        LOWER,
        UPPER,
        NUMBERS,
        SPECIAL
    }
    public static class RandomUtil
    {
        private static char[] chars_lower = "qwertyuiopasdfghjklzxcvbnm".ToCharArray();
        private static char[] chars_upper = "QWERTYUIOPASDFGHJKLZXCVBNM".ToCharArray();
        private static char[] chars_numbers = "1234567890".ToCharArray();
        private static char[] chars_special = "`¬¦!\"£$€%^&*()-_=+[{]};:\'@#~,<.>/?\\|".ToCharArray();
        private static Random rand = new Random();
        public static void Seed ( int seed ) { rand = new Random(seed); }
        public static char Choice ( char[] chars ){return chars[rand.Next(0, chars.Length)];}
        public static int Next(){return rand.Next();}
        public static int Next(int max,bool inclusive=true){if(inclusive){return rand.Next(max+1);}else{return rand.Next(max);}}
        public static int Next(int min, int max, bool inclusive=true){if(inclusive){return rand.Next(min,max+1);}else{return rand.Next(min,max);}}
        public static string RandString ( int length,params char[][] chars )
        {
            StringBuilder sb = new StringBuilder(length);
            int count = chars.Length;
            if (count > 0)
            {
                if (count == 1){for (int i = 0; i < length; i++){sb.Append(Choice(chars[0]));}
                }else{for (int i = 0; i < length; i++){sb.Append(Choice(chars[rand.Next(0,count)]));}}
            }
            return sb.ToString();
        }
        public static string RandString ( int length, CharacterSets char_sets = CharacterSets.LOWER&CharacterSets.UPPER&CharacterSets.NUMBERS )
        {
            byte char_val=0;
            if((char_sets&CharacterSets.LOWER)==CharacterSets.LOWER){char_val+=1;}
            if((char_sets&CharacterSets.UPPER)==CharacterSets.UPPER){char_val+=2;}
            if((char_sets&CharacterSets.NUMBERS)==CharacterSets.NUMBERS){char_val+=4;}
            if((char_sets&CharacterSets.SPECIAL)==CharacterSets.SPECIAL){char_val+=8;}
            if(char_val>0){
                switch (char_val)
                {
                    case 1: return RandString(length,chars_lower);
                    case 2: return RandString(length, chars_upper);
                    case 3: return RandString(length, chars_upper, chars_lower );
                    case 4:return RandString(length,chars_numbers);
                    case 5:return RandString(length,chars_numbers,chars_lower);
                    case 6:return RandString(length,chars_numbers,chars_upper);
                    case 7:return RandString(length,chars_numbers,chars_upper,chars_lower);
                    case 8:return RandString(length,chars_special);
                    case 9:return RandString(length,chars_special,chars_lower);
                    case 10:return RandString(length,chars_special,chars_upper);
                    case 11:return RandString(length,chars_special,chars_upper,chars_lower);
                    case 12:return RandString(length,chars_special,chars_numbers);
                    case 13:return RandString(length,chars_special,chars_numbers,chars_lower);
                    case 14:return RandString(length,chars_special,chars_numbers,chars_upper);
                    case 15:return RandString(length,chars_special,chars_numbers,chars_upper,chars_lower);
                    default: return string.Empty;
                }
            }else{return string.Empty;}
        }
    }
}