Option Strict On
Option Explicit On

Imports System.Reflection
Imports IntraSell_DLL

Module FunctionsDemoVersion

    'Demoversion ist solange die Firma 'Ihre Firma' heisst

    Const Demoversion = True
    Const MAX_POSSIBLE_RECHNUNGEN = 1000 'gerenzwert für die Demoversion
    Const MAX_POSSIBLE_KUNDEN = 1000 'gerenzwert für die Demoversion
    Const CONST_TO_LICKEY = "KALAMARATONKOVGRIGOR" 'erweiterung für Licence key ' to avoid collisions for short company names
    Const EXPIRY_DATE As Date = #12/12/2013#


    Sub PruefeLizenz()
        Try
            Dim path As String = IO.Path.GetFileName("IntraSell_RemoteControl.exe")

            If Not IO.File.Exists(path) Then
                Exit Sub
            End If
            Dim ass As Assembly = Assembly.LoadFrom(path)
            Dim FormInstanceType = ass.GetType("IntraSell_RemoteControl.FormLizenzCheck")
            Dim objForm = CType(Activator.CreateInstance(FormInstanceType), Form)
            ' objForm.MdiParent = Main
            objForm.Show()
            AddHandler objForm.FormClosing, AddressOf PluginFormClosed
        Catch ex As Exception
            HandleAppError(ex, Hilfe:="PruefeLizenz", Silent:=True)
        End Try
    End Sub


    Private Sub PluginFormClosed(sender As Object, e As FormClosingEventArgs)
        Application.Exit()
    End Sub


    ''' <summary>
    ''' Prüft ob die Demoversion abgelaufen ist
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Function IsDemoVersionOver() As Boolean
        PruefeLizenz()
        Return False
    End Function

    ''' <summary>
    ''' Checks if the lizKey is correct
    ''' licKey has 8 chars
    ''' arFehRGR ist die code für "Ihre Firma"
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function IslicenceKeyCorrect() As Boolean
        Dim Firma, LicKey As String
        Dim i As Integer

        Firma = VarValue_Default(Variablen.FIRMA, "Ihre Firma")
        LicKey = Trim(VarValue_Default("Lizenzcode", "TEST") & "")

        If Len(LicKey) <> 8 Then
            'Call MsgBox("Die Lizenzcode hat 8 Stellen!", vbCritical)
            Return False
        End If

        Dim lickeyCorrect As String = CONST_TO_LICKEY & Firma

        For i = 1 To 8 'jede 2 te Buchstabe
            If Not Mid(LicKey, i, 1) = Mid(lickeyCorrect, Len(lickeyCorrect) - (i - 1) * 2, 1) Then
                Return False
            End If
        Next i

        If Date.Today > EXPIRY_DATE Then
            Return False
        End If
        Return True
    End Function


    ''' <summary>
    ''' Generiert einen Key nach Firmennamen
    ''' </summary>
    ''' <param name="Firma"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Function KeyGen(Firma As String) As String

        Dim lickeyCorrect As String = CONST_TO_LICKEY & Firma
        Dim licKey As String = ""
        Dim i As Integer

        For i = 1 To 8 'jede 2 te buchstabe
            licKey &= Mid(lickeyCorrect, Len(lickeyCorrect) - (i - 1) * 2, 1)
        Next i
        Debug.Print(licKey)
        KeyGen = licKey
    End Function

End Module
