'========================================================
' File: MASTileBox.vb  (SSOT + Fixes + Fluent SDK API)
'========================================================
Option Strict On
Option Explicit On

Imports System
Imports System.Collections.Generic
Imports System.Windows.Forms
Imports Nexamas.UI.Controls
Imports Nexamas.UI.Composition
Imports Nexamas.UI.General
Imports Nexamas.UI.Layout
Imports Nexamas.UI.Rendering
Imports Nexamas.UI.Scroll
Imports Nexamas.UI.SkiaScroll
Imports Nexamas.UI.Theming
Imports Nexamas.UI.Architecture
Imports Nexamas.UI.Values
Imports Nexamas.UI.Virtualization
Imports SkiaSharp


Namespace Nexamas.UI.Components

    Partial Public NotInheritable Class MASTileBox

        Private Sub RestorePendingScrollOffset()
            If _scroll Is Nothing Then Return
            If Single.IsNaN(_pendingScrollOffset) OrElse Single.IsInfinity(_pendingScrollOffset) Then Return

            Dim offset As Single = Math.Max(0.0F, _pendingScrollOffset)
            _pendingScrollOffset = Single.NaN

            Try
                _scroll.EnsureVisible(offset, offset + 1.0F)
            Catch masCaughtException21 As Exception
                Nexamas.UI.Diagnostics.MASExceptionSilencer.SwallowRenderFallback(masCaughtException21)
            End Try
        End Sub

        Private Sub EnsureScroll(ctx As MASThemeContext)

            If ctx Is Nothing Then Return

            Dim theme As IMASAppTheme = ctx.Theme
            Dim themeId As String = Nothing
            Dim themeVersion As Integer = Integer.MinValue
            Dim themeStamp As Integer = ctx.ThemeStamp

            If theme IsNot Nothing Then
                themeId = If(theme.Id, String.Empty)
                themeVersion = theme.Version
            End If

            Dim mustRebuild As Boolean =
                (_scroll Is Nothing) OrElse
                (_scrollThemeRef Is Nothing AndAlso theme IsNot Nothing) OrElse
                (Not Object.ReferenceEquals(_scrollThemeRef, theme)) OrElse
                (Not String.Equals(_scrollThemeId, themeId, StringComparison.Ordinal)) OrElse
                (_scrollThemeVersion <> themeVersion) OrElse
                (_scrollThemeStamp <> themeStamp)

            If Not mustRebuild Then
                If _scroll IsNot Nothing Then
                    _scroll.UpdateThemeContext(ctx)
                End If

                Return
            End If

            If _scroll IsNot Nothing Then
                Try
                    _pendingScrollOffset = _scroll.Offset
                Catch masCaughtException20 As Exception
                    Nexamas.UI.Diagnostics.MASExceptionSilencer.SwallowRenderFallback(masCaughtException20)
                    _pendingScrollOffset = Single.NaN
                End Try

                Try
                    _scroll.Dispose()
                Catch masCaughtException4 As Exception
                    Nexamas.UI.Diagnostics.MASExceptionSilencer.SwallowRenderFallback(masCaughtException4)
                End Try

                _scroll = Nothing
            End If

            _scroll = ScrollFactory.Create(
                ctx,
                AddressOf SafeInvalidate,
                postToUiThread:=AddressOf PostTileBoxScrollRepeatToUiThread)

            _scrollThemeRef = theme
            _scrollThemeId = themeId
            _scrollThemeVersion = themeVersion
            _scrollThemeStamp = themeStamp

            MarkFrameCacheDirty()

        End Sub

    End Class

End Namespace
