import dice

from rollbot import as_command
from rollbot.injection import Args, ArgListSplitOn


@as_command
def roll(argstr: Args, args: ArgListSplitOn(",")):
    try:
        rolls = []
        for roll_exp in args:
            roll = int(dice.roll(roll_exp))
            rolls.append(str(roll))
        return f"Rolled {argstr}, got: {', '.join(rolls)}"
    except Exception as _:
        return "Sorry - dice expression appears malformed. Add more ()'s"


@as_command
def rollstats():
    return f"Rolled normal 4d6h3 stats, got: {', '.join(str(int(dice.roll('4d6h3'))) for _ in range(6))}"


@as_command
def rollstatsdumb():
    return f"Rolled dumb d20 stats, got: {', '.join(str(int(dice.roll('d20'))) for _ in range(6))}"


@as_command
def character(raw_args: Args):
    """
    The !character command generates a DnD character by firing 3 commands into the chat.
    """
    if "d20" in raw_args.lower():
        yield "!rollstatsdumb"
    else:
        yield "!rollstats"
    yield "!choice Barbarian Bard Cleric Druid Fighter Monk Paladin Ranger Rogue Sorcerer Warlock Wizard"
    yield "!npc"
