package de.arxsilex.maptools.converter.desktop

import kotlin.math.abs
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertTrue

class MeasurementFormatterTest {
    @Test
    fun `imperial radius input round trips without changing internal kilometers`() {
        val formatter = MeasurementFormatter(MeasurementSystem.IMPERIAL, AppLanguage.ENGLISH)
        val displayed = formatter.formatEditableRadius(10.0)
        val parsed = formatter.parseRadius(
            displayed,
            minimumKilometers = 0.01,
            maximumKilometers = 2_000.0,
            strings = LocalizationCatalog.strings(AppLanguage.ENGLISH),
        ).getOrThrow()
        assertTrue(abs(parsed - 10.0) < 0.002)
    }

    @Test
    fun `metric and imperial distances use their respective units`() {
        assertEquals("1.0 km", MeasurementFormatter(MeasurementSystem.METRIC, AppLanguage.ENGLISH).formatDistance(1.0))
        assertEquals("0.6 mi", MeasurementFormatter(MeasurementSystem.IMPERIAL, AppLanguage.ENGLISH).formatDistance(1.0))
    }

    @Test
    fun `elevation legend follows the selected measurement system`() {
        assertEquals("1,234 m", MeasurementFormatter(MeasurementSystem.METRIC, AppLanguage.ENGLISH).formatElevation(1_234.0))
        assertEquals("4,049 ft", MeasurementFormatter(MeasurementSystem.IMPERIAL, AppLanguage.ENGLISH).formatElevation(1_234.0))
    }

    @Test
    fun `editable radius follows locale and parses decimal comma strictly`() {
        val formatter = MeasurementFormatter(MeasurementSystem.METRIC, AppLanguage.GERMAN)
        val strings = LocalizationCatalog.strings(AppLanguage.GERMAN)
        assertEquals("1,5", formatter.formatEditableRadius(1.5))
        assertEquals(1.5, formatter.parseRadius("1,5", 0.01, 2_000.0, strings).getOrThrow())
        assertEquals(1.5, formatter.parseRadius("1.5", 0.01, 2_000.0, strings).getOrThrow())
        assertTrue(formatter.parseRadius("1,5 km", 0.01, 2_000.0, strings).isFailure)
    }
}
