Option Strict On
Option Explicit On

Imports System
Imports System.Collections.Generic
Imports Nexamas.UI.Controls
Imports Nexamas.UI.Composition
Imports Nexamas.UI.General
Imports Nexamas.UI.Icons
Imports Nexamas.UI.Layout
Imports Nexamas.UI.Rendering
Imports Nexamas.UI.Theming
Imports Nexamas.UI.Values
Imports SkiaSharp
Imports Nexamas.UI.Architecture

Namespace Nexamas.UI.Components


    Partial Public Class MASToolbar
#Region "Properties"

        Friend Sub SetBounds(bounds As SKRect)
            SetBounds(bounds.Left, bounds.Top, bounds.Width, bounds.Height)
        End Sub

        Friend Sub SetBounds(x As Single,
                             y As Single,
                             width As Single,
                             height As Single)

             SetLocalLayoutBoundsInternal(New SKRect(
                x,
                y,
                x + Math.Max(0.0F, width),
                y + Math.Max(0.0F, height)
            ))
        End Sub

        Friend Function WithBounds(bounds As SKRect) As MASToolbar
            SetBounds(bounds)
            Return Me
        End Function

        Friend Function WithBounds(x As Single,
                                   y As Single,
                                   width As Single,
                                   height As Single) As MASToolbar

            SetBounds(x, y, width, height)
            Return Me
        End Function

                <Global.System.ComponentModel.EditorBrowsable(Global.System.ComponentModel.EditorBrowsableState.Advanced)>
        Friend Function WithHorizontalPadding(value As Single) As MASToolbar
            HorizontalPaddingDip = value
            Return Me
        End Function

                <Global.System.ComponentModel.EditorBrowsable(Global.System.ComponentModel.EditorBrowsableState.Advanced)>
        Friend Function WithVerticalInset(value As Single) As MASToolbar
            VerticalInsetDip = value
            Return Me
        End Function

                <Global.System.ComponentModel.EditorBrowsable(Global.System.ComponentModel.EditorBrowsableState.Advanced)>
        Friend Function WithGap(value As Single) As MASToolbar
            GapDip = value
            Return Me
        End Function

                <Global.System.ComponentModel.EditorBrowsable(Global.System.ComponentModel.EditorBrowsableState.Advanced)>
        Friend Function WithItemHeight(value As Single) As MASToolbar
            ItemHeightDip = value
            Return Me
        End Function


                <Global.System.ComponentModel.EditorBrowsable(Global.System.ComponentModel.EditorBrowsableState.Advanced)>
        Friend Property HorizontalPaddingDip As Single
            Get
                Return _horizontalPaddingDip
            End Get
            Set(value As Single)
                value = Math.Max(0.0F, value)
                If _horizontalPaddingDip = value Then Return
                _horizontalPaddingDip = value
                RequestSizeLayoutRefreshForSizeAffectingChange("MASToolbar.HorizontalPaddingDip")
                InvalidateVisual()
            End Set
        End Property

                <Global.System.ComponentModel.EditorBrowsable(Global.System.ComponentModel.EditorBrowsableState.Advanced)>
        Friend Property VerticalInsetDip As Single
            Get
                Return _verticalInsetDip
            End Get
            Set(value As Single)
                value = Math.Max(0.0F, value)
                If _verticalInsetDip = value Then Return
                _verticalInsetDip = value
                RequestSizeLayoutRefreshForSizeAffectingChange("MASToolbar.VerticalInsetDip")
                InvalidateVisual()
            End Set
        End Property

                <Global.System.ComponentModel.EditorBrowsable(Global.System.ComponentModel.EditorBrowsableState.Advanced)>
        Friend Property GapDip As Single
            Get
                Return _gapDip
            End Get
            Set(value As Single)
                value = Math.Max(0.0F, value)
                If _gapDip = value Then Return
                _gapDip = value
                RequestSizeLayoutRefreshForSizeAffectingChange("MASToolbar.GapDip")
                InvalidateVisual()
            End Set
        End Property

                <Global.System.ComponentModel.EditorBrowsable(Global.System.ComponentModel.EditorBrowsableState.Advanced)>
        Friend Property ItemHeightDip As Single
            Get
                Return _itemHeightDip
            End Get
            Set(value As Single)
                value = MASIntrinsicControlSizeMetrics.ResolveToolbarItemHeight(value)
                If _itemHeightDip = value Then Return
                _itemHeightDip = value
                RequestSizeLayoutRefreshForSizeAffectingChange("MASToolbar.ItemHeightDip")
                InvalidateVisual()
            End Set
        End Property

#Region "Size/Layout Contract"

        Friend Function GetPreferredLayoutSize(context As MASLayoutMeasureContext) As SKSize Implements IMASLayoutParticipant.GetPreferredLayoutSize
            Return MeasureIntrinsicSize(CreateSizeContext(context), SizeIntent).DesiredSize
        End Function

        Friend Function GetMinLayoutSize(context As MASLayoutMeasureContext) As SKSize Implements IMASLayoutParticipant.GetMinLayoutSize
            Return MeasureIntrinsicSize(CreateSizeContext(context), SizeIntent).MinSize
        End Function

        Friend Function MeasureIntrinsicSize(context As MASSizeContext, intent As MASSize) As MASSizeResult Implements IMASIntrinsicSizeContract.MeasureIntrinsicSize
            Dim safeContext As MASSizeContext = MASIntrinsicControlSizeMetrics.EnsureContext(context)
            Dim visibleItems As Integer = 0
            Dim fixedWidth As Single = 0.0F
            Dim fillMinWidth As Single = 0.0F
            Dim hasFill As Boolean = False

            For Each item As ToolbarEntry In _items
                If Not IsEntryVisible(item) Then Continue For
                visibleItems += 1
                If item.Sizing = MASToolbarItemSizing.Fill Then
                    hasFill = True
                    fillMinWidth += Math.Max(0.0F, item.MinWidthDip)
                Else
                    fixedWidth += Math.Max(0.0F, item.WidthDip)
                End If
            Next

            Dim desired As SKSize = MASIntrinsicControlSizeMetrics.ResolveToolbarDesiredSize(
                visibleItemCount:=visibleItems,
                fixedWidthDip:=fixedWidth,
                fillMinWidthDip:=fillMinWidth,
                horizontalPaddingDip:=_horizontalPaddingDip,
                verticalInsetDip:=_verticalInsetDip,
                gapDip:=_gapDip,
                itemHeightDip:=_itemHeightDip,
                defaultButtonWidthDip:=_defaultButtonWidthDip)
            Dim minimum As SKSize = MASIntrinsicControlSizeMetrics.ResolveToolbarMinimumSize(
                fillMinWidthDip:=fillMinWidth,
                horizontalPaddingDip:=_horizontalPaddingDip,
                verticalInsetDip:=_verticalInsetDip,
                itemHeightDip:=_itemHeightDip)

            Return MASSizeResolver.FromMeasured(
                desiredSize:=desired,
                minSize:=minimum,
                maxSize:=New SKSize(Single.PositiveInfinity, Single.PositiveInfinity),
                intent:=intent,
                context:=safeContext,
                canGrowWidth:=hasFill,
                canGrowHeight:=False,
                contentInset:=MASIntrinsicControlSizeMetrics.CreateToolbarContentInset(_horizontalPaddingDip, _verticalInsetDip),
                visualOverflowInset:=MASLayoutInset.Empty,
                hitOverflowInset:=MASLayoutInset.Empty,
                isFallback:=False)
        End Function

        Private Shared Function CreateSizeContext(context As MASLayoutMeasureContext) As MASSizeContext
            If context Is Nothing Then Return MASIntrinsicControlSizeMetrics.EnsureContext(Nothing)
            Return context.ToSizeContext()
        End Function

#End Region
#End Region

    End Class

End Namespace
