package com.pixelized.shared.lwa.parser.dice

class DiceParser {
    private val diceParser = Regex(
        """^(?<modifier>[ade])?(?<quantity>\d+)[dD](?<faces>\d+)"""
    )

    fun parse(expression: String): Dice? {
        return diceParser.find(expression)?.let {
            val (modifier, quantity, faces) = it.destructured
            Dice(
                modifier = parseModifier(value = modifier),
                quantity = quantity.toInt(),
                faces = faces.toInt(),
            )
        }
    }

    private fun parseModifier(value: String): Dice.Modifier? {
        return when (value) {
            "a" -> Dice.Modifier.ADVANTAGE
            "d" -> Dice.Modifier.DISADVANTAGE
            "e" -> Dice.Modifier.EMPHASIS
            else -> null
        }
    }
}
