Imports System.Reflection
Imports IntraSell_DLL
Imports IntraSell_Net_Base
Imports IntraSell_Net_Starter
Imports MySql.Data.MySqlClient

Public Class Main

#Region "Main"

    Event LoginShowDialog(ByRef Login As Login)

    ''' <summary>
    ''' Listener für Aufträge von Schnittstellen 
    ''' </summary>
    ''' <remarks></remarks>
    Private AuftragListener As ImportAuftragListener

    ''' <summary>
    ''' Listener für Küchen Aufträge
    ''' </summary>
    Private KuechenAuftragListener As KuechenAuftragListener

    ''' <summary>
    ''' Listener für Reminder
    ''' </summary>
    Private ReminderListener As ReminderListener

    ''' <summary>
    ''' Listener für Auftrag status wechsel
    ''' </summary>
    Private AuftragStatusListener As AuftragStatusListener

    ''' <summary>
    ''' Listener für sonstige IntraSell commandos
    ''' </summary>
    Private IntraSellCommandListener As IntraSellCommandListener


    ''' <summary>
    ''' Flag
    ''' </summary>
    ''' <remarks></remarks>
    Private startDone As Boolean = False
    Private thread1_Dashboard As Boolean = False
    Private thread2_Auftraege As Boolean = False
    Private thread3_Plugins As Boolean = False

    Public Function AllThreadsDone() As Boolean
        Return startDone AndAlso thread1_Dashboard AndAlso thread2_Auftraege AndAlso thread3_Plugins
    End Function

    Public Sub RunAllTimers()
        Startup()
        TimerAfterStart_Tick(Nothing, Nothing)
        TimerAuftraege_Tick(Nothing, Nothing)
        TimerLadePlugins_Tick(Nothing, Nothing)
    End Sub

    ''' <summary>
    ''' Start Main Form
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub Main_Load(sender As Object, e As System.EventArgs) Handles MyBase.Load
        ApplyStyle()

        Cursor = Cursors.WaitCursor
        Dim t As New Threading.Thread(AddressOf Startup)
        t.Start()
        Timer1AfterStart.Enabled = True
        Timer2Auftraege.Enabled = True
        Timer3LadePlugins.Enabled = True
        'Gehe in modus 1024 
        'X800ToolStripMenuItem_Click(Nothing, Nothing)
        'Maximize 
        FullscreenToolStripMenuItem_Click(Nothing, Nothing)
        Try
            If Assembly.GetEntryAssembly() IsNot Nothing Then
                Text += ", Assembly: " & Assembly.GetEntryAssembly().GetName().Version.ToString()
            End If
        Catch ex As Exception
            ' Ignore
        End Try

        AddHandler ModuleGlobals.MitarbeiterIDChanged, AddressOf ModuleGlobals_MitarbeiterIDChanged

    End Sub

    Sub ApplyStyle()
        Try
            ModuleStyle.StyleMenu(Me.MenuStrip1)
        Catch ex As Exception
            ' Ignore
        End Try
    End Sub


    Private Sub TimerAfterStart_Tick(sender As Object, e As System.EventArgs) Handles Timer1AfterStart.Tick
        StartUp_Step1()
    End Sub

    Private Sub TimerAuftraege_Tick(sender As Object, e As EventArgs) Handles Timer2Auftraege.Tick
        If FunctionsDB.CurrentDB.State = ConnectionState.Open Then
            Try
                If Not CBool(VarValue_Default("AUFTRAGLISTENER_AKTIV", "true")) Then
                    Timer2Auftraege.Enabled = False
                    Return
                End If
            Catch ex As Exception
                HandleAppError(ex, Silent:=True)
            End Try

            StartUp_Step2()
        End If
    End Sub

    Private Sub TimerLadePlugins_Tick(sender As Object, e As EventArgs) Handles Timer3LadePlugins.Tick
        StartUp_Step3()
    End Sub

    ''' <summary>
    ''' Wird im eigenen Thread ausgeführt
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub Startup()
        Try
            Try
                IntraSell_DLL.Init(My.MySettings.Default.LOG_FILENAME)

                WriteLog("Start IntraSell")
                'Init Connection 
                CurrentDB = New MySqlConnection(My.MySettings.Default.intrasell_daten_2_ConnectionString)
                CurrentDB.Open()

            Catch ex As Exception
                If Not StartMySQLServer() Then
                    HandleAppError(ex)
                End If
            End Try

            Try
                SetConnection(connString:=My.Settings.intrasell_daten_2_ConnectionString)
            Catch ex As Exception
                HandleAppError(ex)
            End Try
            Try
                'Process SQL files 
                Dim UpdateDb = New ClassDBUpgrade
                UpdateDb.ProcessAllSQLFiles()

                'Start Module 
                StartIntraSell()
            Catch ex As Exception
                WriteLog("Start IntraSell mit Error " + ex.Message)
            End Try

            Try
                ModuleIntraSellEF.InitContext()
            Catch ex As Exception
                WriteLog("Start IntraSell EF Context mit Error " + ex.Message)
            End Try


            startDone = True
        Catch ex As Exception
            'Fataler Error, kann NICHT Starten 
            HandleAppError(ex, Silent:=False)
        End Try

    End Sub

    Public Sub StartUp_Step1()
        Try
            If startDone Then
                Cursor = Cursors.Default
                If FunctionsDB.CurrentDB.State = ConnectionState.Open Then
                    Timer1AfterStart.Enabled = False

                    SpracheFestlegen(VarValue_Default("LANGUAGE", "DEU"), True)

                    StartListeners()

                    RaiseEvent LoginShowDialog(Login)
                    'Login
                    If NeedsLogin() Then
                        Login.ShowDialog()
                    End If

                    If NeedsLogin And MitarbeiterID <= 0 Then
                        Close()
                        Exit Sub
                    End If

                    StartDashboard()

                    FunctionsOnStart.CheckFirmendaten()
                    FunctionsOnStart.CheckBranche()

                    thread1_Dashboard = True
                End If
            End If
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub

    Private Sub StartListeners()
        AuftragListener = New ImportAuftragListener
        AuftragListener.Init()

        KuechenAuftragListener = New KuechenAuftragListener
        KuechenAuftragListener.Init()

        ReminderListener = New ReminderListener
        ReminderListener.Init()

        AuftragStatusListener = New AuftragStatusListener
        AuftragStatusListener.Init()

        IntraSellCommandListener = New IntraSellCommandListener
        IntraSellCommandListener.Init()


    End Sub

    Sub StartDashboard()
        'Start Dashboard 
        Dashboard.MdiParent = Me
        Dashboard.Show()
        Dashboard.Focus()
        Dashboard.WindowState = FormWindowState.Minimized
        Dashboard.WindowState = FormWindowState.Maximized
    End Sub

    Public Sub StartUp_Step2()
        If AuftragListener IsNot Nothing Then
            AuftragListener.Run()
        End If
        thread2_Auftraege = True
    End Sub

    Public Sub StartUp_Step3()
        Try
            If startDone Then
                Timer3LadePlugins.Enabled = False
                Dashboard.LoadPlugins()
                thread3_Plugins = True
            End If
        Catch ex As Exception
            HandleAppError(ex, Silent:=True)
        End Try
    End Sub

    Private Sub Main_Close(sender As Object, e As System.EventArgs) Handles MyBase.FormClosed
        Try
            StopIntraSell()
        Catch ex As Exception
            HandleAppError(ex, Silent:=True)
        End Try
    End Sub

    Function IsImplementedFrom(objectType As Type, intefaceType As Type) As Boolean
        For Each thisInterface As Type In objectType.GetInterfaces
            If thisInterface Is intefaceType Then
                Return True
            End If
        Next
        Return False
    End Function

#End Region

#Region "Kunden"
    Protected Friend Sub NeuerKundeToolStripMenuItem_Click(sender As Object, e As System.EventArgs) Handles NeuerKundeToolStripMenuItem.Click
        OeffneKundenMaske(True)
    End Sub

    Private Sub KundenToolStripMenuItem1_Click(sender As Object, e As System.EventArgs) Handles KundenToolStripMenuItem1.Click
        OeffneKundenMaske(False)
    End Sub

    Protected Friend Sub KundenlisteToolStripMenuItem_Click(sender As Object, e As System.EventArgs) Handles KundenlisteToolStripMenuItem.Click
        OeffneKundenlisteMaske()
    End Sub

    Public Sub KundengruppenToolStripMenuItem_Click(sender As Object, e As System.EventArgs) Handles KundengruppenToolStripMenuItem.Click
        Try
            Dim f As New KundenGruppe With {
                .MdiParent = Me
            }
            f.Show()
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub

#End Region

#Region "Artikel"

    Protected Friend Sub ArtikelToolStripMenuItem_Click(sender As Object, e As System.EventArgs) Handles ArtikelToolStripMenuItem.Click
        OeffneArtikelMaskeNoFilter()
    End Sub

    Protected Friend Sub ArtikelQuickToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ArtikelQuickToolStripMenuItem.Click
        OeffneArtikelMaskeQuickNoFilter()
    End Sub

    Protected Friend Sub ArtikellisteToolStripMenuItem_Click(sender As Object, e As System.EventArgs) Handles ArtikellisteToolStripMenuItem.Click
        OeffneArtikelListe()
    End Sub

    Private Sub ArtikelEinheitenToolStripMenuItem_Click(sender As Object, e As System.EventArgs) Handles ArtikelEinheitenToolStripMenuItem.Click
        Try
            Einheiten.MdiParent = Me
            Einheiten.Show()
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub

#End Region

#Region "Vorgang"

    Protected Friend Sub NeuerVorgangToolStripMenuItem_Click(sender As Object, e As System.EventArgs) Handles NeuerVorgangToolStripMenuItem.Click
        BeginNewVorgang(Nothing)
    End Sub

    Protected Friend Sub AuftragErfassenToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles AuftragErfassenToolStripMenuItem.Click
        Try
            FormManager.OeffneTelefonischeBestellung
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub

    Private Sub VorgangToolStripMenuItem_Click(sender As Object, e As System.EventArgs) Handles VorgangToolStripMenuItem.Click
        Try
            Vorgang.MdiParent = Me
            Vorgang.Show()
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub

    Protected Friend Sub VorganglisteToolStripMenuItem_Click(sender As Object, e As System.EventArgs) Handles VorganglisteToolStripMenuItem.Click
        OeffneVorgangListe()
    End Sub

    Private Sub VerkaufstatistikToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles VerkaufstatistikToolStripMenuItem.Click
        Try
            VorgangStat.MdiParent = Me
            VorgangStat.Show()
            VorgangStat.BringToFront()
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub

    Private Sub VorgangEinstellungenToolStripMenuItem1_Click_1(sender As Object, e As System.EventArgs) Handles VorgangEinstellungenToolStripMenuItem1.Click
        Try
            VorgangEinstellungen.MdiParent = Me
            VorgangEinstellungen.Show()
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub

    Private Sub WorkflowToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles WorkflowToolStripMenuItem.Click
        Try
            VorgangEinstellungen.MdiParent = Me
            VorgangEinstellungen.Show()
            VorgangEinstellungen.Filter(VorgangTypen.AU)
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub

    Private Sub VorgangEinstellungenToolStripMenuItem_Click(sender As Object, e As System.EventArgs) Handles VorgangEinstellungenToolStripMenuItem.Click
        Try
            VorgangBedingungen.MdiParent = Me
            VorgangBedingungen.Show()
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub


    Protected Friend Sub MahnungenToolStripMenuItem_Click(sender As Object, e As System.EventArgs) Handles MahnungenToolStripMenuItem.Click
        Try
            MahnListe.MdiParent = Me
            MahnListe.Show()
            MahnListe.BringToFront()
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub

    Public Sub VorgangBarToolStripMenuItem_Click(sender As Object, e As System.EventArgs) Handles VorgangBarToolStripMenuItem.Click
        ShowHandelskassa()
    End Sub

#End Region

#Region "Infrastructure"

    Private Sub ÜberIntraSellToolStripMenuItem_Click(sender As Object, e As System.EventArgs) Handles ÜberIntraSellToolStripMenuItem.Click
        Try
            About.MdiParent = Me
            About.Show()
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub

    Private Sub BeendenToolStripMenuItem_Click(sender As Object, e As System.EventArgs) Handles BeendenToolStripMenuItem.Click
        Try
            WriteLog("Close IntraSell")
            Close()
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub

    Private Sub SetupToolStripMenuItem_Click(sender As Object, e As System.EventArgs) Handles SetupToolStripMenuItem.Click
        Try
            Log.Show()
            Setup.Show()
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub

    Private Sub EintellungenToolStripMenuItem_Click(sender As Object, e As System.EventArgs) Handles EintellungenToolStripMenuItem.Click
        Try
            Vars.MdiParent = Me
            Vars.Show()
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub

    Private Sub UpdateToolStripMenuItem_Click(sender As Object, e As System.EventArgs) Handles UpdateToolStripMenuItem.Click
        Dim l As New Log
        Try
            l.Show()
            Dim Update = New ClassUpdate
            Update.UpdateIntraSell(False)

            Dim UpdateDb = New ClassDBUpgrade
            UpdateDb.ProcessAllSQLFiles()

            'Kundenspezifische Updates 

            Dim kundenUpdateUrl = Update.GetCustomerUpdateUrl(VarValue(Variablen.FIRMA))
            WriteLog("Prüfe Kundenspezifische updates von " & kundenUpdateUrl)
            Update.UpdateIntraSell(False, kundenUpdateUrl)

        Catch ex As Exception
            HandleAppError(ex)
        Finally
            Try
                If Not l.IsDisposed Then
                    l.Close()
                End If
            Catch ex As Exception
            End Try
        End Try
    End Sub


    Private Sub InstallFileToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles InstallFileToolStripMenuItem.Click
        Try
            Log.MdiParent = Me
            Log.Show()
            Dim Update = New ClassUpdate
            Update.UpdateIntraSellVonDatei(False)
            Dim UpdateDb = New ClassDBUpgrade
            UpdateDb.ProcessAllSQLFiles()
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub

    Private Sub LogToolStripMenuItem_Click(sender As Object, e As System.EventArgs) Handles LogToolStripMenuItem.Click
        Try
            'Log.MdiParent = Me
            Log.Show()
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub

    Private Sub WikiToolStripMenuItem_Click(sender As Object, e As System.EventArgs) Handles WikiToolStripMenuItem.Click
        Try
            Process.Start("http://www.griton.net/wiki")
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub


    Private Sub BugMeldenToolStripMenuItem_Click(sender As Object, e As System.EventArgs) Handles BugMeldenToolStripMenuItem.Click
        Try
            Process.Start("http://www.griton.net/mantis")
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub

#End Region

#Region "Drucken & Export"

    Private Sub Main_MdiChildActivate(sender As Object, e As System.EventArgs) Handles Me.MdiChildActivate

        If ActiveMdiChild Is Nothing Then
            DruckenToolStripMenuItem.Enabled = False
            ExportToolStripMenuItem.Enabled = False
        Else
            DruckenToolStripMenuItem.Enabled = IsImplementedFrom(ActiveMdiChild.GetType, GetType(InterfacePrintable))
            ExportToolStripMenuItem.Enabled = IsImplementedFrom(ActiveMdiChild.GetType, GetType(InterfaceExportableGrid))
        End If

    End Sub


    Private Sub DruckenToolStripMenuItem_Click(sender As Object, e As System.EventArgs) Handles DruckenToolStripMenuItem.Click
        Try
            If ActiveMdiChild IsNot Nothing Then
                'Print the form in focus 
                Dim printable As InterfacePrintable = ActiveMdiChild
                printable.Print(Me)
            Else
                DruckenToolStripMenuItem.Enabled = False
            End If

        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub

    Private Sub ExportToolStripMenuItem_Click(sender As Object, e As System.EventArgs) Handles ExportToolStripMenuItem.Click
        Try
            If ActiveMdiChild IsNot Nothing Then
                'Print the form in focus 
                Dim printable As InterfaceExportableGrid = ActiveMdiChild
                printable.Export(Me)
            Else
                ExportToolStripMenuItem.Enabled = False
            End If

        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub



#End Region

#Region "Preise"

    Private Sub PreisregelnToolStripMenuItem_Click(sender As Object, e As System.EventArgs) Handles PreisregelnToolStripMenuItem.Click
        Try
            ArtikelPreisregeln.MdiParent = Me
            ArtikelPreisregeln.Show()
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub

    Private Sub KundenpreiseToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles KundenpreiseToolStripMenuItem.Click
        OeffneKundenPreise()
    End Sub

    Private Sub ZustellpreiseToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ZustellpreiseToolStripMenuItem.Click
        OeffneKundenPreise(Preisliste:=Transportmethoden.Zustellung)
    End Sub

    Private Sub AbholpreiseToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles AbholpreiseToolStripMenuItem.Click
        OeffneKundenPreise(Preisliste:=Transportmethoden.Abholung)
    End Sub


    Private Sub KundenMengenpreiseToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles KundenMengenpreiseToolStripMenuItem.Click
        OeffneKundenMengenPreise()
    End Sub

    Private Sub PreisKalkulatorToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles PreisKalkulatorToolStripMenuItem.Click
        OeffnePreisKalkulator()
    End Sub

#End Region

#Region "Anrufe"


    Protected Friend Sub NeuerAnrufToolStripMenuItem_Click(sender As Object, e As System.EventArgs) Handles NeuerAnrufToolStripMenuItem.Click
        OeffneAnrufNeu(KundenIDNR)
    End Sub

    Protected Friend Sub ListeToolStripMenuItem1_Click(sender As Object, e As System.EventArgs) Handles ListeToolStripMenuItem1.Click
        FormManager.OeffneAnrufliste(-1)
    End Sub

    Private Sub AnrufToolStripMenuItem_Click(sender As Object, e As System.EventArgs) Handles AnrufToolStripMenuItem.Click
        Try
            Anruf.MdiParent = Me
            Anruf.Show()
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub
#End Region

#Region "Branche"


    Private Sub MitarbeiterToolStripMenuItem_Click(sender As Object, e As System.EventArgs) Handles MitarbeiterToolStripMenuItem.Click
        Try
            Mitarbeiter.MdiParent = Me
            Mitarbeiter.Show()
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub

    Private Sub BranchenToolStripMenuItem_Click(sender As Object, e As System.EventArgs) Handles BranchenToolStripMenuItem.Click
        Try
            Branche.MdiParent = Me
            Branche.Show()
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub
#End Region

#Region "Stammdaten"


    Private Sub ArtikelKategorienToolStripMenuItem_Click(sender As Object, e As System.EventArgs) Handles ArtikelKategorienToolStripMenuItem.Click
        Try
            ArtikelKategorien.MdiParent = Me
            ArtikelKategorien.Show()
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub

    Private Sub LänderToolStripMenuItem_Click(sender As Object, e As System.EventArgs) Handles LänderToolStripMenuItem.Click
        Try
            Land.MdiParent = Me
            Land.Show()
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub


    Private Sub PLZToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles PLZToolStripMenuItem.Click
        Try
            PLZ.MdiParent = Me
            PLZ.Show()
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub

    Private Sub MwStToolStripMenuItem_Click(sender As Object, e As System.EventArgs) Handles MwStToolStripMenuItem.Click
        Try
            MwSt.MdiParent = Me
            MwSt.Show()
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub

    Private Sub BankenToolStripMenuItem_Click(sender As Object, e As System.EventArgs) Handles BankenToolStripMenuItem.Click
        Try
            Bank.MdiParent = Me
            Bank.Show()
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub



#End Region

#Region "Dashboard"
    Public Shared Function GetDashboard() As Dashboard
        Return Dashboard
    End Function

    Public Sub DashboardToolStripMenuItem_Click(sender As Object, e As System.EventArgs) Handles DashboardToolStripMenuItem.Click, DashboardToolStripMenuItem1.Click
    End Sub

    Private Sub ProgrammDashboardToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ProgrammDashboardToolStripMenuItem.Click
        Try
            Dashboard.MdiParent = Me
            Dashboard.WindowState = FormWindowState.Maximized
            Dashboard.Show()
            Dashboard.BringToFront()
            Dashboard.Focus()
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub
#End Region

#Region "Tools"
    Private Sub MagentoSyncToolStripMenuItem_Click(sender As Object, e As System.EventArgs)
        Try
            Dim path As String = "MagentoSync.exe"
            Dim ass As Assembly = Assembly.LoadFrom(path)
            Dim FormInstanceType = ass.GetType("MagentoSync.FormStart")
            Dim objForm = CType(Activator.CreateInstance(FormInstanceType), Form)
            objForm.MdiParent = Me
            objForm.Show()
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub

    Private Sub TConnectSyncToolStripMenuItem_Click(sender As Object, e As EventArgs)
        Try
            Dim path As String = "TConnectSync.exe"
            Dim ass As Assembly = Assembly.LoadFrom(path)
            Dim FormInstanceType = ass.GetType("TConnectSync.FormStart")
            Dim objForm = CType(Activator.CreateInstance(FormInstanceType), Form)
            objForm.MdiParent = Me
            objForm.Show()
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub

    Private Sub TennisTimeToolStripMenuItem_Click(sender As Object, e As System.EventArgs)
        Try
            Dim path As String = "IntraSell_Net_AddIn_Dienst.exe"
            Dim ass As Assembly = Assembly.LoadFrom(path)
            Dim FormInstanceType = ass.GetType("IntraSell_Net_AddIn_Dienst.DiensterfassungListe")
            Dim objForm = CType(Activator.CreateInstance(FormInstanceType), Form)
            objForm.MdiParent = Me
            objForm.Show()
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub
#End Region

#Region "Kassa"
    Private Sub KassabuchToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles KassabuchToolStripMenuItem.Click
        Try
            If Not HasRecht(MitarbeiterID, Rechte.Kassabuch, True) Then
                Exit Sub
            End If
            KassaListe.MdiParent = Me
            KassaListe.Show()
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub
#End Region


#Region "Lieferanten"
    Private Sub LieferantenToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles LieferantenToolStripMenuItem.Click
        Try
            Lieferant.MdiParent = Me
            Lieferant.Show()
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub
#End Region


    Private Sub ToolStripComboBoxSprache_TextChanged(sender As Object, e As EventArgs) Handles ToolStripComboBoxSprache.TextChanged

        Dim neueSprache = ToolStripComboBoxSprache.Text
        Try
            SpracheFestlegen(neueSprache, True)
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub

    Private Sub SpracheFestlegen(neueSprache As String, updateCombo As Boolean)
        If updateCombo Then
            ToolStripComboBoxSprache.Text = neueSprache
        End If

        ModuleSetup.SpracheFestlegen(neueSprache)
    End Sub

    Private Sub HilfeToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles HilfeToolStripMenuItem.Click

    End Sub

    Private Sub X800ToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles X800ToolStripMenuItem.Click
        Width = 1024
        Height = 800
    End Sub

    Private Sub X1024ToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles X1024ToolStripMenuItem.Click
        Width = 1280
        Height = 1024
    End Sub

    Private Sub FullscreenToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles FullscreenToolStripMenuItem.Click
        WindowState = FormWindowState.Maximized
        For Each c In MdiChildren
            c.WindowState = FormWindowState.Maximized
        Next
    End Sub

    Private Sub ImportToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ImportToolStripMenuItem.Click
        Try
            Dim path As String = "PlugIns\IntraSell_Net_ImpEx.exe"
            Dim ass As Assembly = Assembly.LoadFrom(path)
            Dim FormInstanceType = ass.GetType("IntraSell_Net_ImpEx.FormImportArtikel")
            Dim objForm = CType(Activator.CreateInstance(FormInstanceType), Form)
            objForm.MdiParent = Me
            objForm.Show()
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub

    Public Sub AddPlugin(pluginMenuItem As ToolStripMenuItem)
        Try
            PlugInsToolStripMenuItem.ForeColor = Color.Green
            PlugInsToolStripMenuItem.DropDownItems.Add(pluginMenuItem)
        Catch ex As Exception
            HandleAppError(ex, Silent:=True)
        End Try
    End Sub

    Private Sub IntraSellInstallationToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles IntraSellInstallationToolStripMenuItem.Click

    End Sub


    Public Sub HideMenu(BARRECHNUNG_RESTAURANT_KASSA_MAIN_MENU_VERSTECKT As Boolean)
        Try
            MenuStrip1.Visible = Not BARRECHNUNG_RESTAURANT_KASSA_MAIN_MENU_VERSTECKT
        Catch ex As Exception
            HandleAppError(ex, Silent:=True)
        End Try
    End Sub

    Private Sub CleanToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles CleanToolStripMenuItem.Click
        ClassCleanUp.CleanUp()
    End Sub

    Private Sub AlteDatenLöschenToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles AlteDatenLöschenToolStripMenuItem.Click
        ClassCleanUp.CleanUp()
    End Sub

    Private Sub TelefonischeBestellungToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles TelefonischeBestellungToolStripMenuItem.Click
        AuftragErfassenToolStripMenuItem_Click(sender, e)
    End Sub

    Private Sub KassabuchungToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles KassabuchungToolStripMenuItem.Click
        Try
            KassaBewegung.MdiParent = Me
            KassaBewegung.Show()
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub

    Private Sub ToolStripComboBoxSprache_Click(sender As Object, e As EventArgs) Handles ToolStripComboBoxSprache.Click

    End Sub

    Private Sub RunSQLToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles RunSQLToolStripMenuItem.Click
        Try
            FormSQL.MdiParent = Me
            FormSQL.Show()
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub



    Public Sub ToolTipDashboard(show As Boolean)
        If show Then
            DashboardToolStripMenuItem1.ForeColor = Color.Green
            'DashboardToolStripMenuItem1.BackColor = Color.White
        Else
            DashboardToolStripMenuItem1.ForeColor = Color.Black
        End If
    End Sub


    Private Sub TouchToolStripMenuItem_Click(sender As Object, e As EventArgs)

    End Sub

    Private Sub DatenArchivierenToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles DatenArchivierenToolStripMenuItem.Click
        Try
            Archivierung.MdiParent = Me
            Archivierung.Show()
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub

    Private Sub AutostartToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles AutostartToolStripMenuItem.Click
        Try
            Vars.SetTitle("Autostart für Plugins definieren.")
            Vars.Show()
            Vars.Filter(AUTOSTART)
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub

    Private Sub ToolStripMenuItemSucheAU_Click(sender As Object, e As EventArgs) Handles ToolStripMenuItemSucheAU.Click
        SucheNachNummer(VorgangTypen.AU, "Auftrag")
    End Sub

    Private Sub ToolStripMenuItemSucheAR_Click(sender As Object, e As EventArgs) Handles ToolStripMenuItemSucheAR.Click
        SucheNachNummer(VorgangTypen.AR, "Rechnung")
    End Sub

    Shared Function GetMain() As Main
        Return Main
    End Function

    Shared Function GetVars() As Vars
        Return Vars
    End Function

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub SucheToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles SucheToolStripMenuItem.Click
        Try
            Suche.Show()
        Catch ex As Exception
            HandleAppError(ex, Silent:=True)
        End Try
    End Sub

    Public Sub SpracheToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles SpracheToolStripMenuItem.Click
        AddHandler Language.UcLanguage1.SpracheGewechelt, AddressOf SpracheGewechelt
        Language.ShowDialog()
    End Sub

    Private Sub SpracheGewechelt(lang As String)
        Try
            'Controls.Clear()
            'InitializeComponent()
            Dashboard.OnLanguageChange()
        Catch ex As Exception
            HandleAppError(ex, Silent:=True)
        End Try
    End Sub

    ''' <summary>
    ''' Use it in UI Tests to allow Timers to be executed.
    ''' By Pressing Log Exit button the Test will finish 
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function LogShowDialog(closeInSeconds As Integer) As Log
        Log.TimerAutoclose.Interval = closeInSeconds * 1000
        Log.TimerAutoclose.Enabled = True
        Log.ShowDialog()
        Return Log
    End Function

    Private Sub MandantenToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles MandantenToolStripMenuItem.Click
        Try
            Mandant.MdiParent = Me
            Mandant.Show()
        Catch ex As Exception
            HandleAppError(ex, Silent:=True)
        End Try

    End Sub

    Private Sub ZustellerPanelToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ZustellerPanelToolStripMenuItem.Click
        OeffneZustellerPanel()
    End Sub

    Private Sub KüchenDisplayToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles KüchenDisplayToolStripMenuItem.Click
        OeffneKuechenDisplay()
    End Sub

    Private Sub ToolStripMenuItemNeueFeatures_Click(sender As Object, e As EventArgs) Handles ToolStripMenuItemNeueFeatures.Click
        Process.Start("http://www.griton.net/index.php/produkte/intrasell-features")
    End Sub

#Region "Sicherung"
    Private Sub OnlineDatenSichernToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles OnlineDatenSichernToolStripMenuItem.Click
        OnlineSicherung()
    End Sub

    Private Sub AufUSBDatenSichernToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles AufUSBDatenSichernToolStripMenuItem.Click
        OfflineSicherungErstellen()
    End Sub

    Private Sub VonUSBDatenWiederherstellenToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles VonUSBDatenWiederherstellenToolStripMenuItem.Click
        OfflineSicherungWiederherstellen()
    End Sub

    Private Sub SMSEmailReminderToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles SMSEmailReminderToolStripMenuItem.Click
        FormManager.OpenNachrichtEinstellungen()
    End Sub

    Private Sub GastroKonfigurationToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles BrancheKonfigurationToolStripMenuItem.Click
        BrancheWaehlen.Show()
    End Sub

#End Region

    Private Sub RechteSetzen()
        Me.EintellungenToolStripMenuItem.Visible = HasRecht(MitarbeiterID, Rechte.Administrator)
        Me.StammdatenToolStripMenuItem.Visible = HasRecht(MitarbeiterID, Rechte.Administrator)
        Me.VorgängeToolStripMenuItem.Visible = HasRecht(MitarbeiterID, Rechte.Administrator)
        'Me.LagerToolStripMenuItem.Visible = HasRecht(MitarbeiterID, Rechte.Administrator)
        Me.AnrufeToolStripMenuItem.Visible = HasRecht(MitarbeiterID, Rechte.Anrufe)
        Me.ToolsToolStripMenuItem.Visible = HasRecht(MitarbeiterID, Rechte.Administrator)
        Me.PlugInsToolStripMenuItem.Visible = HasRecht(MitarbeiterID, Rechte.Plugins)
        Me.PreiseToolStripMenuItem.Visible = HasRecht(MitarbeiterID, Rechte.Preise)
    End Sub


    Private Sub ModuleGlobals_MitarbeiterIDChanged(MitarbeiterId As Integer)
        RechteSetzen()
    End Sub

    Private Sub InstallationsverzeichnisToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles InstallationsverzeichnisToolStripMenuItem.Click
        Try  'Oeffne Folder 
            Dim folder = AppFolder()
            Process.Start("explorer.exe", folder)
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub

    Private Sub EasyToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles EasyToolStripMenuItem.Click
        Try
            VorgangEasy.Show()
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub

    Private Sub AutomatischeAufgabenToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles AutomatischeAufgabenToolStripMenuItem.Click
        Try
            AutomAufgabenEinstellungen.Show()
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub

    Private Sub Timer4_OfflineTäglicheSicherung_Tick(sender As Object, e As EventArgs) Handles Timer4_OfflineTäglicheSicherung.Tick
        Try
            If ModuleOnlineSicherung.OfflineSicherungTäglichErstellen() Then
                Timer4_OfflineTäglicheSicherung.Enabled = False
            End If
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub
End Class
