Imports DigitalData.Modules.Base
Imports DigitalData.Modules.EDMI.API
Imports DigitalData.Modules.Logging

Namespace DocumentResultList
    Public Class CheckInOut
        Inherits BaseClass
        Private Client As Client

        Public Sub New(pLogConfig As LogConfig, pClient As Client)
            MyBase.New(pLogConfig)
            Client = pClient
        End Sub

        Public Async Function GetCheckoutState(pObjectId As Long) As Task(Of CheckoutState)
            Try
                Dim oSQL = $"SELECT * FROM VWOBJECT_CHECKED_OUT WHERE IDB_OBJ_ID = {pObjectId}"
                Dim oResponse = Await Client.GetDatatableFromIDBAsync(oSQL)

                If oResponse.ok = False Then
                    Return Nothing
                End If

                If oResponse.Table.Rows.Count = 0 Then
                    Return New CheckoutState()
                End If

                Dim oRow = oResponse.Table.Rows.Item(0)
                Dim oState As New CheckoutState With {
                    .IsCheckedOut = True,
                    .CheckedOutWhen = oRow.Item("CHECKED_OUT_WHEN"),
                    .CheckedOutWho = oRow.Item("EMAIL")
                }

                Return oState

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

        Public Class CheckoutState
            Public Property IsCheckedOut As Boolean = False
            Public Property CheckedOutWhen As Date = Nothing
            Public Property CheckedOutWho As String = Nothing
        End Class
    End Class
End Namespace
