Imports DigitalData.Modules.Logging
Imports DigitalData.Modules.EDMI.API.EDMIServiceReference

Public Class ClassCommonCommands
    Private _LogConfig As LogConfig
    Private _Logger As Logger

    Public Sub New(LogConfig As LogConfig)
        _LogConfig = LogConfig
        _Logger = LogConfig.GetLogger()
    End Sub

    Public Async Function FNIDB_RADM_NEW_USER2GROUP(UserId As Integer, GroupId As Integer) As Task(Of Integer)
        Try
            Dim oSQL = $"SELECT FNIDB_RADM_NEW_USER2GROUP({UserId},{GroupId}, '{Environment.UserName}') FROM RDB$DATABASE;"
            Dim oRequest = Await My.Channel.CreateDatabaseRequestAsync($"Add User To Group", False)

            Dim oResult = Await My.Channel.ReturnScalarAsync(oSQL)
            Await My.Channel.CloseDatabaseRequestAsync()

            If oResult.OK Then
                Return oResult.Scalar
            Else
                Return Nothing
            End If
        Catch ex As Exception
            _Logger.Error(ex)
            Throw ex
        End Try
    End Function

    Public Async Function FNIDB_RADM_NEW_GROUP2GROUP(ParentGroupId As Integer, GroupId As Integer) As Task(Of Integer)
        Try
            Dim oSQL = $"SELECT FNIDB_RADM_NEW_GROUP2GROUP({GroupId},{ParentGroupId}, '{Environment.UserName}') FROM RDB$DATABASE;"
            Dim oRequest = Await My.Channel.CreateDatabaseRequestAsync($"Add Group To Group", False)
            Dim oRecordIds As New List(Of Integer)

            Dim oResult = Await My.Channel.ReturnScalarAsync(oSQL)
            Await My.Channel.CloseDatabaseRequestAsync()

            If oResult.OK Then
                Return oResult.Scalar
            Else
                Return Nothing
            End If
        Catch ex As Exception
            _Logger.Error(ex)
            Throw ex
        End Try
    End Function

    Public Async Function FNIDB_DELETE_RECORD_FINALLY(RecordId As Integer) As Task(Of Boolean)
        Try
            Dim oSQL = $"SELECT FNIDB_DELETE_RECORD_FINALLY({RecordId},'{Environment.UserName}') FROM RDB$DATABASE;"
            Dim oRequest = Await My.Channel.CreateDatabaseRequestAsync($"Delete Record", False)

            Dim oResult = Await My.Channel.ReturnScalarAsync(oSQL)
            Await My.Channel.CloseDatabaseRequestAsync()

            Return oResult.OK
        Catch ex As Exception
            _Logger.Error(ex)
            Throw ex
        End Try
    End Function

    Public Async Function FNIDB_NEW_DOC_VALUE(DocumentId As Int64, Syskey As String, LanguageKey As String, Value As String) As Task(Of ScalarResult)
        Try
            Dim oSQL = $"SELECT FNIDB_NEW_DOC_VALUE({DocumentId}, '{Syskey}', '{LanguageKey}', '{My.Application.User.UserName}', '{Value}') FROM RDB$DATABASE;"
            Dim oRequest = Await My.Channel.CreateDatabaseRequestAsync($"Delete Record", False)
            Dim oResult = Await My.Channel.ReturnScalarAsync(oSQL)

            Await My.Channel.CloseDatabaseRequestAsync()

            Return oResult
        Catch ex As Exception
            _Logger.Error(ex)
            Throw ex
        End Try
    End Function
End Class
