package de.arxsilex.maptools.converter.desktop

import androidx.compose.ui.geometry.Offset
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertTrue

class LabelPlacementTest {
    @Test
    fun `first budget is distributed over the full visible height`() {
        val candidates = buildList {
            repeat(10) { row ->
                repeat(20) { column ->
                    add(
                        LabelPlacementCandidate(
                            labelIndex = row * 20 + column,
                            position = Offset(column * 90f + 20f, row * 90f + 20f),
                            importance = 50,
                        ),
                    )
                }
            }
        }

        val firstBudget = spatiallyBalancedLabelCandidates(candidates, 1_800f, 900f, 60).take(60)

        assertTrue(firstBudget.any { it.position.y < 150f })
        assertTrue(firstBudget.any { it.position.y in 350f..550f })
        assertTrue(firstBudget.any { it.position.y > 750f })
    }

    @Test
    fun `most important label in a cell is considered first`() {
        val low = LabelPlacementCandidate(1, Offset(100f, 100f), importance = 20)
        val high = LabelPlacementCandidate(2, Offset(110f, 105f), importance = 90)

        val ordered = spatiallyBalancedLabelCandidates(listOf(low, high), 400f, 300f, 4)

        assertEquals(high, ordered.first())
    }

    @Test
    fun `settled map never samples visible geometry`() {
        assertEquals(1, previewSamplingStep(visibleCount = 250_000, targetCount = 20_000, interactive = false))
        assertEquals(13, previewSamplingStep(visibleCount = 250_000, targetCount = 20_000, interactive = true))
    }
}
