Option Strict On
Option Explicit On

Imports System

Namespace Nexamas.UI.TextInput

    Friend NotInheritable Class MASTextCaretEngine

        Private _visible As Boolean = True
        Private _lastTick As Integer

        Private Const BLINK_MS As Integer = 530

        Friend ReadOnly Property IsVisible As Boolean
            Get
                Return _visible
            End Get
        End Property

        Friend Sub Reset()
            _visible = True
            _lastTick = Nexamas.UI.Timing.MASInteractionClock.TickCount()
        End Sub

        Friend Sub Hide()
            _visible = False
        End Sub

        Friend Function Update(hasFocus As Boolean) As Boolean

            If Not hasFocus Then
                Dim changed As Boolean = _visible
                _visible = False
                Return changed
            End If

            Dim nowTick As Integer = Nexamas.UI.Timing.MASInteractionClock.TickCount()
            Dim dt As Integer = nowTick - _lastTick

            If dt < 0 Then
                _lastTick = nowTick
                _visible = True
                Return True
            End If

            If dt >= BLINK_MS Then
                _visible = Not _visible
                _lastTick = nowTick
                Return True
            End If

            Return False
        End Function

    End Class

End Namespace