Imports DevExpress.XtraEditors
Imports DevExpress.XtraLayout
Imports DigitalData.Modules.Logging


Public Class ControlLoader
    Public Const CONTROL_TEXTEDIT = "TextBox"
    Public Const CONTROL_MEMOEDIT = "Memoedit"
    Public Const CONTROL_COMBOEDIT = "Combobox"
    Public Const CONTROL_CHECKEDIT = "Checkbox"
    Public Const CONTROL_RADIOEDIT = "Radiobutton"
    Public Const CONTROL_DATEEDIT = "Datepicker"

    Private _LayoutControlGroup As LayoutControlGroup
    Private _LayoutControls As List(Of BaseEdit)
    Private _LogConfig As LogConfig
    Private _LOgger As Logger

    Public ReadOnly Property LayoutControls As List(Of BaseEdit)
        Get
            If _LayoutControls Is Nothing Then
                _LayoutControls = New List(Of BaseEdit)
            End If
            Return _LayoutControls
        End Get
    End Property

    Public Sub New(LogConfig As LogConfig, LayoutControlGroup As LayoutControlGroup)
        _LayoutControlGroup = LayoutControlGroup
        _LogConfig = LogConfig
        _LOgger = LogConfig.GetLogger
    End Sub

    Public Sub AddControl(Name As String, Value As String, LayoutControlGroup As LayoutControlGroup)
        Dim oTextEdit As New SimpleLabelItem() With {
        .Name = Name,
        .Text = Name & " - " & Value
    }

        LayoutControlGroup.AddItem(oTextEdit)
    End Sub
    Public Sub AddControl(Name As String, Value As String)
        AddControl(Name, Value, _LayoutControlGroup)
    End Sub

    Public Sub AddSeparator(LayoutControlGroup As LayoutControlGroup)
        Dim oSeparator = New SimpleSeparator()

        LayoutControlGroup.AddItem(oSeparator)
    End Sub
    Public Sub AddSeparator()
        AddSeparator(_LayoutControlGroup)
    End Sub

    Public Sub LoadControls(Datatable As DataTable, LayoutControlGroup As LayoutControlGroup)
        For Each oRow As DataRow In Datatable.Rows
            Dim oCaption As String = oRow.Item("COLNAME")
            Dim oControlType As String = oRow.Item("CTRLTYPE")
            Dim oControlId As Int64 = oRow.Item("RECORD_ID")
            Dim oEditor As BaseEdit = CreateLayoutControl(oControlType, oControlId, oControlId)

            If oEditor Is Nothing Then
                Continue For
            End If

            oEditor.Tag = New Metadata() With {
                .Id = oControlId,
                .Type = oControlType,
                .Caption = oCaption
            }

            LayoutControls.Add(oEditor)
            LayoutControlGroup.AddItem(oCaption, oEditor)
        Next

        LayoutControlGroup.AddItem(New EmptySpaceItem())
    End Sub
    Public Sub LoadControls(Datatable As DataTable)
        LoadControls(Datatable, _LayoutControlGroup)
    End Sub

    Public Function CreateLayoutControl(Type As String, Name As String, Id As Int64)
        Dim oEditor As BaseEdit = Nothing

        LOGGER.Debug("Create new Control of type {0} with name {1}", Type, Name)

        Select Case Type
            Case CONTROL_TEXTEDIT
                Dim oTextEdit As New TextEdit() With {.Name = Name}
                oEditor = oTextEdit
            Case CONTROL_MEMOEDIT
                Dim oMemoEdit As New MemoEdit() With {.Name = Name}
                oEditor = oMemoEdit
            Case CONTROL_DATEEDIT
                Dim oDateEdit As New DateEdit() With {.Name = Name}
                oEditor = oDateEdit
            Case CONTROL_CHECKEDIT
                Dim oCheckEdit As New CheckEdit() With {.Name = Name}
                oEditor = oCheckEdit
            Case Else
                oEditor = Nothing
        End Select

        Return oEditor
    End Function
End Class




