Imports DigitalData.Modules.Base
Imports DigitalData.Modules.Logging
Imports DigitalData.Modules.Language

Namespace DetailForms

    Public Class Loader

        Inherits BaseClass

        Public Forms As New Dictionary(Of String, DetailForm)

        Public Sub New(pLogConfig As LogConfig)
            MyBase.New(pLogConfig)
        End Sub

        Public Function Load_SQLData() As Boolean
            Try
                Dim oTable As DataTable = My.DatabaseECM.GetDatatable("SELECT * FROM TBZF_ADMIN_SOURCE_SQL WHERE SCOPE = 'OVERVIEW'")
                Forms.Clear()

                For Each oRow As DataRow In oTable.Rows
                    Dim oCHeck = oRow.Item("ENTITY_TITLE").ToString

                    ' TODO: Do a proper replace lol
                    Dim oSQL As String = oRow.Item("SQL_COMMAND").ToString.Replace("@LANG_CODE", My.Application.User.Language)

                    Dim oForm As New DetailForm() With {
                        .Data = New DetailFormData With {
                            .Guid = CInt(oRow.Item("GUID")),
                            .ParentId = CInt(oRow.Item("PARENT_ID")),
                            .Entity = oRow.Item("ENTITY_TITLE").ToString,
                            .Scope = oRow.Item("SCOPE").ToString,
                            .SQLCommand = oRow.Item("SQL_COMMAND").ToString.Replace("@LANG_CODE", My.Application.User.Language),
                            .PrimaryKey = oRow.ItemEx(("PK_COLUMN"), String.Empty)
                        }
                    }

                    Try
                        oForm.Data.SQLResult = My.DatabaseECM.GetDatatable(oForm.Data.SQLCommand)
                    Catch ex As Exception
                        oForm.Data.SQLResult = Nothing
                        Logger.Error(ex)
                    End Try

                    Dim oKey As String = oForm.Key
                    Forms.Add(oKey, oForm)
                Next

                Return True
            Catch ex As Exception
                Logger.Error(ex)
                Return False
            End Try
        End Function

    End Class

End Namespace
