Option Strict On
Option Explicit On

Imports System
Imports Nexamas.UI.Controls
Imports Nexamas.UI.Icons
Imports Nexamas.UI.Architecture
Imports Nexamas.UI.Values

Namespace Nexamas.UI.Components

    Friend NotInheritable Class MASFilePickerNavigationBar
        Inherits MASToolbar


        Private Shared ReadOnly _contract As MASResolvedComponentContract =
    MASArchitectureRuntime.ResolveContractOrThrow(GetType(MASFilePickerNavigationBar))

#Region "Fields"

        Private ReadOnly _btnBack As GlyphButton
        Private ReadOnly _btnForward As GlyphButton
        Private ReadOnly _btnUp As GlyphButton
        Private ReadOnly _btnRefresh As GlyphButton
        Private ReadOnly _pathBox As MASPathDisplayBox

        Private _canGoBack As Boolean
        Private _canGoForward As Boolean
        Private _canGoUp As Boolean
        Private _canRefresh As Boolean = True

#End Region

#Region "Events"

        Friend Event BackRequested As EventHandler
        Friend Event ForwardRequested As EventHandler
        Friend Event UpRequested As EventHandler
        Friend Event RefreshRequested As EventHandler

#End Region

#Region "Ctor"

        Friend Sub New()
            Me.HorizontalPaddingDip = FilePickerTokens.NavigationToolbarHorizontalPadding
            Me.VerticalInsetDip = FilePickerTokens.NavigationToolbarVerticalInset
            Me.GapDip = FilePickerTokens.NavigationToolbarGap
            Me.ItemHeightDip = FilePickerTokens.NavigationToolbarItemHeight

            _pathBox = New MASPathDisplayBox() With {
        .Name = "FilePickerPathBox"
    }

            AddControl(
        id:="Path",
        control:=_pathBox,
        widthDip:=0.0F,
        sizing:=MASToolbarItemSizing.Fill,
        alignment:=MASToolbarItemAlignment.Left,
        minWidthDip:=FilePickerTokens.NavigationPathMinWidth,
        maxWidthDip:=0.0F
    )

            _btnBack = AddIconButtonInternal(
    id:="Back",
    iconKind:=MASIconKind.Back,
    alignment:=MASToolbarItemAlignment.Right
)

            _btnForward = AddIconButtonInternal(
    id:="Forward",
    iconKind:=MASIconKind.Forward,
    alignment:=MASToolbarItemAlignment.Right
)

            _btnUp = AddIconButtonInternal(
    id:="Up",
    iconKind:=MASIconKind.Up,
    alignment:=MASToolbarItemAlignment.Right
)

            _btnRefresh = AddIconButtonInternal(
    id:="Refresh",
    iconKind:=MASIconKind.Refresh,
    alignment:=MASToolbarItemAlignment.Right
)

            AddHandler Me.ButtonClicked, AddressOf OnToolbarButtonClicked

            SyncChildEnabledState()
        End Sub

#End Region

#Region "Public API"

        Friend Property PathText As String
            Get
                Return _pathBox.Text
            End Get
            Set(value As String)
                _pathBox.Text = If(value, String.Empty)
            End Set
        End Property

        Friend ReadOnly Property PathControl As MASPathDisplayBox
            Get
                Return _pathBox
            End Get
        End Property

        Friend Property CanGoBack As Boolean
            Get
                Return _canGoBack
            End Get
            Set(value As Boolean)
                If _canGoBack = value Then Return
                _canGoBack = value
                SyncChildEnabledState()
            End Set
        End Property

        Friend Property CanGoForward As Boolean
            Get
                Return _canGoForward
            End Get
            Set(value As Boolean)
                If _canGoForward = value Then Return
                _canGoForward = value
                SyncChildEnabledState()
            End Set
        End Property

        Friend Property CanGoUp As Boolean
            Get
                Return _canGoUp
            End Get
            Set(value As Boolean)
                If _canGoUp = value Then Return
                _canGoUp = value
                SyncChildEnabledState()
            End Set
        End Property

        Friend Property CanRefresh As Boolean
            Get
                Return _canRefresh
            End Get
            Set(value As Boolean)
                If _canRefresh = value Then Return
                _canRefresh = value
                SyncChildEnabledState()
            End Set
        End Property

#End Region

#Region "Keyboard"

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

#End Region

#Region "State"

        Protected Overrides Sub OnEnabledChanged()
            MyBase.OnEnabledChanged()
            SyncChildEnabledState()
        End Sub

        Private Sub SyncChildEnabledState()
            Dim allow As Boolean = Me.Enabled

            _btnBack.Enabled = allow
            _btnForward.Enabled = allow
            _btnUp.Enabled = allow
            _btnRefresh.Enabled = allow

            _btnBack.EnabledEx = allow AndAlso _canGoBack
            _btnForward.EnabledEx = allow AndAlso _canGoForward
            _btnUp.EnabledEx = allow AndAlso _canGoUp
            _btnRefresh.EnabledEx = allow AndAlso _canRefresh

            _pathBox.Enabled = allow
        End Sub

#End Region

#Region "Events"

        Private Sub OnToolbarButtonClicked(sender As Object,
                                           e As MASToolbarButtonClickedEventArgs)

            If e Is Nothing Then Return

            Select Case e.Id
                Case "Back"
                    RaiseEvent BackRequested(Me, EventArgs.Empty)

                Case "Forward"
                    RaiseEvent ForwardRequested(Me, EventArgs.Empty)

                Case "Up"
                    RaiseEvent UpRequested(Me, EventArgs.Empty)

                Case "Refresh"
                    RaiseEvent RefreshRequested(Me, EventArgs.Empty)
            End Select
        End Sub

#End Region

#Region "Dispose"

        Protected Overrides Sub Dispose(disposing As Boolean)
            If disposing Then
                Try
                    RemoveHandler Me.ButtonClicked, AddressOf OnToolbarButtonClicked
                Catch masCaughtException1 As Exception
                    Nexamas.UI.Diagnostics.MASExceptionSilencer.SwallowDisposeCleanup(masCaughtException1)
                End Try
            End If

            MyBase.Dispose(disposing)
        End Sub

#End Region


#Region "Unified MASSize Fluent API"

        ''' <summary>
        ''' Strongly typed entry into the shared MASSize intent API.
        ''' This method keeps fluent chains on the concrete element while delegating all sizing decisions to MASControlBase/MASSize.
        ''' </summary>
        Friend Shadows Function WithSize(sizeIntent As Nexamas.UI.Layout.MASSize) As MASFilePickerNavigationBar
            MyBase.SetSize(sizeIntent)
            Return Me
        End Function

        Friend Shadows Function WithDefaultSize() As MASFilePickerNavigationBar
            MyBase.SetSize(Nexamas.UI.Layout.MASSize.[Default])
            Return Me
        End Function

        Friend Shadows Function WithCompactSize() As MASFilePickerNavigationBar
            MyBase.SetSize(Nexamas.UI.Layout.MASSize.Compact)
            Return Me
        End Function

        Friend Shadows Function WithLargeSize() As MASFilePickerNavigationBar
            MyBase.SetSize(Nexamas.UI.Layout.MASSize.Large)
            Return Me
        End Function

        Friend Shadows Function WithFillWidthSize() As MASFilePickerNavigationBar
            MyBase.SetSize(Nexamas.UI.Layout.MASSize.FillWidth)
            Return Me
        End Function

        Friend Shadows Function WithHugContentSize() As MASFilePickerNavigationBar
            MyBase.SetSize(Nexamas.UI.Layout.MASSize.HugContent)
            Return Me
        End Function

#End Region
    End Class

End Namespace
