using System;
using System.Collections.Generic;

namespace FishDice.LuckyDice
{
    public interface IRandomSource
    {
        int Next(int minInclusive, int maxExclusive);
    }

    public sealed class SystemRandomSource : IRandomSource
    {
        private readonly Random _random;

        public SystemRandomSource()
            : this(new Random())
        {
        }

        public SystemRandomSource(Random random)
        {
            _random = random ?? throw new ArgumentNullException(nameof(random));
        }

        public int Next(int minInclusive, int maxExclusive)
        {
            return _random.Next(minInclusive, maxExclusive);
        }
    }

    public sealed class DiceRollService
    {
        private readonly IRandomSource _random;

        public DiceRollService(IRandomSource random)
        {
            _random = random ?? throw new ArgumentNullException(nameof(random));
        }

        public DiceRollResult Roll(DiceSetConfig config, string rollSessionId, string triggerSource)
        {
            if (config == null)
            {
                throw new ArgumentNullException(nameof(config));
            }

            var rolledFaces = new List<DiceFace>(config.DiceCount);
            for (var i = 0; i < config.DiceCount; i++)
            {
                var index = _random.Next(0, config.Faces.Count);
                rolledFaces.Add(config.Faces[index]);
            }

            return DiceRollResult.FromConfig(
                rollSessionId,
                config,
                rolledFaces,
                multiplier: 1,
                triggerSource: triggerSource);
        }
    }
}
