package app.voltplan.cable.ui.systems

import java.text.Normalizer
import java.util.Locale

/** Derives a system icon from its name by keyword. Mirrors `SystemsView.systemIconName(for:)`. */
object SystemIconMapper {
    private val mappings: List<Pair<List<String>, String>> = listOf(
        listOf("rv", "van", "camper", "motorhome", "coach") to "bus",
        listOf("truck", "trailer", "rig") to "truck.box",
        listOf("boat", "marine", "yacht", "sail") to "sailboat",
        listOf("plane", "air", "flight") to "airplane",
        listOf("ferry", "ship") to "ferry",
        listOf("house", "home", "cabin", "cottage", "lodge") to "house",
        listOf("building", "office", "warehouse", "factory", "facility") to "building",
        listOf("camp", "tent", "outdoor") to "tent",
        listOf("solar", "sun") to "sun.max",
        listOf("battery", "storage") to "battery.100",
        listOf("server", "data", "network", "rack") to "server.rack",
        listOf("computer", "electronics", "lab", "tech") to "cpu",
        listOf("gear", "mechanic", "machine", "workshop") to "gear",
        listOf("tool", "maintenance", "repair", "shop") to "wrench.adjustable",
        listOf("hammer", "carpentry") to "hammer",
        listOf("light", "lighting", "lamp") to "lightbulb",
        listOf("bolt", "power", "electric") to "bolt",
        listOf("plug") to "powerplug",
        listOf("engine", "generator", "motor") to "engine.combustion",
        listOf("fuel", "diesel", "gas") to "fuelpump",
        listOf("water", "pump", "tank") to "drop",
        listOf("heat", "heater", "furnace") to "flame",
        listOf("cold", "freeze", "cool") to "snowflake",
        listOf("climate", "hvac", "temperature") to "thermometer",
    )

    private const val DEFAULT = "building.2"

    fun iconFor(name: String): String {
        val normalized = Normalizer.normalize(name, Normalizer.Form.NFD)
            .replace(Regex("\\p{Mn}+"), "")
            .lowercase(Locale.getDefault())
        for ((keywords, icon) in mappings) {
            if (keywords.any { normalized.contains(it) }) return icon
        }
        return DEFAULT
    }

    val colorOptions = listOf(
        "blue", "green", "orange", "red", "purple", "yellow",
        "pink", "teal", "indigo", "mint", "cyan", "brown", "gray",
    )
}
