Imports DevExpress.Utils
Imports DevExpress.XtraTreeList.Nodes
Imports DigitalData.Modules.Base
Imports DigitalData.Modules.Logging

Public Class ClassNodeCreator
    Inherits BaseClass

    Private ReadOnly _ImageList As ImageCollection

    Public Sub New(pLogConfig As LogConfig, pImageList As ImageCollection)
        MyBase.New(pLogConfig)
        _ImageList = pImageList
    End Sub

    Public Function AddNode(pEntityId As Integer, pNodeCaption As String, pParentNodeId As Integer, pNodeConfigId As Integer, pCreateRecord As Boolean) As Boolean

        Dim oRecordId

        ' If a record should be created, do it.
        ' Otherwise we set the record id to 0.
        If pCreateRecord Then
            ' Create a new Record in the given Entity
            oRecordId = ClassRecordCommands.CreateRecordProcedure(pEntityId)
            If oRecordId = 0 Then
                Logger.Warn("Could not create a record. Exiting.")
                Return False
            End If
        Else
            ' Zero will be handled by CreateNodeProcedure
            oRecordId = 0
        End If

        ' Update the proxy?
        ClassProxy.PRPROXY_RECORD_UPD_INS(pEntityId, oRecordId)

        ' Create the Node in the Database
        Dim oNodeIdentifier = $"CONFIG {oRecordId}-{pNodeConfigId}"
        Dim oNodeId = ClassNodeNavigation.CreateNodeProcedure(oRecordId, pNodeConfigId, pNodeCaption, oNodeIdentifier, pParentNodeId, USER_USERNAME)

        If oNodeId = 0 Then
            Logger.Warn("Node could not be created in database. Exiting.")
            Return False
        End If

        Return True
    End Function
End Class
