package de.arxsilex.maptools.converter.worker

import de.arxsilex.maptools.converter.core.ContentSelection
import java.nio.file.Files
import javax.xml.parsers.DocumentBuilderFactory
import kotlin.io.path.createTempFile
import kotlin.test.Test
import kotlin.test.assertFalse
import kotlin.test.assertTrue

class MapsforgeTagMappingTest {
    @Test
    fun `keeps selected roads and removes paths buildings landscape and pois`() {
        val output = createTempFile(suffix = ".xml")
        try {
            MapsforgeTagMapping.write(
                ContentSelection(
                    roads = true,
                    pathsAndTrails = false,
                    buildings = false,
                    landAndWater = false,
                    pointsOfInterest = false,
                    sourceMetadata = false,
                ),
                output,
            )
            val document = DocumentBuilderFactory.newInstance().apply { isNamespaceAware = true }
                .newDocumentBuilder().parse(output.toFile())
            val tags = document.getElementsByTagNameNS("http://mapsforge.org/tag-mapping", "osm-tag")
            val pairs = (0 until tags.length).map { index ->
                val element = tags.item(index) as org.w3c.dom.Element
                element.getAttribute("key") to element.getAttribute("value")
            }.toSet()

            assertTrue("highway" to "primary" in pairs)
            assertFalse("highway" to "path" in pairs)
            assertFalse(pairs.any { it.first == "building" })
            assertFalse(pairs.any { it.first == "landuse" })
            assertFalse(pairs.any { it.first == "amenity" })
        } finally {
            Files.deleteIfExists(output)
        }
    }
}
