package space.nstart.pcp.complan.apl

import org.junit.jupiter.api.io.TempDir
import org.locationtech.jts.geom.Coordinate
import org.locationtech.jts.geom.GeometryFactory
import space.nstart.pcp.complan.types.GeoJsonItem
import java.nio.file.Files
import java.nio.file.Path
import kotlin.test.Test
import kotlin.test.assertFalse
import kotlin.test.assertTrue

class GeoJsonLayerWriterTest {

    @Test
    fun `does not create layer file when layer writing is disabled`(@TempDir tempDir: Path) {
        val layerDir = tempDir.resolve("layers")
        val writer = GeoJsonLayerWriter(enabled = false, outputDir = layerDir.toString())

        writer.write(listOf(testGeoJsonItem()), "Requests")

        assertFalse(Files.exists(layerDir))
    }

    @Test
    fun `creates layer file in configured directory when layer writing is enabled`(@TempDir tempDir: Path) {
        val layerDir = tempDir.resolve("layers")
        val writer = GeoJsonLayerWriter(enabled = true, outputDir = layerDir.toString())

        writer.write(listOf(testGeoJsonItem()), "Requests")

        assertTrue(Files.exists(layerDir.resolve("Requests.json")))
    }

    private fun testGeoJsonItem(): GeoJsonItem =
        GeoJsonItem(
            geometry = GeometryFactory().createPoint(Coordinate(37.0, 55.0)),
            properties = emptyMap<String, Any>()
        )
}
