package ch.dissem.yaep.ui.common.focus

import androidx.compose.ui.input.key.Key
import ch.tutteli.atrium.api.fluent.en_GB.toEqual
import ch.tutteli.atrium.api.verbs.expect
import kotlin.test.Test

class LinearSelectionManagerTest : SelectionManagerTest<LinearFocusable, LinearSelectionManager>() {

    override fun setUp() {
        manager = LinearSelectionManager(
            keyNext = Key.A,
            keyPrevious = Key.B
        )
    }

    @Test
    fun ensure_manager_with_focusables_has_focus_on_first_item() {
        val focusables = arrayOf(
            manager.add(),
            manager.add()
        )

        expect(manager.focused).toEqual(focusables[0])
    }

    @Test
    fun ensure_keyEvent_A_selects_next() {
        val focusables = arrayOf(
            manager.add(),
            manager.add(),
            manager.add()
        )

        manager.onKeyEvent(keyEvent(Key.A))
        expect(manager.focused).toEqual(focusables[1])

        manager.onKeyEvent(keyEvent(Key.A))
        expect(manager.focused).toEqual(focusables[2])
    }

    @Test
    fun ensure_keyEvent_B_selects_previous() {
        val focusables = arrayOf(
            manager.add(),
            manager.add(),
            manager.add()
        )
        manager.focused = focusables[2]

        manager.onKeyEvent(keyEvent(Key.B))
        expect(manager.focused).toEqual(focusables[1])

        manager.onKeyEvent(keyEvent(Key.B))
        expect(manager.focused).toEqual(focusables[0])
    }

    @Test
    fun ensure_ring_behaviour() {
        val focusables = arrayOf(
            manager.add(),
            manager.add(),
            manager.add()
        )

        manager.onKeyEvent(keyEvent(Key.B))
        expect(manager.focused).toEqual(focusables[2])

        manager.onKeyEvent(keyEvent(Key.A))
        expect(manager.focused).toEqual(focusables[0])
    }

}