Imports DD_LIB_Standards
Public Class frmLicense
    Dim CLIENT_ID As Integer
    Public _lizenzManager As clsLicenseManager
    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
                If DT_CLIENT_USER.Rows.Count > 1 Then
                    lblClient1.Visible = False
                    txtClient.Visible = False
                    lblClient.Visible = True
                    cmbClients.Visible = True
                    Try
                        cmbClients.DataSource = DT_CLIENT_USER
                        cmbClients.DisplayMember = DT_CLIENT_USER.Columns("CLIENT_NAME").ColumnName
                        cmbClients.ValueMember = DT_CLIENT_USER.Columns("CLIENT_ID").ColumnName
                    Catch ex As Exception
                        MsgBox("Unexpected Error in Loading Clients: " & ex.Message, MsgBoxStyle.Critical)
                    End Try
                ElseIf DT_CLIENT_USER.Rows.Count = 1 Then
                    lblClient1.Visible = True
                    txtClient.Visible = True

                    lblClient.Visible = False
                    cmbClients.Visible = False
                    CLIENT_ID = DT_CLIENT_USER.Rows(0).Item("CLIENT_ID")
                    txtClient.Text = CLIENT_ID
                End If


            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
            _lizenzManager = New clsLicenseManager("#DigitalData35452!#", "")
            If Me.txtNewlicences.Text <> "" Then
                'Encode the license
                Dim result As String = Me._lizenzManager.EncodeLicenseKey(txtNewlicences.Text & "#" & dtpGueltig_GI.Text, "#DigitalData35452!#")

                Dim sql As String = String.Format("UPDATE TBDD_CLIENT SET LICENSE_CBSEARCHER = '" & result & "' WHERE GUID = {0}", CLIENT_ID)
                If Database.ExecuteNonQuery(sql) = False Then
                    sql = $"UPDATE TBDD_MODULES SET LICENSE = '{result}' where SHORT_NAME = 'CW'"
                    Database.ExecuteNonQuery(sql)
                    Refresh_Licence(True)
                Else
                    MsgBox("Could not renew the license!!", MsgBoxStyle.Critical)
                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)
        Try
            ClassLicense.Refresh_Licence(CLIENT_ID)

            txtNewlicences.Text = LICENSE_COUNT
            dtpGueltig_GI.Value = LICENSE_DATE

            If Neu = True Then
                If CInt(LICENSE_COUNT) > 0 Then
                    Dim msg As String = "Die Lizenz wurde aktualisiert!" & vbNewLine & Now
                    If USER_LANGUAGE <> "de-DE" Then
                        msg = "The license was renewed!" & vbNewLine & Now
                    End If
                    MsgBox(msg, MsgBoxStyle.Information, "License renewed:")
                End If
            End If
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Error in 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
            Me.txtPW.Text = ""
        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

    Private Sub cmbClients_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cmbClients.SelectedIndexChanged
        If cmbClients.SelectedIndex <> -1 Then
            Try
                CLIENT_ID = cmbClients.SelectedValue
                Refresh_Licence(False)
            Catch ex As Exception
                CLIENT_ID = 99
            End Try

        End If
    End Sub
End Class