Imports System.Globalization
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
        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.txtlicenseCount.Text <> "" And txtProfileCount.Text <> "" Then
                Dim oDateddMMyyyy = dtp_Gültigkeit.Value.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture)
                Dim oLicString = txtlicenseCount.Text & "#" & oDateddMMyyyy & "#" & txtProfileCount.Text
                If txtModules.Text <> String.Empty Then
                    oLicString = oLicString & "#" & txtModules.Text
                End If
                Dim result As String = Me._lizenzManager.EncodeLicenseKey(oLicString, "#DigitalData35452!#")
                txtlicensekey.Text = result
                Me.TBPM_KONFIGURATIONTableAdapter.CmdUpdateLizenz(USER_USERNAME, CStr(result))
                Refresh_Licence(True)
                'MsgBox("Die Lizenzen wurden erfolgreich aktualisiert!", MsgBoxStyle.Exclamation, "Erfolgsmeldung:")
            End If
        Catch ex As Exception
            LOGGER.Error(ex)
            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 lic As String = TBPM_KONFIGURATIONTableAdapter.cmdGetLizenz
            Dim licString = Me._lizenzManager.DecodeLicenseKey(lic)

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

            If lic <> "" Then
                lizenzzahl = split(0)
            Else
                lizenzzahl = "0"
            End If
            Try
                dtp_Gültigkeit.Value = CDate(split(1))
            Catch ex As Exception
                LOGGER.Error(ex)
                Dim oLicDateString As String = split(1)
                Dim cultureInfo As System.Globalization.CultureInfo
                cultureInfo = New System.Globalization.CultureInfo("en-US")
                cultureInfo.DateTimeFormat.ShortDatePattern = "yyyy-mmm-yyyy"


                Dim oLicDate As Date = Convert.ToDateTime(oLicDateString, cultureInfo)

                Try
                    dtp_Gültigkeit.Value = oLicDate
                Catch ex1 As Exception
                    MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Unexpected error in Setting DTP-Value:")
                End Try

            End Try

            txtProfileCount.Text = split(2)
            LICENSE_PROFILES = split(2)
            Try
                Dim oMod = split(3)
                If Not IsNothing(split(3)) Then
                    txtModules.Text = split(3)
                End If
            Catch ex As Exception

            End Try

            Me.lblAktuelleLizenzen.Text = "Aktuelle Anzahl Lizenzen: " & lizenzzahl
            If Neu = True Then
                If CInt(lizenzzahl) > 0 Then
                    MsgBox("License was refreshed!" & vbNewLine & Now.ToString, MsgBoxStyle.Information, "Success:")
                End If
            End If
        Catch ex As Exception
            LOGGER.Error(ex)
            MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Unexpected error in LicenseManager-RefreshLicense:")
        End Try
    End Sub

    Private Sub frmLicense_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        Try
            TBPM_KONFIGURATIONTableAdapter.Connection.ConnectionString = CONNECTION_STRING_ECM
        Catch ex As Exception
            LOGGER.Error(ex)
            MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Fehler bei Formular Load:")
        End Try
    End Sub
End Class