Imports System.IO
Imports DigitalData.GUIs.Common

Public Class ClassIndexFunctions
    Public Shared Function FileExistsinDropTable(pFilename As String, pHandleType As String) As Date
        Dim oSQL As String
        Dim oHash As String

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

            If pFilename.ToUpper.EndsWith(".MSG") And (pHandleType = "|OUTLOOK_MESSAGE|" Or pHandleType = "|MSGONLY|") Then
                ' MSG Files cannot be hashed based on filecontents, so we use the filename instead
                oHash = FILESYSTEM.GetChecksumFromString(pFilename)
            Else
                ' If file cannot be accessed, checksum cannot be generated
                ' In this case, the file should be treated as not yet existing
                oHash = FILESYSTEM.GetChecksum(pFilename)
            End If

            If oHash Is Nothing Then
                LOGGER.Warn("Checksum for file {0} could not be generated. Treating as new file.", pFilename)
                Return Nothing
            End If

            'oSQL = "SELECT *  FROM TBGI_FILES_USER WHERE UPPER(FILE_HASH) = UPPER('" & oHash & "') AND WORKED = 0 ORDER BY ADDED_WHEN"
            'Dim oResult As DataTable = ClassDatabase.Return_Datatable_CS(oSQL, MyConnectionString, True)

            '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"
            'Dim oResult = ClassDatabase.Return_Datatable_CS(oSQL, MyConnectionString, True)
            Dim oResult = DATABASE_ECM.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

    Public Shared Function CheckDuplicateFiles(pForm As Form, pFilepath As String, pModuleTitle As String, Optional pHandleType As String = "")
        Dim oFileInfo As New FileInfo(pFilepath)
        Dim oFormHelper As New FormHelper(LOGCONFIG, pForm)
        Dim oFilename As String = oFileInfo.Name
        Dim oFileExists As Date = FileExistsinDropTable(pFilepath, pHandleType)

        If oFileExists.Equals(Date.MinValue) Then
            Return True
        Else
            Dim oResult As DialogResult
            Dim oDate As String = oFileExists.ToString("d")
            Dim oBoxTitle = $"fileFLOW - {pModuleTitle}"
            Dim oMessage As String

            If USER_LANGUAGE = "de-DE" Then
                oMessage = $"Die Datei [{oFilename}] wurde bereits am [{oDate}] verarbeitet.{vbNewLine}{vbNewLine}Wollen Sie die gleiche Datei noch einmal verarbeiten?"
            Else
                oMessage = $"The file [{oFilename}] has already been processed at [{oDate}].{vbNewLine}{vbNewLine}Do you want to process the same file again?"
            End If


            oResult = oFormHelper.ShowQuestionMessage(oMessage, oBoxTitle)
            'oResult = MessageBox.Show(oMessage, oBoxTitle, MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly)

            If oResult = DialogResult.Yes Then
                Return True
            End If
        End If

        Return False
    End Function
End Class
