package app.voltplan.cable.ui.loads

import app.voltplan.cable.calc.ElectricalCalculations
import app.voltplan.cable.data.UnitSystem
import app.voltplan.cable.data.model.SavedLoad
import app.voltplan.cable.util.Fmt
import java.util.Locale

/** Display helpers for loads, honouring the active unit system. Mirrors `LoadsView` formatting. */
object LoadFormatting {

    fun displayLengthMeters(meters: Double, unit: UnitSystem): Double =
        if (unit == UnitSystem.IMPERIAL) meters * ElectricalCalculations.FEET_PER_METER else meters

    fun wireGaugeString(load: SavedLoad, unit: UnitSystem): String {
        val cs = ElectricalCalculations.recommendedCrossSection(load.length, load.current, load.voltage, unit)
        return if (unit == UnitSystem.METRIC) {
            String.format(Locale.US, "%.1f mm²", cs)
        } else {
            "${ElectricalCalculations.formatAWG(cs)} AWG"
        }
    }

    fun lengthString(load: SavedLoad, unit: UnitSystem): String {
        val len = displayLengthMeters(load.length, unit)
        return if (unit == UnitSystem.METRIC) String.format(Locale.US, "%.1f m", len)
        else String.format(Locale.US, "%.1f ft", len)
    }

    fun fuseString(load: SavedLoad): String =
        Fmt.fuse(ElectricalCalculations.recommendedFuse(load.current)) + " A"

    /** "{V}V • {power|current} • {length}" summary line shown on load rows. */
    fun summaryBadge(load: SavedLoad, unit: UnitSystem): String {
        val v = String.format(Locale.US, "%.1fV", load.voltage)
        val mid = if (load.isWattMode) String.format(Locale.US, "%.0fW", load.power)
        else String.format(Locale.US, "%.1fA", load.current)
        return "$v • $mid • ${lengthString(load, unit)}"
    }
}
