using System;
using System.Collections.Generic;

namespace JiaZhiQuan.Common
{
    public static class Extensions
    {
        private static Random rng = new Random();

        /// <summary>
        /// 随机排列
        /// </summary>
        public static void Shuffle<T>(this IList<T> list)
        {
            int n = list.Count;
            while (n > 1)
            {
                n--;
                int k = rng.Next(n + 1);
                T value = list[k];
                list[k] = list[n];
                list[n] = value;
            }
        }

        /// <summary>
        /// 如：通过110101，可获得 110101,1101,11
        /// </summary>
        /// <param name="relatedRegionCodes"></param>
        /// <returns></returns>
        public static List<string> Regions(this string relatedRegionCodes)
        {
            List<string> list = new List<string>();
            if (string.IsNullOrEmpty(relatedRegionCodes))
            {
                return list;
            }
            var codes = relatedRegionCodes.Split(',', StringSplitOptions.RemoveEmptyEntries);
            foreach (var code in codes)
            {
                if (code.Length == 6)
                {
                    list.Add(code);
                    list.Add(code.Substring(0, 4));
                    list.Add(code.Substring(0, 2));
                }
            }
            return list;
        }


        public static byte[] FromBase64(this string base64WithoutPadding)
        {
            var base64 = base64WithoutPadding.Length % 4 == 0
                   ? base64WithoutPadding
                   : base64WithoutPadding + new string('=', 4 - base64WithoutPadding.Length % 4);

            return Convert.FromBase64String(base64.Replace("-", "+").Replace('_', '/'));
        }
    }


}
