Imports System.Globalization
Imports DLLLicenseManager
Public Class ClassLicence

    Private Shared _lizenzManager As ClassLicenseManager
    Public Shared Sub Refresh_Licence(CLIENT_ID As Integer)
        Try

            Dim todayDate As Date = New Date(Now.Year, Now.Month, Now.Day)
            LOGGER.Debug("todayDate: " & todayDate.ToString)
            _lizenzManager = New ClassLicenseManager("#DigitalData35452!#", "")
            Dim sql As String = String.Format("SELECT LICENSE_PMO FROM TBDD_CLIENT WHERE GUID = {0}", CLIENT_ID)

            Dim lic As String = MYDB_ECM.GetScalarValue(sql)
            If lic = "" Then
                LICENSE_COUNT = 0
                LICENSE_DATE = "1900-01-01"
                LICENSE_DOSSIER_COUNT = 0
                Exit Sub
            End If
            Dim licString = _lizenzManager.DecodeLicenseKey(lic)
            Dim split() = licString.ToString.Split("#")

            If lic <> "" Then
                LICENSE_COUNT = split(0)
            Else
                LICENSE_COUNT = 0
            End If
            Dim LicDATEsplit() = split(1).ToString.Split("-")
            Dim LICENSE_FILE_DATE As Date
            Try
                LICENSE_FILE_DATE = New Date(LicDATEsplit(0), LicDATEsplit(1), LicDATEsplit(2))
                LOGGER.Debug("LICENSE_FILE_DATE: " & LICENSE_FILE_DATE.ToString)
                Try
                    Try
                        LICENSE_DATE = CDate(LICENSE_FILE_DATE)
                    Catch ex As Exception
                        LOGGER.Warn("Could not read LICENSE_DATE2: " & ex.Message)
                    End Try

                Catch ex As Exception
                    LOGGER.Warn("Could not read LICENSE_DATE1: " & ex.Message)
                End Try
            Catch ex As Exception
                LOGGER.Warn("Could not read LICENSE_DATE3: " & ex.Message)
                LICENSE_FILE_DATE = New Date(Now.Year, Now.Month, Now.Day)
            End Try




            If LICENSE_FILE_DATE < CDate(todayDate) Then
                MsgBox("Your license expired!" & vbNewLine & "Last valid date: " & split(1) & vbNewLine & "Please contact Digital Data.", MsgBoxStyle.Exclamation)
                LICENSE_EXPIRED = True
                LICENSE_COUNT = 0
            End If

            sql = String.Format("SELECT NAME, VALID_DATE, '' AS State FROM TBDD_LICENSE_ADD_ON WHERE CLIENT_ID = {0} AND MODULE = 'Record-Organizer'", CLIENT_ID)
            DT_LICENSE_ADDONS = MYDB_ECM.GetDatatable(sql)
            For Each license_Row As DataRow In DT_LICENSE_ADDONS.Rows
                Dim expired As Boolean = False
                LOGGER.Debug("AddOnName: " & license_Row.Item("NAME"))
                Dim result = ClassHelper.DecryptString(license_Row.Item("VALID_DATE"), license_Row.Item("NAME"))
                If result <> "" Then
                    Dim datesplit() = result.ToString.Split("-")
                    Dim licdate As Date = New Date(datesplit(0), datesplit(1), datesplit(2))
                    Dim license_date = CDate(result)
                    If CDate(licdate) < CDate(todayDate) Then
                        expired = True
                        license_Row.Item("State") = "Expired"
                    Else
                        expired = False
                        license_Row.Item("State") = "Valid"
                    End If
                Else
                    expired = True
                End If

                If license_Row.Item("NAME") = "Proxy-Server" Then
                    If expired = False Then
                        LICENSE_PROXY = True
                    End If
                ElseIf license_Row.Item("NAME") = "Site-AddOn" Then
                    If expired = False Then
                        LICENSE_SITE = True
                    End If
                ElseIf license_Row.Item("NAME") = "SAP-Connect" Then
                    If expired = False Then
                        LICENSE_SAP_Connect = True
                    End If
                End If
            Next

            Try
                LICENSE_DOSSIER_COUNT = split(2)
            Catch ex As Exception
                LOGGER.Warn("Could not read License for LICENSE_DOSSIER_COUNT: " & ex.Message)
                LICENSE_DOSSIER_COUNT = 0
            End Try

        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Error in licensemanager:")
        End Try
    End Sub

    Public Shared Function license_is_Valid()
        If LICENSE_EXPIRED Then
            MsgBox("Your license has expired!" & vbNewLine & "Last valid date: " & LICENSE_DATE & vbNewLine & "Please contact Digital Data!", MsgBoxStyle.Exclamation)
            Return False
        Else : Return True

        End If
    End Function

End Class
