using System;
using System.Linq;
using DatabaseEditor.DAL.Entities;

namespace DatabaseEditor.DAL
{
    public static class Randomizer
    {
        private const int BookNameLength = 12;
        private const int AuthorNameLength = 8;
        private const int AuthorSurnameLength = 8;
        private const int BookPagesMax = 1000;
        private const int BookYearMax = 2017;
        private const int BookYearMin = 1500;
        private const int IsbnLength = 13;

        private static readonly Random Random = new Random();

        /// <summary>
        /// Creates book with random name, pages, year and ISBN, but without authors.
        /// </summary>
        /// <returns></returns>
        public static Book RandomBook()
        {
            return new Book
            {
                Name = RandomText(BookNameLength),
                BookId = Guid.NewGuid(),
                ISBN = Guid.NewGuid().ToString().Substring(0, IsbnLength),
                Pages = Random.Next(0, BookPagesMax),
                Year = Random.Next(BookYearMin, BookYearMax)
            };
        }

        /// <summary>
        /// Creates author with random name, surname, but without books.
        /// </summary>
        /// <returns></returns>
        public static Author RandomAuthor()
        {
            return new Author
            {
                AuthorId = Guid.NewGuid(),
                Name = RandomText(AuthorNameLength),
                Surname = RandomText(AuthorSurnameLength)
            };
        }

        public static string RandomString(int length)
        {
            const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
            return new string(Enumerable.Repeat(chars, length)
              .Select(s => s[Random.Next(s.Length)]).ToArray());
        }

        public static string RandomText(int length)
        {
            const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
            return new string(Enumerable.Repeat(chars, length)
                .Select(s => s[Random.Next(s.Length)]).ToArray());
        }

        public static int RandomInt(int min, int max)
        {
            return Random.Next(min, max+1); // max value in Random.Next is exclusive
        }
    }
}