Option Strict On
Option Explicit On

Imports IntraSell_DLL

Public Module FunctionsOnStart

    Public Const VARNAME_BRANCHE = "BRANCHE"
    Public Const POSITION_DEALER As String = "Dealer_Logik_Deaktiviert" '"Dealer"
    Public Const POSITION_ADMINISTRATOR = "Admin"

    ''' <summary>
    ''' hier startet das intrasell programm
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub StartIntraSell()
        SetWorkingFolder()
        'check if DB connected 
        Try
            OpenRecordsetInMemory("select 1 from ofVars")
        Catch ex As Exception
            HandleAppError(ex, "StartIntraSell", Silent:=True)
        End Try
        'end check

        Call BuildCache()
        'check lizenz key
        If Not IslicenceKeyCorrect() Then ' lizenz nicht ok 
            Call IsDemoVersionOver()
        End If 'lizenz ok

        Call ClassIntraSellUpdate.CheckUpdates()
    End Sub

    Public Sub StopIntraSell()
        ClassIntraSellUpdate.StopCheckUpdates()
    End Sub

    Function ConnectionOK() As Boolean
        Try
            FirstRow("select 1 from ofVars")
            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function


    Public Sub CheckBranche()
        Dim BrancheAusgewaelt = ExistsVarValue(Variablen.BRANCHE)

        If Not BrancheAusgewaelt Then  ' not set yet
            'bestimmen Sie Ihre Branche
            Dim branche As BrancheWaehlen = New BrancheWaehlen
            branche.Show()
            branche.BringToFront()
        End If
    End Sub



    Public Sub SetBranche(brancheId As String)
        '        If MsgBox("Wollen Sie die Brancheneinstellungen übernehmen?", vbYesNo) = vbYes Then
        '            Dim rs, sql
        '            DoCmd.SetWarnings(False)


        '            sql = "delete from  [grArtikel-KeyWords]  where ArtKatNr=0"
        '            DoCmd.RunSQL(sql)

        '            sql = "INSERT INTO [grArtikel-KeyWords] ( Name, [Order], enabled, ArtKatNr ) " & _
        '                       " SELECT grBranchenKeyWords.Keyword, 1 , True, 0 " & _
        '                       " FROM grBranchenKeyWords " & _
        '                       " WHERE grBranchenKeyWords.BrNr=" & brancheId

        '            'DoCmd.SetWarnings False
        '            DoCmd.RunSQL(sql)
        '            DoCmd.SetWarnings(True)

        '            Call SETVARVALUE(VARNAME_BRANCHE, brancheId)
        '        End If
    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="autostart"></param>
    ''' <remarks></remarks>
    Sub SetAutostart(autostart As Boolean)

        ' The following code is a rendition of one provided by
        ' Firestarter_75, so he gets the credit here:

        Dim applicationName As String = Application.ProductName
        Dim applicationPath As String = Application.ExecutablePath

        If autostart Then
            Dim regKey As Microsoft.Win32.RegistryKey
            regKey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Run", True)
            regKey.SetValue(applicationName, """" & applicationPath & """")
            regKey.Close()
        Else
            Dim regKey As Microsoft.Win32.RegistryKey
            regKey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Run", True)
            regKey.DeleteValue(applicationName, False)
            regKey.Close()
        End If
    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    Sub SetWorkingFolder()
        WriteLog("Start with paths :" & IO.Directory.GetCurrentDirectory() & "<>" & GetAppPath())
        If IO.Directory.GetCurrentDirectory().ToLower() <> GetAppPath().ToLower() Then
            IO.Directory.SetCurrentDirectory(GetAppPath())
        End If
    End Sub

    Public Sub CheckFirmendaten()

        'Oeffne Einstellungen wenn neue Installation 
        If VarValue_Default(Variablen.FIRMA, "Ihre Firma") = "Ihre Firma" Or
           VarValue_Default(Variablen.FIRMA_NAME, "Ihre Firma") = "Ihre Firma" Or
           VarValue_Default(Variablen.FIRMA_EMAIL, "office@griton.net") = "office@griton.net" Or
           VarValue_Default(Variablen.FIRMA_TEL, "0732 / 123 123") = "0732 / 123 123" Or
           VarValue_Default(Variablen.FIRMA_UID, "ATU0000000") = "ATU0000000" Or
           VarValue_Default(Variablen.Firma_Oeffnungszeiten, "MO-FR 09:00 18:00") = "MO-FR 09:00 18:00" Then

            Vars.SetTitle("Kontrollieren Sie Ihre Firmendaten!")
            Vars.Show()
            Vars.Filter("Firma")
            Vars.BringToFront()
            'Vars.TopMost = True
        End If
    End Sub

End Module
