package de.arxsilex.maptools.converter.desktop

import de.arxsilex.maptools.converter.core.InputFileInfo
import de.arxsilex.maptools.converter.core.InputFormat
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFalse
import kotlin.test.assertTrue

class InputSourceSetTest {
    @Test
    fun `map and poi are separated into independent output sources`() {
        val map = InputFileInfo("region.map", "region.map", 10, InputFormat.MAPSFORGE_MAP)
        val poi = InputFileInfo("region.poi", "region.poi", 5, InputFormat.MAPSFORGE_POI)

        val sources = listOf(map, poi).toInputSourceSet()

        assertEquals(listOf(map), sources.mapSources)
        assertEquals(listOf(poi), sources.poiSources)
        assertEquals(listOf(map, poi), sources.selectionSources)
        assertEquals(listOf(map), sources.previewSources)
        assertTrue(sources.hasPoiCompanion)
        assertFalse(sources.isPoiOnly)
    }

    @Test
    fun `poi only uses its own bounds as selection source`() {
        val poi = InputFileInfo("region.poi", "region.poi", 5, InputFormat.MAPSFORGE_POI)

        val sources = listOf(poi).toInputSourceSet()

        assertTrue(sources.isPoiOnly)
        assertEquals(listOf(poi), sources.selectionSources)
        assertEquals(listOf(poi), sources.previewSources)
    }

    @Test
    fun `elevation sources remain independent from map and poi sources`() {
        val map = InputFileInfo("region.map", "region.map", 10, InputFormat.MAPSFORGE_MAP)
        val poi = InputFileInfo("region.poi", "region.poi", 5, InputFormat.MAPSFORGE_POI)
        val elevation = InputFileInfo("terrain.tif", "terrain.tif", 8, InputFormat.ELEVATION_GEOTIFF)

        val sources = listOf(map, poi, elevation).toInputSourceSet()

        assertEquals(listOf(map), sources.mapSources)
        assertEquals(listOf(poi), sources.poiSources)
        assertEquals(listOf(elevation), sources.elevationSources)
        assertEquals(listOf(map, poi, elevation), sources.selectionSources)
        assertEquals(listOf(map), sources.previewSources)
        assertTrue(sources.hasElevationData)
        assertFalse(sources.isElevationOnly)
    }

    @Test
    fun `elevation only uses raster bounds without pretending to be a map preview`() {
        val first = InputFileInfo("a.hgt", "a.hgt", 8, InputFormat.ELEVATION_HGT)
        val second = InputFileInfo("b.hgt", "b.hgt", 8, InputFormat.ELEVATION_HGT)

        val sources = listOf(first, second).toInputSourceSet()

        assertTrue(sources.isElevationOnly)
        assertEquals(listOf(first, second), sources.elevationSources)
        assertEquals(listOf(first, second), sources.selectionSources)
        assertTrue(sources.previewSources.isEmpty())
    }

    @Test
    fun `multiple poi sources remain available for merging and preview`() {
        val first = InputFileInfo("a.poi", "a.poi", 1, InputFormat.MAPSFORGE_POI)
        val second = InputFileInfo("b.poi", "b.poi", 1, InputFormat.MAPSFORGE_POI)

        val sources = listOf(first, second).toInputSourceSet()

        assertEquals(listOf(first, second), sources.poiSources)
        assertEquals(listOf(first, second), sources.selectionSources)
        assertEquals(listOf(first, second), sources.previewSources)
        assertTrue(sources.isPoiOnly)
    }
}
