package de.arxsilex.maptools.converter.desktop

import de.arxsilex.maptools.converter.core.ElevationOutputFormat
import de.arxsilex.maptools.converter.core.GeoBounds
import de.arxsilex.maptools.converter.core.GeoRectangleRegion
import de.arxsilex.maptools.converter.core.elevation.ElevationMetadataReader
import de.arxsilex.maptools.converter.core.InputFormat
import java.nio.file.Files
import kotlin.io.path.createTempDirectory
import kotlin.io.path.writeText
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertTrue

class ElevationWorkerIntegrationTest {
    @Test
    fun `isolated worker converts and reports an elevation output`() {
        val directory = createTempDirectory("elevation-worker-")
        try {
            val input = directory.resolve("source.asc").also {
                it.writeText(
                    "ncols 2\nnrows 2\nxllcorner 7\nyllcorner 49\ncellsize 0.01\n" +
                        "NODATA_value -9999\n100 110\n120 130\n",
                )
            }
            val output = directory.resolve("result.asc")
            val result = WorkerLauncher.elevation(
                inputs = listOf(input),
                output = output,
                outputFormat = ElevationOutputFormat.ESRI_ASCII_GRID,
                areas = listOf(GeoRectangleRegion(GeoBounds(7.0, 49.0, 7.02, 49.02))),
                temporaryDirectory = directory.resolve("temp"),
                logDirectory = directory.resolve("logs"),
            )

            assertEquals(Files.size(output), result.outputs[output])
            assertTrue(Files.readString(result.logPath).contains("phase=elevation status=completed"))
            assertEquals(2, ElevationMetadataReader.read(output, InputFormat.ELEVATION_ASC).width)
        } finally {
            directory.toFile().deleteRecursively()
        }
    }
}
