package com.ponzischeme89.memby.ui

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

class UserSwitcherNavigationTest {
    @Test
    fun `removed profile hands focus to its stable neighbour`() {
        assertEquals(1, profileFocusIndexAfterRemoval(removedIndex = 1, remainingCount = 3))
        assertEquals(2, profileFocusIndexAfterRemoval(removedIndex = 3, remainingCount = 3))
        assertNull(profileFocusIndexAfterRemoval(removedIndex = 0, remainingCount = 0))
    }

    @Test
    fun `active profile receives initial focus`() {
        assertEquals(
            1,
            userSwitcherInitialIndex(listOf("one", "two", "three"), "two"),
        )
    }

    @Test
    fun `three profiles lead to pinned manage action`() {
        val profileCount = 3

        assertEquals(
            3,
            userSwitcherNextIndex(2, profileCount, UserSwitcherDirection.DOWN),
        )
        assertEquals(
            3,
            userSwitcherNextIndex(3, profileCount, UserSwitcherDirection.DOWN),
        )
    }

    @Test
    fun `up and down navigation stay inside the switcher`() {
        assertEquals(0, userSwitcherNextIndex(0, 3, UserSwitcherDirection.UP))
        assertEquals(2, userSwitcherNextIndex(3, 3, UserSwitcherDirection.UP))
    }

    @Test
    fun `manage remains reachable when no profiles exist`() {
        assertEquals(0, userSwitcherInitialIndex(emptyList(), null))
        assertEquals(0, userSwitcherNextIndex(0, 0, UserSwitcherDirection.DOWN))
    }

    @Test
    fun `both pinned actions are reachable below the profiles`() {
        val profileCount = 3

        // …the last profile, then My Alerts, then Manage users, and no further.
        assertEquals(3, userSwitcherNextIndex(2, profileCount, UserSwitcherDirection.DOWN, 2))
        assertEquals(4, userSwitcherNextIndex(3, profileCount, UserSwitcherDirection.DOWN, 2))
        assertEquals(4, userSwitcherNextIndex(4, profileCount, UserSwitcherDirection.DOWN, 2))
        assertEquals(3, userSwitcherNextIndex(4, profileCount, UserSwitcherDirection.UP, 2))
    }

    @Test
    fun `pinned actions stay reachable with a single profile`() {
        assertEquals(1, userSwitcherNextIndex(0, 1, UserSwitcherDirection.DOWN, 2))
        assertEquals(2, userSwitcherNextIndex(1, 1, UserSwitcherDirection.DOWN, 2))
        assertEquals(2, userSwitcherNextIndex(9, 1, UserSwitcherDirection.DOWN, 2))
    }
}
