package com.pixelized.shared.lwa.usecase

import kotlin.math.ceil
import kotlin.math.max

class CharacterSheetUseCase {

    fun normalize(value: Int): Int {
        return value - value % 5 // (truncate(value.toFloat() / 5f) * 5f).toInt()
    }

    fun movement(): Int = 10

    fun reflex(
        strength: Int,
        dexterity: Int,
        intelligence: Int,
    ): Int {
        return (strength + dexterity + intelligence) / 3
    }

    fun initiative(
        dexterity: Int,
    ): Int {
        return dexterity
    }

    fun maxHp(
        constitution: Int,
        height: Int,
        level: Int,
    ): Int {
        val add = max(hpGrow(constitution = constitution) * (level - 1), 0)
        return (ceil((constitution + height) / 2f).toInt()) + add
    }

    fun maxPp(
        power: Int,
    ): Int {
        return power
    }

    fun meleeBonusDamage(
        strength: Int,
        height: Int,
    ): String {
        return meleeBonusDamage(sum = strength + height)
    }

    fun meleeBonusDamage(
        sum: Int,
    ): String {
        return when {
            sum < 12 -> "-1d6"
            sum in 12..17 -> "-1d4"
            sum in 18..22 -> "+0"
            sum in 23..29 -> "+1d4"
            sum in 30..39 -> "+1d6"
            else -> "+2d6"
        }
    }

    fun distanceBonusDamage(
        strength: Int,
        height: Int,
    ): String {
        return distanceBonusDamage(sum = strength + height)
    }

    fun distanceBonusDamage(
        sum: Int,
    ): String {
        return when {
            sum < 12 -> "-1d3"
            sum in 12..17 -> "-1d2"
            sum in 18..22 -> "+0"
            sum in 23..29 -> "+1d2"
            sum in 30..39 -> "+1d3"
            else -> "+2d3"
        }
    }

    fun armor(): Int = 0

    fun learning(intelligence: Int): Int {
        return max(0, (intelligence - 10) * 2)
    }

    fun hpGrow(constitution: Int): Int {
        return (constitution / 3)
    }
}
