Imports System.IO
Imports System.Net

Public Class SplashScreen
    Dim noiamage As String = 0
    Dim ImageFileName As String
    Sub New()
        InitializeComponent()

        Me.labelCopyright.Text = "Copyright © 1998-" & DateTime.Now.Year.ToString()
    End Sub

    Public Overrides Sub ProcessCommand(ByVal cmd As System.Enum, ByVal arg As Object)
        MyBase.ProcessCommand(cmd, arg)
    End Sub

    Public Enum SplashScreenCommand
        SomeCommandId
    End Enum

    Private Sub SplashScreen_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        InitializeDatabaseConnection()
        ' txtConnection.Text = My.Settings.SQL_CONNECTION_STRINGS
        If InitializeDatabaseConnection() = True Then
            'BackgroundWorker1.RunWorkerAsync()
            If BackgroundWorker1.IsBusy Then
                Dim a As String = 0
            ElseIf noiamage = 0 Then

                BackgroundWorker1.RunWorkerAsync()
            End If

            Timer1.Enabled = True
            Timer1.Start()
        Else
            Me.Close()
            frmManagement.Show()
        End If
    End Sub

    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        'Dim noiamage As String = 0


        Try
            peLogo.Image = Image.FromFile(ImageFileName)
            If InitializeDatabaseConnection() = True Then



                progressBarControl.Visible = True
                progressBarControl.Position = progressBarControl.Position + 2
                If (progressBarControl.Position = 10) And noiamage = 1 Then


                    ' peLogo.Image.Save(ImageFileName, System.Drawing.Imaging.ImageFormat.Png)
                    labelStatus.Text = "Loading all images.."
                ElseIf (progressBarControl.Position = 20) Then
                    labelStatus.Text = "Turning on modules."
                ElseIf (progressBarControl.Position = 40) Then
                    labelStatus.Text = "Starting modules.."
                ElseIf (progressBarControl.Position = 60) Then
                    labelStatus.Text = "Loading modules.."
                ElseIf (progressBarControl.Position = 80) Then
                    labelStatus.Text = "Done Loading modules.."
                ElseIf (progressBarControl.Position = 100) Then
                    frmManagement.Show()
                    Timer1.Enabled = False
                    Me.Close()
                End If
            Else
                'ProgressBar1.Visible = True
                'ProgressBar1.Value = ProgressBar1.Value + 2
                'If (ProgressBar1.Value = 10) Then
                '    lblSet.Text = "Reading modules.."
                'ElseIf (ProgressBar1.Value = 20) Then
                '    lblSet.Text = "Turning on modules."
                'ElseIf (ProgressBar1.Value = 40) Then
                '    lblSet.Text = "Starting modules.."
                'ElseIf (ProgressBar1.Value = 60) Then
                '    lblSet.Text = "Loading modules.."
                'ElseIf (ProgressBar1.Value = 80) Then
                '    lblSet.Text = "Done Loading modules.."
                'ElseIf (ProgressBar1.Value = 100) Then
                '    frmSqlServerSetting.Reset()
                '    frmSqlServerSetting.Show()
                '    Timer1.Enabled = False
                '    Me.Hide()
                frmManagement.Show()
                Timer1.Enabled = False
                Me.Close()
            End If

        Catch ex As Exception
            Timer1.Stop()
            ' MsgBox(ex.Message, MsgBoxStyle.Critical, "Error!")

            End
        End Try
    End Sub

    Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork

        Try

            If InitializeDatabaseConnection() = True Then
                Dim DestPath As String = Application.StartupPath + "\Upload\Company\"
                If Not Directory.Exists(DestPath) Then
                    Directory.CreateDirectory(DestPath)
                End If
                Try
                    ExecuteSQLQuery("SELECT * FROM CompanyProfile")
                    If sqlDT.Rows.Count > 0 Then
                        Dim updatedTime As String = 0
                        Dim fileName As String = System.IO.Path.GetFullPath(Application.StartupPath + "\Upload\Company\" + sqlDT.Rows(0)("CompanyLogoFileName"))
                        If System.IO.File.Exists(fileName) Then
                            Dim fi As New IO.FileInfo(fileName)

                            Dim exists As Boolean = fi.Exists

                            ' File ReadOnly ?

                            updatedTime = fi.Length
                        End If

                        If updatedTime = sqlDT.Rows(0)("CompanyLogoFiledetail") Then
                        Else
                            FTPDownloadFile(Application.StartupPath + "\Upload\Company\" + sqlDT.Rows(0)("CompanyLogoFileName"), My.Settings.ftpserver + sqlDT.Rows(0)("CompanyLogoFileName"), My.Settings.ftpuser, My.Settings.ftppassword)

                        End If

                        ImageFileName = DestPath + "" + sqlDT.Rows(0)("CompanyLogoFileName")
                        If noiamage = 0 Then
                            noiamage = 1
                            ' peLogo.Image = Image.FromFile(ImageFileName)
                            ' peLogo.Properties.SizeMode = DevExpress.XtraEditors.Controls.PictureSizeMode.Stretch
                        End If
                        noiamage = 1
                        'arrImage = Application.StartupPath + "\Upload\Company\" + sqlDT.Rows(0)("CompanyLogoFileName")
                        'Dim mstream As New System.IO.MemoryStream(arrImage)
                        'peLogo.Image = Image.FromFile(mstream)


                    End If

                    'peLogo.Image = Image.FromFile(Application.StartupPath + "\Upload\Company\" + sqlDT.Rows(0)("CompanyLogoFileName"))
                    'If sqlDT.Rows.Count > 0 Then
                    '    arrImage = sqlDT.Rows(0)("CompanyLogoFileName")
                    '    Dim mstream As New System.IO.MemoryStream(arrImage)
                    '    peLogo.Image = Image.FromStream(mstream)


                    '    System.IO.File.Delete(DestPath + "\" + "company" + ".png")

                    'End If


                Catch ex As Exception
                    'MsgBox(ex.Message)
                    noiamage = 1

                End Try
            End If

        Catch ex As Exception

            MsgBox(ex.Message, MsgBoxStyle.Critical, "Error!")

            End
        End Try
    End Sub
End Class
