Imports System.ComponentModel
Imports System.Diagnostics
Imports System.Threading
Imports System.Globalization

Public NotInheritable Class frmSplash

    'TODO: Dieses Formular kann einfach als Begrüßungsbildschirm für die Anwendung festgelegt werden, indem Sie zur Registerkarte "Anwendung"
    '  des Projekt-Designers wechseln (Menü "Projekt", Option "Eigenschaften").
    Private ReadOnly InitSteps As Integer = 5
    Private ReadOnly bw As New BackgroundWorker()

    Private Sub frmSplash_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'Anwendungstitel
        'If My.Application.Info.Title <> "" Then
        '    ApplicationTitle.Text = My.Application.Info.Title
        'Else
        '    'Wenn der Anwendungstitel fehlt, Anwendungsnamen ohne Erweiterung verwenden
        '    ApplicationTitle.Text = IO.Path.GetFileNameWithoutExtension(My.Application.Info.AssemblyName)
        'End If

        Version.Text = String.Format("Version {0}", My.Application.Info.Version.ToString)

        'Copyrightinformationen
        Copyright.Text = My.Application.Info.Copyright & " " & My.Application.Info.CompanyName
        Me.BringToFront()

        Dim RunCount = 0
        For Each p As Process In Process.GetProcesses
            If p.ProcessName.Contains("asyFLOW") Then
                RunCount += 1
            End If
        Next

        InitProgram()
    End Sub

    Private Sub InitProgram()
        bw.WorkerReportsProgress = True
        AddHandler bw.DoWork, AddressOf bw_DoWork
        AddHandler bw.ProgressChanged, AddressOf bw_ProgressChanged
        AddHandler bw.RunWorkerCompleted, AddressOf bw_RunWorkerCompleted

        bw.RunWorkerAsync()
    End Sub

    Private Function CalcProgress(_step As Integer)
        Return _step * (100 / InitSteps)
    End Function

    <STAThread()> _
    Private Sub bw_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs)
        Dim Init = New ClassInit(Me)

        bw.ReportProgress(CalcProgress(1), "Initialize Logging")
        Init.InitLogger()

        Thread.Sleep(300)

        bw.ReportProgress(CalcProgress(2), "Initialize Config")
        Init.InitConfig()

        Thread.Sleep(300)

        bw.ReportProgress(CalcProgress(3), "Initialize Database")
        If Init.InitDatabase() = True Then
            Thread.Sleep(300)
            bw.ReportProgress(CalcProgress(4), "Initialize UserConfiguration")
            Init.InitUserLogin()

            Thread.Sleep(300)

            bw.ReportProgress(CalcProgress(5), "Initialize windream-Settings")
            Init.InitBasics()

            Thread.Sleep(300)
        Else
            Throw New Exception("Database could not be reached! Application will be closed now!")
        End If
    End Sub

    Private Sub bw_ProgressChanged(sender As Object, e As ProgressChangedEventArgs)
        pbStatus.Value = e.ProgressPercentage
        lblStatus.Text = e.UserState.ToString()
    End Sub

    Private Sub bw_RunWorkerCompleted(sender As Object, e As RunWorkerCompletedEventArgs)
        ' Bei Fehler MsgBox anzeigen und Programm beenden
        If e.Error IsNot Nothing Then
            MsgBox(e.Error.Message, MsgBoxStyle.Critical, "Error while Initializing application")
            Application.ExitThread()
        End If

        ' Wenn kein Fehler, Splashscreen schließen
        Close()
    End Sub

    Public Sub New()
       
        ' Dieser Aufruf ist für den Designer erforderlich.
        InitializeComponent()

        ' Fügen Sie Initialisierungen nach dem InitializeComponent()-Aufruf hinzu.

    End Sub
End Class
