Option Strict On
Option Explicit On

Imports System

Namespace Nexamas.UI.Scroll

    Friend NotInheritable Class ScrollModel

        Private _contentSize As Integer
        Private _viewportSize As Integer
        Private _position As Integer
        Private _maxPosition As Integer

        Friend Property ContentSize As Integer
            Get
                Return _contentSize
            End Get
            Private Set(value As Integer)
                _contentSize = Math.Max(0, value)
            End Set
        End Property

        Friend Property ViewportSize As Integer
            Get
                Return _viewportSize
            End Get
            Private Set(value As Integer)
                _viewportSize = Math.Max(0, value)
            End Set
        End Property

        Friend Property Position As Integer
            Get
                Return _position
            End Get
            Set(value As Integer)
                _position = Clamp(value, 0, _maxPosition)
            End Set
        End Property

        Friend ReadOnly Property MaxPosition As Integer
            Get
                Return _maxPosition
            End Get
        End Property

        Friend Sub Update(content As Integer, viewport As Integer)
            ContentSize = content
            ViewportSize = viewport

            _maxPosition = Math.Max(0, ContentSize - ViewportSize)
            Position = _position
        End Sub

        Friend Sub ScrollBy(delta As Integer)
            Position += delta
        End Sub

        Private Shared Function Clamp(value As Integer, minValue As Integer, maxValue As Integer) As Integer
            If value < minValue Then Return minValue
            If value > maxValue Then Return maxValue
            Return value
        End Function

    End Class

End Namespace