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 "Helpers"

        Private Function FindEntry(id As String) As ToolbarEntry
            Dim key As String = If(id, String.Empty)

            For Each item As ToolbarEntry In _items
                If item Is Nothing Then Continue For

                If String.Equals(item.Id, key, StringComparison.Ordinal) Then
                    Return item
                End If
            Next

            Return Nothing
        End Function

        Private Shared Function MapToolKindToIconKind(toolKind As MASToolbarToolKind) As MASIconKind
            Select Case toolKind
                Case MASToolbarToolKind.Back
                    Return MASIconKind.Back
                Case MASToolbarToolKind.Forward
                    Return MASIconKind.Forward
                Case MASToolbarToolKind.Up
                    Return MASIconKind.Up
                Case MASToolbarToolKind.Home
                    Return MASIconKind.Home
                Case MASToolbarToolKind.Refresh
                    Return MASIconKind.Refresh
                Case MASToolbarToolKind.StopAction
                    Return MASIconKind.Stopaction
                Case MASToolbarToolKind.Search
                    Return MASIconKind.Search
                Case MASToolbarToolKind.Filter
                    Return MASIconKind.Filter
                Case MASToolbarToolKind.Sort
                    Return MASIconKind.Sort
                Case MASToolbarToolKind.More
                    Return MASIconKind.More
                Case MASToolbarToolKind.Settings
                    Return MASIconKind.Settings
                Case MASToolbarToolKind.NewFolder
                    Return MASIconKind.NewFolder
                Case MASToolbarToolKind.OpenFolder
                    Return MASIconKind.OpenFolder
                Case MASToolbarToolKind.Save
                    Return MASIconKind.Save
                Case MASToolbarToolKind.SaveAs
                    Return MASIconKind.SaveAs
                Case MASToolbarToolKind.Upload
                    Return MASIconKind.Upload
                Case MASToolbarToolKind.Export
                    Return MASIconKind.Export
                Case MASToolbarToolKind.Edit
                    Return MASIconKind.Edit
                Case MASToolbarToolKind.Rename
                    Return MASIconKind.Rename
                Case MASToolbarToolKind.Delete
                    Return MASIconKind.Delete
                Case MASToolbarToolKind.Copy
                    Return MASIconKind.Copy
                Case MASToolbarToolKind.Cut
                    Return MASIconKind.Cut
                Case MASToolbarToolKind.Paste
                    Return MASIconKind.Paste
                Case MASToolbarToolKind.Check
                    Return MASIconKind.Check
                Case MASToolbarToolKind.Clear
                    Return MASIconKind.Clear
                Case MASToolbarToolKind.GridView
                    Return MASIconKind.GridView
                Case MASToolbarToolKind.ListView
                    Return MASIconKind.ListView
                Case MASToolbarToolKind.DetailsView
                    Return MASIconKind.DetailsView
                Case Else
                    Return MASIconKind.None
            End Select
        End Function

        Private Sub AddControlInternal(id As String,
                                       control As MASControlBase,
                                       widthDip As Single,
                                       Optional kind As MASToolbarItemKind = MASToolbarItemKind.Control,
                                       Optional sizing As MASToolbarItemSizing = MASToolbarItemSizing.Fixed,
                                       Optional alignment As MASToolbarItemAlignment = MASToolbarItemAlignment.Left,
                                       Optional minWidthDip As Single = 0.0F,
                                       Optional maxWidthDip As Single = 0.0F)

            If control Is Nothing Then Throw New ArgumentNullException(NameOf(control))

            minWidthDip = Math.Max(0.0F, minWidthDip)
            maxWidthDip = Math.Max(0.0F, maxWidthDip)

            If maxWidthDip > 0.0F AndAlso minWidthDip > maxWidthDip Then
                minWidthDip = maxWidthDip
            End If

            Dim entry As New ToolbarEntry With {
                .Id = If(id, String.Empty),
                .Kind = kind,
                .Control = control,
                .WidthDip = Math.Max(0.0F, widthDip),
                .Sizing = sizing,
                .Alignment = alignment,
                .MinWidthDip = minWidthDip,
                .MaxWidthDip = maxWidthDip
            }

            _items.Add(entry)
            RegisterChild(control)
            RequestSizeLayoutRefreshForSizeAffectingChange("MASToolbar.AddControlInternal")
            InvalidateVisual()
        End Sub

        Private Function FindIdByControl(control As MASControlBase) As String
            For Each item As ToolbarEntry In _items
                If item Is Nothing Then Continue For

                If Object.ReferenceEquals(item.Control, control) Then
                    Return item.Id
                End If
            Next

            Return String.Empty
        End Function

        Private Shared Function SafeName(id As String) As String
            If String.IsNullOrWhiteSpace(id) Then Return "ToolbarItem"
            Return "Toolbar_" & id.Trim()
        End Function

#End Region

    End Class

End Namespace
