package com.ponzischeme89.memby.ui

import org.junit.Assert.assertEquals
import org.junit.Assert.assertNull
import org.junit.Test

class RowNavigationTest {
    @Test
    fun `vertical movement skips rows without focusable cards`() {
        val counts = listOf(6, 0, 0, 4)

        assertEquals(
            3,
            adjacentFocusableRowIndex(counts, 0, RowFocusDirection.DOWN),
        )
        assertEquals(
            0,
            adjacentFocusableRowIndex(counts, 3, RowFocusDirection.UP),
        )
    }

    @Test
    fun `vertical movement stops at the page boundary`() {
        val counts = listOf(3, 2)

        assertNull(adjacentFocusableRowIndex(counts, 0, RowFocusDirection.UP))
        assertNull(adjacentFocusableRowIndex(counts, 1, RowFocusDirection.DOWN))
    }

    @Test
    fun `vertical movement can traverse beyond the second row`() {
        val counts = listOf(6, 6, 6, 6, 6)
        val visited = mutableListOf(0)
        var current = 0

        while (true) {
            current = adjacentFocusableRowIndex(
                counts,
                current,
                RowFocusDirection.DOWN,
            ) ?: break
            visited += current
        }

        assertEquals(listOf(0, 1, 2, 3, 4), visited)
    }

    @Test
    fun `first visit keeps horizontal position and clamps to a shorter row`() {
        assertEquals(4, rowEntryItemIndex(4, destinationItemCount = 8, null))
        assertEquals(2, rowEntryItemIndex(7, destinationItemCount = 3, null))
    }

    @Test
    fun `return visit restores the destination row position`() {
        assertEquals(
            1,
            rowEntryItemIndex(
                sourceIndex = 5,
                destinationItemCount = 8,
                rememberedDestinationIndex = 1,
            ),
        )
        assertEquals(
            2,
            rowEntryItemIndex(
                sourceIndex = 1,
                destinationItemCount = 3,
                rememberedDestinationIndex = 20,
            ),
        )
    }
}
