namespace RpgRoller.Services;

public static class RollBreakdownFormatter
{
    public static string BuildBreakdown(IEnumerable<int> diceValues, int modifier, int total)
    {
        var dicePart = string.Join("+", diceValues);
        if (string.IsNullOrWhiteSpace(dicePart))
            dicePart = "0";

        return BuildModifierBreakdown(dicePart, modifier, total);
    }

    public static string BuildRolemasterOpenEndedBreakdown(int initialRoll, IReadOnlyList<int> followUpRolls, bool subtractFollowUps, int modifier, int total)
    {
        return BuildRolemasterOpenEndedBreakdown(initialRoll, followUpRolls, subtractFollowUps, modifier, total, 0);
    }

    public static string BuildRolemasterOpenEndedBreakdown(int initialRoll, IReadOnlyList<int> followUpRolls, bool subtractFollowUps, int modifier, int total, int situationalModifier)
    {
        if (subtractFollowUps)
        {
            var segments = new List<string> { $"({FormatRolemasterTriggerRoll(initialRoll)})" };
            segments.AddRange(followUpRolls.Select(roll => $"-{roll}"));
            AddRolemasterModifierSegments(segments, modifier, situationalModifier);

            return $"{string.Join(" ", segments)} = {total}";
        }

        var core = initialRoll.ToString();
        if (followUpRolls.Count > 0)
        {
            var followUpBreakdown = string.Join("+", followUpRolls);
            core = $"{core}+{followUpBreakdown}";
        }

        return BuildRolemasterModifierBreakdown(core, modifier, situationalModifier, total);
    }

    public static string BuildRolemasterRetryBreakdown(string firstAttemptBreakdown, int retryBonus, string retryAttemptBreakdown, int finalTotal)
    {
        return $"{firstAttemptBreakdown}; retry(+{retryBonus}): {retryAttemptBreakdown}; final={finalTotal}";
    }

    public static string FormatRolemasterTriggerRoll(int roll)
    {
        return roll.ToString("00");
    }

    public static string BuildModifierBreakdown(string core, int modifier, int total)
    {
        return modifier switch
        {
            > 0 => $"{core}+{modifier}={total}",
            < 0 => $"{core}{modifier}={total}",
            _   => $"{core}={total}"
        };
    }

    public static string BuildRolemasterModifierBreakdown(IEnumerable<int> diceValues, int modifier, int situationalModifier, int total)
    {
        var dicePart = string.Join("+", diceValues);
        if (string.IsNullOrWhiteSpace(dicePart))
            dicePart = "0";

        return BuildRolemasterModifierBreakdown(dicePart, modifier, situationalModifier, total);
    }

    public static string BuildRolemasterModifierBreakdown(string core, int modifier, int situationalModifier, int total)
    {
        if (situationalModifier == 0)
            return BuildModifierBreakdown(core, modifier, total);

        return $"{core}{FormatSignedModifier(modifier)}{FormatSignedModifier(situationalModifier)}={total}";
    }

    private static void AddRolemasterModifierSegments(List<string> segments, int modifier, int situationalModifier)
    {
        if (modifier != 0)
            segments.Add(FormatSignedModifier(modifier));

        if (situationalModifier != 0)
            segments.Add(FormatSignedModifier(situationalModifier));
    }

    private static string FormatSignedModifier(int modifier)
    {
        return modifier switch
        {
            > 0 => $"+{modifier}",
            < 0 => modifier.ToString(),
            _   => string.Empty
        };
    }
}