Imports EDSL.Entities
Imports EDSL.DAL

Public Class miscFunctions
    Public Function ConvertDateToDBFormat(ByVal strInputDate As String) As String
        Dim strOutput As String
        strOutput = Format(CDate(strInputDate), "yyyy-MM-dd")
        Return strOutput
    End Function
    Public Function ConvertDateToDDMMYYFormat(ByVal strInputDate As String) As String
        Dim strOutput As String
        strOutput = Format(CDate(strInputDate), "dd/MM/yyyy")
        Return strOutput
    End Function
    Public Function ConvertDateToLongDisplayFormat(ByVal strInputDate As String) As String
        If strInputDate = "" Then
            Return ""
        End If
        Dim datInput As Date = CDate(strInputDate)
        Dim strOutput As String = ""
        strOutput += datInput.DayOfWeek.ToString & " "
        strOutput += datInput.ToLongDateString
        Return strOutput
    End Function
    Public Function GetFilenameTimestamp() As String
        Dim strOutput As String
        strOutput = Format(Now(), "yyyyMMdd") & "_" & Format(Now(), "hhmmss") & "_"
        Return strOutput
    End Function
    Public Function CleanFilename(ByVal strFileName As String) As String
        Dim strOutput As String = strFileName

        strOutput = Replace(strOutput, "{", "")
        strOutput = Replace(strOutput, "}", "")
        strOutput = Replace(strOutput, " ", "")
        strOutput = Replace(strOutput, "!", "")
        strOutput = Replace(strOutput, "$", "")
        strOutput = Replace(strOutput, "&", "")
        strOutput = Replace(strOutput, "%", "")
        strOutput = Replace(strOutput, "#", "")
        strOutput = Replace(strOutput, "(", "")
        strOutput = Replace(strOutput, ")", "")
        strOutput = Replace(strOutput, "+", "")
        strOutput = Replace(strOutput, "*", "")
        strOutput = Replace(strOutput, ",", "")
        strOutput = Replace(strOutput, ";", "")
        strOutput = Replace(strOutput, "?", "")
        strOutput = Replace(strOutput, "@", "")
        strOutput = Replace(strOutput, "—", "")
        strOutput = Replace(strOutput, "–", "")
        strOutput = Replace(strOutput, "~", "")

        Return GetFilenameTimestamp() & strOutput
    End Function
    Public Function CleanUrlString(ByVal strFileName As String) As String
        Dim strOutput As String = strFileName

        strOutput = Replace(strOutput, "{", "")
        strOutput = Replace(strOutput, "}", "")
        strOutput = Replace(strOutput, " ", "")
        strOutput = Replace(strOutput, "!", "")
        strOutput = Replace(strOutput, "$", "")
        strOutput = Replace(strOutput, "&", "")
        strOutput = Replace(strOutput, "%", "")
        strOutput = Replace(strOutput, "#", "")
        strOutput = Replace(strOutput, "(", "")
        strOutput = Replace(strOutput, ")", "")
        strOutput = Replace(strOutput, "+", "")
        strOutput = Replace(strOutput, "*", "")
        strOutput = Replace(strOutput, ",", "")
        strOutput = Replace(strOutput, ";", "")
        strOutput = Replace(strOutput, "?", "")
        strOutput = Replace(strOutput, "@", "")
        strOutput = Replace(strOutput, "~", "")
        strOutput = Replace(strOutput, "'", "")
        strOutput = Replace(strOutput, ".", "")
        strOutput = Replace(strOutput, "-", " ")
        strOutput = Replace(strOutput, "|", " ")
        strOutput = Replace(strOutput, "/", " ")
        strOutput = Replace(strOutput, ":", " ")

        Return strOutput
    End Function

    Public Function UploadFile(ByRef objFileUpload As FileUpload, ByVal intType As Integer, ByVal idFileName As String) As String
        Dim strFileName As String = objFileUpload.PostedFile.FileName
        Dim strUploadDirectory As String = ConfigurationManager.AppSettings("UploadFilePath")

        If objFileUpload.HasFile Then

            strFileName = idFileName & System.IO.Path.GetExtension(strFileName).ToLower()


            strFileName = CleanFilename(strFileName)
            Dim strExtensionName As String = System.IO.Path.GetExtension(strFileName).ToLower()
            Dim intFileSize = objFileUpload.PostedFile.ContentLength / 1024

            Select Case intType
                Case 1
                    Select Case strExtensionName
                        Case ".jpg", ".gif", ".png"
                            If intFileSize < 1024 Then
                                objFileUpload.PostedFile.SaveAs(strUploadDirectory & "images\" & strFileName)
                            Else
                                strFileName = "EdenlightError:::Image is too large maximum size is 1Mb and that image is " & System.Math.Round(intFileSize / 1000, 2) & "Mb"
                            End If
                        Case Else
                            strFileName = "EdenlightError:::Can not upload that file type <br /> You can only upload jpg, gif or png files"
                    End Select
                Case 2
                    Select Case strExtensionName
                        Case ".pdf", ".doc", ".docx"
                            objFileUpload.PostedFile.SaveAs(strUploadDirectory & "/cvsubmitted/" & strFileName)
                            Dim objCVUpdate As New dbMainEnquiry
                            'Dim successful As New Integer
                            'successful = objCVUpdate.UpdateCVRecord(idFileName, strFileName)
                            'If successful = 1 Then
                            'strFileName = "EdenlightError:::There was an error saving details to the database"
                            ' End If
                        Case Else
                            strFileName = "EdenlightError:::Can not upload that file type <br /> You can only upload .pdf, .doc, .docx files"
                    End Select
                Case Else

            End Select
        End If
        Return strFileName
    End Function
End Class
