Option Strict On
Option Explicit On

Imports Nexamas.UI.Icons

Namespace Nexamas.UI.Components

    Friend NotInheritable Class MASTreeIconService

        Private Sub New()
        End Sub

        Friend Shared Function ResolveNodeIcon(node As MASTreeNode,
                                               resolver As IMASIconResolver,
                                               sizeDip As Single,
                                               dpi As Single) As MASIconResult

            Dim result As MASIconResult = Nothing

            If node Is Nothing Then
                Return MASIconResult.FromMasIcon(MASIconKind.File)
            End If

            If resolver IsNot Nothing Then
                Try
                    Dim request As MASIconRequest =
                        MASIconRequestMapper.FromTreeNode(
                            node:=node,
                            sizeDip:=sizeDip,
                            dpi:=dpi
                        )

                    result = resolver.ResolveIcon(request)
                Catch masCaughtException1 As Exception
                    Nexamas.UI.Diagnostics.MASExceptionSilencer.SwallowRecoverable(masCaughtException1)
                    result = Nothing
                End Try
            End If

            If result IsNot Nothing AndAlso result.HasIcon Then
                Return result
            End If

            If node.HasChildren Then
                Return MASIconResult.FromMasIcon(MASIconKind.Folder)
            End If

            Return MASIconResult.FromMasIcon(MASIconKind.File)
        End Function

    End Class

End Namespace