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

namespace FishDice.LuckyDice
{
    public sealed class RollActionResult
    {
        private RollActionResult(
            string actionType,
            bool success,
            IReadOnlyList<RollEffect> effects,
            string outcome,
            string error)
        {
            ActionType = actionType;
            Success = success;
            Effects = effects == null ? Array.Empty<RollEffect>() : effects.ToArray();
            Outcome = outcome;
            Error = error;
        }

        public string ActionType { get; }

        public bool Success { get; }

        public IReadOnlyList<RollEffect> Effects { get; }

        public string Outcome { get; }

        public string Error { get; }

        public static RollActionResult Succeeded(string actionType, string outcome)
        {
            return new RollActionResult(actionType, true, Array.Empty<RollEffect>(), outcome, null);
        }

        public static RollActionResult Succeeded(string actionType, IReadOnlyList<RollEffect> effects)
        {
            var outcome = effects == null || effects.Count == 0 ? null : effects[effects.Count - 1].EffectType;
            return new RollActionResult(actionType, true, effects, outcome, null);
        }

        public static RollActionResult Failure(string actionType, string error)
        {
            return new RollActionResult(actionType, false, Array.Empty<RollEffect>(), null, error);
        }
    }
}
