Imports DigitalData.GUIs.ZooFlow.Base
Imports DigitalData.Modules.Logging

Public Class ClassHelpers
    Inherits BaseClass

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

    Public Function GetValueFromDatatable(pDatatable As DataTable, pFilterString As String, pCheckColumn As String, pSortString As String) As Object
        Try
            If pDatatable Is Nothing OrElse pDatatable.Rows.Count = 0 Then
                Return Nothing
            End If

            Dim oDataView As DataView = pDatatable.DefaultView
            oDataView.RowFilter = pFilterString

            If oDataView.Count > 1 Then
                Logger.Warn("Multiple Results For Filter [{0}] and Column [{1}].", pFilterString, pCheckColumn)
                Return Nothing
            End If

            If oDataView.Count = 0 Then
                Logger.Warn("No Results For Filter [{0}] and Column [{1}].", pFilterString, pCheckColumn)
                Return Nothing
            End If

            Dim oView As DataRowView = oDataView.Item(0)
            Dim oRow As DataRow = oView.Row

            Try
                Return oRow.Item(pCheckColumn)
            Catch ex As Exception
                Logger.Warn("Datatable with Filter [{0}] does not contain Column [{1}]", pFilterString, pCheckColumn)
                Logger.Error(ex)
                Return Nothing
            End Try
        Catch ex As Exception
            Logger.Error(ex)
            Return Nothing
        End Try
    End Function

    Public Function GetFilteredDatatable(pDatatable As DataTable, pFilterString As String, pSortString As String) As DataTable
        Try
            If pDatatable.Rows.Count = 0 Then
                Return Nothing
            End If

            Dim oSelectedRows = pDatatable.Select(pFilterString, pSortString)

            If oSelectedRows.Count = 0 Then
                Return Nothing
            End If

            Dim oFilteredTable As DataTable = New DataTable()
            oFilteredTable = oSelectedRows.CopyToDataTable()
            Return oFilteredTable

        Catch ex As Exception
            Logger.Error(ex)
            Return Nothing

        End Try
    End Function

    Public Shared Function FileExistsinDropTable(Filename As String) As Date
        Dim oSQL As String
        Dim oHash As String
        Dim oFilesystem As New DigitalData.Modules.Filesystem.File(My.LogConfig)

        Try
            If Filename.Contains("'") Then
                Filename = Filename.Replace("'", "''")
            End If

            Try
                oHash = oFilesystem.GetChecksum(Filename)
            Catch ex As Exception
                oHash = ""
            End Try

            oSQL = "SELECT *  FROM TBGI_FILES_USER WHERE UPPER(FILE_HASH) = UPPER('" & oHash & "') AND WORKED = 0 ORDER BY ADDED_WHEN"
            Dim oResult As DataTable = My.DatabaseECM.GetDatatable(oSQL)

            If oResult Is Nothing Then
                Return Nothing
            End If

            If oResult.Rows.Count = 0 Then
                oSQL = "SELECT * FROM TBGI_HISTORY WHERE UPPER(FILE_HASH) = UPPER('" & oHash & "') ORDER BY ADDED_WHEN"
                oResult = My.DatabaseECM.GetDatatable(oSQL)

                If oResult Is Nothing Then
                    Return Nothing
                End If

                If oResult.Rows.Count = 0 Then
                    Return Nothing
                Else
                    Dim oFirstRow As DataRow = oResult.Rows.Item(0)
                    Return oFirstRow.Item("ADDED_WHEN")
                End If
            Else
                Dim oFirstRow As DataRow = oResult.Rows.Item(0)
                Return oFirstRow.Item("ADDED_WHEN")
            End If
        Catch ex As Exception
            MsgBox("Error in FileExistsinDropTable - Error-Message:" & vbNewLine & ex.Message & vbNewLine & "SQL-Command:" & vbNewLine & oSQL, MsgBoxStyle.Critical)
            Return Nothing
        End Try
    End Function
End Class
