package de.arxsilex.maptools.converter.desktop

import de.arxsilex.maptools.converter.core.ExportSelection
import de.arxsilex.maptools.converter.core.ElevationOutputFormat
import de.arxsilex.maptools.converter.core.MapFormatVersion
import de.arxsilex.maptools.converter.core.PoiSchemaVersion
import java.nio.file.Path
import kotlin.test.Test
import kotlin.test.assertEquals

class ExportTargetsTest {
    @Test
    fun `adds selected map and poi versions to the suggested name`() {
        val suggestion = ExportTargets.suggestedBaseName(
            sourceBaseName = "hessen",
            fullFile = true,
            selection = ExportSelection(
                mapsforgeMap = true,
                mapsforgePoi = true,
                mapFormatVersion = MapFormatVersion.V4,
                poiSchemaVersion = PoiSchemaVersion.V3,
            ),
        )

        assertEquals("hessen-konvertiert-mapv4-poiv3", suggestion)
    }

    @Test
    fun `keeps the old scope suffix for pbf only suggestions`() {
        val suggestion = ExportTargets.suggestedBaseName(
            sourceBaseName = "hessen",
            fullFile = false,
            selection = ExportSelection(mapsforgeMap = false, mapsforgePoi = false, filteredPbf = true),
        )

        assertEquals("hessen-ausschnitt", suggestion)
    }

    @Test
    fun `derives all selected formats from a known suffix`() {
        val targets = ExportTargets.fromSelectedBase(
            java.nio.file.Path.of("C:/maps/hessen.map"),
            ExportSelection(mapsforgeMap = true, mapsforgePoi = true, filteredPbf = true),
        )

        assertEquals("hessen.map", targets.mapsforgeMap?.fileName.toString())
        assertEquals("hessen.poi", targets.mapsforgePoi?.fileName.toString())
        assertEquals("hessen.osm.pbf", targets.filteredPbf?.fileName.toString())
    }

    @Test
    fun `keeps only selected output formats`() {
        val targets = ExportTargets.fromSelectedBase(
            java.nio.file.Path.of("hessen-ausschnitt"),
            ExportSelection(mapsforgeMap = false, mapsforgePoi = true, filteredPbf = false),
        )

        assertEquals(listOf("hessen-ausschnitt.poi"), targets.all.map { it.fileName.toString() })
    }

    @Test
    fun `detects an output that would overwrite a source`() {
        val source = Path.of("hessen.poi").toAbsolutePath().normalize()
        val targets = ExportTargets.fromSelectedBase(
            source,
            ExportSelection(mapsforgeMap = false, mapsforgePoi = true, filteredPbf = false),
        )

        assertEquals(listOf(source), targets.sourceConflicts(listOf(source)))
    }

    @Test
    fun `adds elevation format to suggestion and target extension`() {
        val selection = ExportSelection(
            mapsforgeMap = false,
            mapsforgePoi = false,
            elevationData = true,
            elevationOutputFormat = ElevationOutputFormat.ESRI_ASCII_GRID,
        )

        val suggestion = ExportTargets.suggestedBaseName("terrain", fullFile = false, selection)
        val targets = ExportTargets.fromSelectedBase(Path.of("$suggestion.tif"), selection)

        assertEquals("terrain-ausschnitt-dem-esri-ascii-grid", suggestion)
        assertEquals("terrain-ausschnitt-dem-esri-ascii-grid.asc", targets.elevationData?.fileName.toString())
        assertEquals(listOf(targets.elevationData), targets.all)
    }
}
