Option Strict On
Option Explicit On

Imports System

Namespace Nexamas.UI.Composition

    ''' <summary>
    ''' Logical-unit thickness used for padding and margins.
    ''' It owns values only and performs no DPI conversion.
    ''' Invalid values are preserved so layout diagnostics can report them,
    ''' while layout math must use sanitized accessors from MASLayoutNodeBase.
    ''' </summary>
    <Global.System.ComponentModel.EditorBrowsable(Global.System.ComponentModel.EditorBrowsableState.Advanced)>
    Friend Structure MASLayoutThickness

        Friend ReadOnly Property Left As Single
        Friend ReadOnly Property Top As Single
        Friend ReadOnly Property Right As Single
        Friend ReadOnly Property Bottom As Single

        Friend Sub New(left As Single, top As Single, right As Single, bottom As Single)
            Me.Left = left
            Me.Top = top
            Me.Right = right
            Me.Bottom = bottom
        End Sub

        Friend ReadOnly Property Horizontal As Single
            Get
                Return SafeValue(Left) + SafeValue(Right)
            End Get
        End Property

        Friend ReadOnly Property Vertical As Single
            Get
                Return SafeValue(Top) + SafeValue(Bottom)
            End Get
        End Property

        Friend ReadOnly Property HasInvalidValues As Boolean
            Get
                Return IsInvalid(Left) OrElse IsInvalid(Top) OrElse IsInvalid(Right) OrElse IsInvalid(Bottom)
            End Get
        End Property

        Friend Shared Function SafeValue(value As Single) As Single
            If IsInvalid(value) Then Return 0.0F
            Return value
        End Function

        Friend Shared Function IsInvalid(value As Single) As Boolean
            Return Single.IsNaN(value) OrElse Single.IsInfinity(value) OrElse value < 0.0F
        End Function

        Friend Shared Function None() As MASLayoutThickness
            Return New MASLayoutThickness(0.0F, 0.0F, 0.0F, 0.0F)
        End Function

        Friend Shared Function All(value As Single) As MASLayoutThickness
            Return New MASLayoutThickness(value, value, value, value)
        End Function

        Friend Shared Function Symmetric(horizontal As Single, vertical As Single) As MASLayoutThickness
            Return New MASLayoutThickness(horizontal, vertical, horizontal, vertical)
        End Function

    End Structure

End Namespace
