Imports TreeksLicensingLibrary2.EasyIntegration

Public Class Form1

    Private Const MessageBoxTitle As String = "TLL Demo App"

    Dim tlli As TLLInterface = Nothing

    Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles btnInitTLL.Click
        InitiateTLL()
    End Sub

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles btnBrowse.Click
        Dim ofd As New OpenFileDialog
        ofd.Filter = "All files (*.*)|*.*"

        If ofd.ShowDialog = Windows.Forms.DialogResult.OK Then
            txtInitChunkPath.Text = ofd.FileName
        End If
    End Sub


    Private Sub InitiateTLL()
        Try
            Dim chunkbytes() As Byte = IO.File.ReadAllBytes(txtInitChunkPath.Text)
            tlli = New TLLInterface(chunkbytes, txtInitChunkPassword.Text)
            LoadLicenseStatus()
            GroupBox1.Enabled = False
            GroupBox2.Enabled = True
            GroupBox3.Enabled = True
            MessageBox.Show("TLL Interface object successfully initialized.", MessageBoxTitle, MessageBoxButtons.OK, MessageBoxIcon.Information)
        Catch ex As Exception
            MessageBox.Show("Initialization error: " & ex.Message, MessageBoxTitle, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Private Sub LoadLicenseStatus()
        If Not tlli.MyLicense Is Nothing Then
            txtLicStatus.Text = GenerateLicenseDump(tlli.MyLicense)
        Else
            txtLicStatus.Text = "License validation failed: " & tlli.LastVerificationResult
        End If
    End Sub

    Private Function GenerateLicenseDump(lic As TreeksLicensingLibrary2.License) As String
        If lic Is Nothing Then Return "No license is present."

        Dim strLicenseDump As String = "Correct and working license present." & vbNewLine & vbNewLine & "Product: " & lic.ProductName & vbNewLine & "License owner: " & lic.OwnerName

        strLicenseDump &= vbNewLine & "Serial: " & lic.SerialNumber

        strLicenseDump &= vbNewLine & vbNewLine & "License expiration enabled: " & lic.ExpirationDateEnabled

        If lic.ExpirationDateEnabled Then
            strLicenseDump &= vbNewLine & "Expiration date: " & lic.ExpirationDate
            strLicenseDump &= vbNewLine & "Expiration check time server: " & lic.ExpirationCheckTimeServer
            strLicenseDump &= vbNewLine & "Treat offline as expired: " & lic.ExpirationCheckOnlineOnly
        End If

        If lic.HardwareID.Count > 0 Then
            strLicenseDump &= vbNewLine & vbNewLine & "Hardware locking enabled: True"

            strLicenseDump &= vbNewLine & "Allowed hardware IDs: "

            Dim first As Boolean = True
            For Each hwid As String In lic.HardwareID
                If first Then
                    strLicenseDump &= hwid
                    first = False
                Else
                    strLicenseDump &= ", " & hwid
                End If
            Next
        Else
            strLicenseDump &= vbNewLine & vbNewLine & "Hardware locking enabled: False"
        End If


        If lic.AllowedFeatures.Count > 0 Then
            strLicenseDump &= vbNewLine & vbNewLine & "Allowed features: "

            Dim first As Boolean = True
            For Each af As String In lic.AllowedFeatures
                If first Then
                    strLicenseDump &= af
                    first = False
                Else
                    strLicenseDump &= ", " & af
                End If
            Next
        Else
            strLicenseDump &= vbNewLine & vbNewLine & "Allowed features: none"
        End If

        If lic.BlockedFeatures.Count > 0 Then
            strLicenseDump &= vbNewLine & vbNewLine & "Blocked features: "

            Dim first As Boolean = True
            For Each bf As String In lic.BlockedFeatures
                If first Then
                    strLicenseDump &= bf
                    first = False
                Else
                    strLicenseDump &= ", " & bf
                End If
            Next
        Else
            strLicenseDump &= vbNewLine & vbNewLine & "Blocked features: none"
        End If

        strLicenseDump &= vbNewLine & vbNewLine & "Custom fields:"

        strLicenseDump &= vbNewLine & "Info 1: " & lic.Info1
        strLicenseDump &= vbNewLine & "Info 2: " & lic.Info2
        strLicenseDump &= vbNewLine & "Info 3: " & lic.Info3
        strLicenseDump &= vbNewLine & "Info 4: " & lic.Info4
        strLicenseDump &= vbNewLine & "Info 5: " & lic.Info5
        strLicenseDump &= vbNewLine & "Info 6: " & lic.Info6
        strLicenseDump &= vbNewLine & "Info 7: " & lic.Info7
        strLicenseDump &= vbNewLine & "Info 8: " & lic.Info8
        strLicenseDump &= vbNewLine & "Info 9: " & lic.Info9
        strLicenseDump &= vbNewLine & "Info 10: " & lic.Info10

        Return strLicenseDump
    End Function

    Private Sub Button1_Click_1(sender As System.Object, e As System.EventArgs) Handles btnRegister.Click
        Dim strRegistrationResult As String = tlli.Register(txtLicense.Text)

        If strRegistrationResult = "OK" Then
            LoadLicenseStatus()
            MessageBox.Show("License was successfully registered.", MessageBoxTitle, MessageBoxButtons.OK, MessageBoxIcon.Information)
        Else
            MessageBox.Show("Registration failed: " & strRegistrationResult, MessageBoxTitle, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        End If
    End Sub

    Private Sub Button2_Click_1(sender As System.Object, e As System.EventArgs) Handles btnDeleteLicense.Click
        tlli.DeleteLicenseInRegistry()
        LoadLicenseStatus()
        MessageBox.Show("License deleted.", MessageBoxTitle, MessageBoxButtons.OK, MessageBoxIcon.Information)
    End Sub
End Class
