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 "Child layout placement"

        Private Sub LayoutChildren(boundsPx As SKRect, dpi As Single, Optional layoutScale As Single = 1.0F)
            Dim safeDpi As Single = PixelSnap.SafeDpi(dpi)

            _layout = MASFilePickerLayoutBuilder.Build(
                boundsPx:=boundsPx,
                dpi:=safeDpi,
                isSaveMode:=IsSaveMode,
                layoutScale:=layoutScale
            )

            SetControlLogicalBounds(_navigationBar, _layout.NavigationBarRect, safeDpi)

            If _browser IsNot Nothing Then
                SetControlLogicalBounds(_browser, _layout.BrowserRect, safeDpi)
            End If

            LayoutFooterButtons(_layout.ButtonsBarRect, safeDpi, layoutScale)

            SetControlLogicalBounds(_footerLabel, _layout.FooterLabelRect, safeDpi)

            If _fileNameBox IsNot Nothing Then
                _fileNameBox.Visible = IsSaveMode
                SetControlLogicalBounds(_fileNameBox, _layout.FileNameBoxRect, safeDpi)
            End If
        End Sub


        Private Sub LayoutFooterButtons(buttonsAreaPx As SKRect, dpi As Single, Optional layoutScale As Single = 1.0F)

            Dim safeDpi As Single = PixelSnap.SafeDpi(dpi)
            Dim metricDpi As Single = safeDpi * Math.Max(0.62F, Math.Min(1.22F, layoutScale))

            Dim outerInsetX As Single = FilePickerTokens.FooterButtonOuterInsetX * metricDpi
            Dim gapPx As Single = FilePickerTokens.FooterButtonGap * metricDpi
            Dim hPx As Single = FilePickerTokens.FooterButtonHeight * metricDpi
            Dim buttonW As Single = FilePickerTokens.FooterButtonWidth * metricDpi

            Dim usableLeft As Single = buttonsAreaPx.Left + outerInsetX
            Dim usableRight As Single = buttonsAreaPx.Right - outerInsetX
            Dim usableWidth As Single = Math.Max(1.0F, usableRight - usableLeft)

            If hPx > buttonsAreaPx.Height Then
                hPx = buttonsAreaPx.Height
            End If

            Dim totalW As Single = (buttonW * 2.0F) + gapPx

            If totalW > usableWidth Then
                buttonW = (usableWidth - gapPx) / 2.0F
                If buttonW < 1.0F Then buttonW = 1.0F
            End If

            Dim xStart As Single = usableLeft
            Dim y As Single = buttonsAreaPx.MidY - (hPx / 2.0F)

            Dim primaryRectPx As New SKRect(
        xStart,
        y,
        xStart + buttonW,
        y + hPx
    )

            Dim cancelRectPx As New SKRect(
        primaryRectPx.Right + gapPx,
        y,
        primaryRectPx.Right + gapPx + buttonW,
        y + hPx
    )

            SetControlLogicalBounds(_primaryButton, primaryRectPx, safeDpi)
            SetControlLogicalBounds(_cancelButton, cancelRectPx, safeDpi)

        End Sub


        Private Sub SetControlLogicalBounds(ctrl As MASControlBase, rectPx As SKRect, dpi As Single)
            ApplyChildLogicalBounds(ctrl, rectPx, dpi)
        End Sub


        Private Sub ApplyChildLogicalBounds(ctrl As MASControlBase, rectPx As SKRect, dpi As Single)
            If ctrl Is Nothing Then Return

            Dim safeDpi As Single = PixelSnap.SafeDpi(dpi)

            If rectPx.IsEmpty OrElse rectPx.Width <= 0.0F OrElse rectPx.Height <= 0.0F Then
                ctrl.SetLocalLayoutBoundsInternal(SKRect.Empty)
                Return
            End If

            ctrl.SetLocalLayoutBoundsInternal(New SKRect(
                rectPx.Left / safeDpi,
                rectPx.Top / safeDpi,
                rectPx.Right / safeDpi,
                rectPx.Bottom / safeDpi
            ))
        End Sub



#End Region

    End Class

End Namespace
