Option Strict On
Option Explicit On

Imports System
Imports System.Collections.Specialized
Imports System.Diagnostics
Imports System.IO
Imports System.Windows.Forms
Imports Nexamas.UI.Controls
Imports Nexamas.UI.Composition
Imports Nexamas.UI.FileSystem
Imports Nexamas.UI.General
Imports Nexamas.UI.Rendering
Imports Nexamas.UI.Theming
Imports Nexamas.UI.Values
Imports Nexamas.UI.Layout
Imports SkiaSharp

Namespace Nexamas.UI.Components


    Partial Public NotInheritable Class MASFilePickerControl
#Region "Rendering and host input hooks"

        Protected Overrides Sub OnAttached()
            SafeInvalidate()
        End Sub


        Protected Overrides Sub OnDetached()
        End Sub

        Protected Overrides Sub Render(canvas As SKCanvas, ctx As MASThemeContext, pixelBounds As SKRect)
            If _disposedLocal Then Return
            If canvas Is Nothing OrElse ctx Is Nothing Then Return
            If pixelBounds.Width <= 1.0F OrElse pixelBounds.Height <= 1.0F Then Return

            _lastDpi = PixelSnap.SafeDpi(ctx.Dpi)

            Dim shownTitle As String =
        If(_controller IsNot Nothing, _controller.DialogTitle, "File Picker")

            Dim shellOptions As MASPanelShellDialogOptions =
                MASPanelShellDialogOptions.FilePicker(shownTitle)

            Dim shellResult As MASPanelShellResult =
                MASPanelShellDialogHost.Draw(
                    canvas:=canvas,
                    ctx:=ctx,
                    dpi:=_lastDpi,
                    boundsPx:=pixelBounds,
                    options:=shellOptions,
                    closeHover:=_panelShellCloseHover,
                    closePressed:=_panelShellClosePressed)

            _lastShellResult = shellResult

            If shellResult Is Nothing OrElse shellResult.ContentRectPx.IsEmpty Then
                _lastShellResult = Nothing
                _layout = New FilePickerLayoutInfo()
                Return
            End If


            Dim responsivePlan As MASFilePickerResponsiveLayoutPlan = MASFilePickerResponsiveLayoutPlan.Create(ctx, shellResult.ContentRectPx, SizeIntent)
            LayoutChildren(shellResult.ContentRectPx, _lastDpi, responsivePlan.LayoutScale)

            If Not _layout.ContentRect.IsEmpty Then
                _contentRenderer.DrawFooterRegion(canvas, ctx, _layout, _lastDpi)
                _contentRenderer.DrawSaveFileNameLabel(canvas, ctx, _layout, IsSaveMode, _lastDpi)
            End If
        End Sub


        Protected Overrides Function HitTest(ctx As MASThemeContext, ptPx As SKPoint) As Boolean
            Return GetPixelHitBounds(ctx).Contains(ptPx.X, ptPx.Y)
        End Function


        Protected Overrides Function WantsKeyboardFocus() As Boolean
            Return False
        End Function



#End Region

    End Class

End Namespace
