Imports DLLLicenseManager
Public Class frmLicense
    Public _lizenzManager As ClassLicenseManager
    Dim CLIENT_ID As Integer

    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
                    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
                Else
                    lblClient.Visible = False
                    cmbClients.Visible = False
                End If

                If DT_CLIENT_USER.Rows.Count = 1 Then
                    CLIENT_ID = 0
                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
            If cmbClients.SelectedIndex = -1 Or CLIENT_ID = 99 Then
                MsgBox("Please choose a valid client!", MsgBoxStyle.Critical)
                Exit Sub
            End If
            _lizenzManager = New ClassLicenseManager("#DigitalData35452!#", "")
            If Me.txtNewlicences.Text <> "" And txtDossierCount.Text <> "" Then
                'Encode the license
                Dim ressultstring = txtNewlicences.Text & "#" & dtpGueltig_GI.Value.ToString("yyyy-MM-dd") & "#" & txtDossierCount.Text
                Dim result As String = Me._lizenzManager.EncodeLicenseKey(ressultstring, "#DigitalData35452!#")

                Dim sql As String = String.Format("UPDATE TBDD_CLIENT SET LICENSE_PMO = '" & result & "' WHERE GUID = {0}", CLIENT_ID)
                If MYDB_ECM.ExecuteNonQuery(Sql) = True Then
                    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)
        Try
            ClassLicence.Refresh_Licence(CLIENT_ID)

            cmbAddOn.Items.Clear()

            For Each row As DataRow In DT_LICENSE_ADDONS.Rows
                cmbAddOn.Items.Add(row.Item("NAME"))
            Next
            DataGridViewAddOns.DataSource = DT_LICENSE_ADDONS
            DataGridViewAddOns.Columns(0).Width = 200
            DataGridViewAddOns.Columns(1).Visible = False
            DataGridViewAddOns.Columns(2).Width = 100
            GridColor()
            txtNewlicences.Text = LICENSE_COUNT
            Try
                dtpGueltig_GI.Value = LICENSE_DATE
            Catch ex As Exception
                Dim dateTime As String = LICENSE_DATE.ToString
                Dim dt As DateTime = Convert.ToDateTime(dateTime)
                Dim format As String = "yyyy-MM-dd"
                dtpGueltig_GI.Value = CDate(dt)
            End Try

            txtDossierCount.Text = LICENSE_DOSSIER_COUNT
            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
                If DT_CLIENT_USER.Rows.Count = 1 Then
                    CLIENT_ID = 0
                Else
                    CLIENT_ID = 99
                End If

            End Try

        End If
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        If CLIENT_ID <> 99 Then
            Dim wrapper As New ClassEncryption("!35452didalog=")
            Dim cipherText As String = wrapper.EncryptData(dtpLicenseAddon.Value.ToString("yyyy-MM-dd"))
            Dim sql = String.Format("SELECT COUNT(GUID) FROM TBDD_LICENSE_ADD_ON WHERE CLIENT_ID = {0} AND MODULE = 'Record-Organizer' AND UPPER(NAME) = UPPER('{1}')", CLIENT_ID, cmbAddOn.Text)
            Dim count = MYDB_ECM.GetScalarValue(SQL)
            If count = 0 Then
                sql = String.Format("INSERT INTO TBDD_LICENSE_ADD_ON (CLIENT_ID, MODULE,NAME,VALID_DATE) VALUES ({0},'Record-Organizer','{1}','{2}')", CLIENT_ID, cmbAddOn.Text, cipherText)
                MYDB_ECM.ExecuteNonQuery(Sql)
            Else
                sql = String.Format("UPDATE TBDD_LICENSE_ADD_ON SET VALID_DATE = '{0}' WHERE CLIENT_ID = {1} AND UPPER(MODULE) = UPPER('Record-Organizer') AND UPPER(NAME) = UPPER('{2}')", cipherText, CLIENT_ID, cmbAddOn.Text)
                MYDB_ECM.ExecuteNonQuery(Sql)
            End If
            Refresh_Licence(False)
        Else
            MsgBox("Please choose a Client!", MsgBoxStyle.Exclamation)
        End If

    End Sub

    Sub GridColor()
        Try
            For i = 0 To DataGridViewAddOns.RowCount - 1
                If DataGridViewAddOns.Rows(i).Cells(2).Value.ToString = "Valid" Then
                    DataGridViewAddOns.Rows(i).DefaultCellStyle.BackColor = Color.LimeGreen
                Else
                    DataGridViewAddOns.Rows(i).DefaultCellStyle.BackColor = Color.Red
                End If
            Next
        Catch ex As Exception

        End Try


    End Sub

    Private Sub grbBoxlicense_Enter(sender As Object, e As EventArgs) Handles grbBoxlicense.Enter

    End Sub
End Class