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 GridSelectionManagerTest : SelectionManagerTest<GridFocusable, GridSelectionManager>() {

    lateinit var focusables: MutableList<GridFocusable>

    override fun setUp() {
        manager = GridSelectionManager()

        focusables = mutableListOf()
        repeat(3) {
            manager.addRow()
            repeat(3) {
                focusables.add(
                    manager.add()
                )
            }
        }
    }

    @Test
    fun ensure_navigation_right_cycles() {
        manager.onKeyEvent(keyEvent(Key.DirectionRight))
        expect(manager.focused).toEqual(focusables[1])

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

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

    @Test
    fun ensure_navigation_left_cycles() {
        manager.onKeyEvent(keyEvent(Key.DirectionLeft))
        expect(manager.focused).toEqual(focusables[2])

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

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

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

    @Test
    fun ensure_navigation_down_cycles() {
        manager.onKeyEvent(keyEvent(Key.DirectionDown))
        expect(manager.focused).toEqual(focusables[3])

        manager.onKeyEvent(keyEvent(Key.DirectionDown))
        expect(manager.focused).toEqual(focusables[6])

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

    @Test
    fun ensure_navigation_up_cycles() {
        manager.onKeyEvent(keyEvent(Key.DirectionUp))
        expect(manager.focused).toEqual(focusables[6])

        manager.onKeyEvent(keyEvent(Key.DirectionUp))
        expect(manager.focused).toEqual(focusables[3])

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

        manager.onKeyEvent(keyEvent(Key.DirectionUp))
        expect(manager.focused).toEqual(focusables[6])
    }

}