Option Strict On
Option Explicit On

Imports System
Imports Nexamas.UI.Controls
Imports Nexamas.UI.Layout

Namespace Nexamas.UI.Composition

    ''' <summary>
    ''' Public form-field description for Page.Form field rows.
    ''' It describes a label/control pair only. It does not render, validate, bind data,
    ''' own TextInput, or mutate ControlsLayer/BoundsLogical.
    ''' </summary>
    <Global.System.ComponentModel.EditorBrowsable(Global.System.ComponentModel.EditorBrowsableState.Advanced)>
    Friend NotInheritable Class MASFormField

        Friend ReadOnly Property Label As MASControlBase
        Friend ReadOnly Property Field As MASControlBase
        Friend ReadOnly Property LabelWidth As MASSizePolicy
        Friend ReadOnly Property FieldWidth As MASSizePolicy
        Friend ReadOnly Property FieldHeight As MASSizePolicy
        Friend ReadOnly Property RowHeight As MASSizePolicy
        Friend ReadOnly Property Spacing As Single
        Friend ReadOnly Property Margin As MASLayoutThickness
        Friend ReadOnly Property Key As String

        Friend Sub New(label As MASControlBase,
                       field As MASControlBase,
                       Optional labelWidth As MASSizePolicy = Nothing,
                       Optional fieldWidth As MASSizePolicy = Nothing,
                       Optional fieldHeight As MASSizePolicy = Nothing,
                       Optional rowHeight As MASSizePolicy = Nothing,
                       Optional spacing As Single = MASCompositionPageSizeTokens.FormFieldSpacing,
                       Optional margin As MASLayoutThickness = Nothing,
                       Optional key As String = "")

            If label Is Nothing Then Throw New ArgumentNullException(NameOf(label))
            If field Is Nothing Then Throw New ArgumentNullException(NameOf(field))

            Me.Label = label
            Me.Field = field
            Me.LabelWidth = If(labelWidth, MASCompositionPageSizeTokens.FormFieldLabelWidthPolicy())
            Me.FieldWidth = If(fieldWidth, MASSizePolicy.Fill(1.0F))
            Me.FieldHeight = If(fieldHeight, MASSizePolicy.Auto())
            Me.RowHeight = If(rowHeight, MASSizePolicy.Auto())
            Me.Spacing = SafeSpacing(spacing)
            Me.Margin = margin
            Me.Key = If(key, String.Empty)
        End Sub

        Friend Function ToLayoutItem() As MASLayoutItem
            Dim row As IMASLayoutNode =
                MASComposition.Horizontal(
                    spacing:=Spacing,
                    padding:=MASLayoutThickness.None(),
                    alignment:=MASLayoutAlignment.Stretch,
                    children:=New MASLayoutItem() {
                        MASComposition.Item(
                            control:=Label,
                            width:=LabelWidth,
                            height:=MASSizePolicy.Auto(),
                            alignment:=MASLayoutAlignment.Center),
                        MASComposition.Item(
                            control:=Field,
                            width:=FieldWidth,
                            height:=FieldHeight,
                            alignment:=MASLayoutAlignment.Stretch)
                    }
                )

            Return MASComposition.NodeItem(
                node:=row,
                width:=MASSizePolicy.Fill(1.0F),
                height:=RowHeight,
                alignment:=MASLayoutAlignment.Stretch,
                margin:=Margin,
                key:=Key)
        End Function

        Private Shared Function SafeSpacing(value As Single) As Single
            Return MASCompositionPageSizeTokens.SafeSpacing(value)
        End Function

    End Class

End Namespace
