using System;
using System.Collections.Generic;
using System.Linq;

namespace FishDice.LuckyDice
{
    public sealed class TargetModeResolver
    {
        public TargetModeResolveResult Resolve(
            LuckyDiceSelectedResult selected,
            RollFlowRequest request,
            int multiplier,
            IReadOnlyList<TargetModeConfig> definitions)
        {
            if (selected == null)
            {
                return TargetModeResolveResult.Failed("MissingLuckyDiceCandidate");
            }

            var definition = (definitions ?? Array.Empty<TargetModeConfig>())
                .FirstOrDefault(item =>
                    item.ResultKey == selected.ResultKey &&
                    item.TargetKey == selected.TargetKey);

            if (definition == null)
            {
                return TargetModeResolveResult.Failed("MissingTargetMode");
            }

            return TargetModeResolveResult.Resolved(TargetModeEntry.From(definition, request, multiplier));
        }
    }
}
