package com.rosetta.messenger.ui.chats.components

import org.junit.Assert.*
import org.junit.Before
import org.junit.Test

class TextSelectionHelperTest {

    private lateinit var helper: TextSelectionHelper

    @Before
    fun setup() {
        helper = TextSelectionHelper()
    }

    @Test
    fun `initial state is not active`() {
        assertFalse(helper.isActive)
        assertFalse(helper.isInSelectionMode)
        assertEquals(-1, helper.selectionStart)
        assertEquals(-1, helper.selectionEnd)
        assertNull(helper.selectedMessageId)
    }

    @Test
    fun `clear resets all state`() {
        helper.clear()
        assertFalse(helper.isActive)
        assertEquals(-1, helper.selectionStart)
        assertEquals(-1, helper.selectionEnd)
        assertNull(helper.selectedMessageId)
        assertNull(helper.layoutInfo)
        assertFalse(helper.showToolbar)
        assertFalse(helper.movingHandle)
    }

    @Test
    fun `getSelectedText returns null when not active`() {
        assertNull(helper.getSelectedText())
    }

    @Test
    fun `updateSelectionEnd does not change when not active`() {
        helper.updateSelectionEnd(5)
        assertEquals(-1, helper.selectionEnd)
    }

    @Test
    fun `updateSelectionStart does not change when not active`() {
        helper.updateSelectionStart(0)
        assertEquals(-1, helper.selectionStart)
    }

    @Test
    fun `getCharOffsetFromCoords returns -1 when no layout`() {
        assertEquals(-1, helper.getCharOffsetFromCoords(100, 100))
    }

    @Test
    fun `selectAll does nothing when no layout`() {
        helper.selectAll()
        assertEquals(-1, helper.selectionStart)
        assertEquals(-1, helper.selectionEnd)
    }

    @Test
    fun `moveHandle does nothing when not moving`() {
        helper.moveHandle(100f, 100f)
        assertFalse(helper.movingHandle)
    }

    @Test
    fun `endHandleDrag sets movingHandle to false`() {
        helper.endHandleDrag()
        assertFalse(helper.movingHandle)
    }
}
