Imports DLLLicenseManager
Public Class frmLicense
    Public _lizenzManager As ClassLicenseManager
    Private Sub btnPW_check_Click(sender As System.Object, e As System.EventArgs) Handles btnPW_check.Click
        OpenLicenseManager()
    End Sub
    Sub OpenLicenseManager()
        If Me.txtPW.Text <> "" Then
            If Me.txtPW.Text = "35452dd!" Then
                Me.grbBoxlicense.Visible = True
                Refresh_Licence(False)
                Me.lblPW.Visible = False
                Me.txtPW.Visible = False
                Me.btnPW_check.Visible = False
            Else
                Me.grbBoxlicense.Visible = False
                MsgBox("Das eingegebene Passwort stimmt nicht überein!", MsgBoxStyle.Critical, "Falsche Eingabe:")
                Me.txtPW.Text = ""
                Me.txtPW.Focus()
            End If
        End If
    End Sub

    Private Sub btnnewLicenses_Click(sender As System.Object, e As System.EventArgs) Handles btnnewLicenses.Click
        Try
            If Me.txtNewlicences.Text <> "" And txtDossierCount.Text <> "" Then
                'Encode the license
                Dim result As String = Me._lizenzManager.EncodeLicenseKey(txtNewlicences.Text & "#" & dtpGueltig_GI.Text & "#" & txtDossierCount.Text, "#DigitalData35452!#")

                Dim sql As String = "UPDATE TBDD_MODULES SET LICENSE = '" & result & "'  WHERE UPPER(NAME) = UPPER('Record-Organizer')"
                If ClassDatabase.Execute_non_Query(sql, True) = True Then
                    txtlicensekey.Text = result
                    Refresh_Licence(True)
                End If

                'MsgBox("Die Lizenzen wurden erfolgreich aktualisiert!", MsgBoxStyle.Exclamation, "Erfolgsmeldung:")
            End If
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical, "Fehler bei New Licenses:")
        End Try
    End Sub
    Sub Refresh_Licence(Neu As Boolean)
        Dim lizenzzahl As Integer
        Try
            Me._lizenzManager = New ClassLicenseManager("#DigitalData35452!#", "")
            Dim sql As String = "SELECT LICENSE FROM TBDD_MODULES WHERE UPPER(NAME) = UPPER('Record-Organizer')"

            Dim lic As String = ClassDatabase.Execute_Scalar(sql, True)
            Dim licString = Me._lizenzManager.DecodeLicenseKey(lic)

            Dim split() = licString.ToString.Split("#")

            If lic <> "" Then
                lizenzzahl = split(0)
                LICENSE_COUNT = lizenzzahl
            Else
                lizenzzahl = "0"
            End If
            txtNewlicences.Text = lizenzzahl
            dtpGueltig_GI.Value = split(1)
            If CDate(split(1)) < CDate(Now.ToShortDateString) Then
                License_Expired = True
            Else
                License_Expired = False
            End If

            txtDossierCount.Text = split(2)
            LICENSE_COUNT = CInt(txtNewlicences.Text)

            LICENSE_DOSSIER_COUNT = split(2)
            txtDossierCount.Text = split(2)

            Me.lblAktuelleLizenzen.Text = "Aktuelle Anzahl Lizenzen: " & lizenzzahl
            If Neu = True Then
                If CInt(lizenzzahl) > 0 Then
                    MsgBox("Die Lizenz wurde aktualisiert!", MsgBoxStyle.Information, "Erfolgsmeldung:")
                End If
            End If
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Fehler bei Licensemanager:")
        End Try
    End Sub

    Private Sub frmLicense_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        Try
            grbBoxlicense.Visible = False
            Me.lblPW.Visible = True
            Me.txtPW.Visible = True
            Me.btnPW_check.Visible = True
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Fehler bei Formular Load:")
        End Try
    End Sub

    Private Sub frmLicense_KeyUp(sender As Object, e As KeyEventArgs) Handles MyBase.KeyUp
        If e.KeyCode = Keys.Return Then
            If Me.txtPW.Focus = True Then
                OpenLicenseManager()
            End If
        End If
    End Sub
End Class