using System.Collections.Generic;
using System.Linq;
using RolemasterDb.App.Features;

namespace RolemasterDb.App.Components.Shared;

public static class CriticalCellPresentation
{
    public static string BuildSourceImageAltText(CriticalCellEditorModel model)
    {
        var segments = new List<string>
        {
            model.TableName,
            $"roll band {model.RollBand}",
            $"column {model.ColumnLabel}"
        };

        if (!string.IsNullOrWhiteSpace(model.GroupLabel))
        {
            segments.Add($"variant {model.GroupLabel}");
        }

        return string.Join(", ", segments);
    }

    public static IReadOnlyList<CriticalEffectLookupResponse> BuildPreviewEffects(CriticalCellEditorModel model) =>
        model.Effects
            .Select(CreatePreviewEffect)
            .ToList();

    public static IReadOnlyList<CriticalBranchLookupResponse> BuildPreviewBranches(CriticalCellEditorModel model) =>
        model.Branches
            .OrderBy(branch => branch.SortOrder)
            .Select(CreatePreviewBranch)
            .ToList();

    private static CriticalEffectLookupResponse CreatePreviewEffect(CriticalEffectEditorModel effect) =>
        new(
            effect.EffectCode,
            effect.Target,
            effect.ValueInteger,
            effect.ValueExpression,
            effect.DurationRounds,
            effect.PerRound,
            effect.Modifier,
            effect.BodyPart,
            effect.IsPermanent,
            effect.SourceType,
            effect.SourceText);

    private static CriticalBranchLookupResponse CreatePreviewBranch(CriticalBranchEditorModel branch) =>
        new(
            branch.BranchKind,
            branch.ConditionKey,
            branch.ConditionText,
            branch.DescriptionText,
            branch.RawAffixText,
            branch.Effects
                .Select(CreatePreviewEffect)
                .ToList(),
            branch.RawText,
            branch.SortOrder);
}
