Imports System.Net
Imports System.ComponentModel

Imports Stax
Imports Stax.UI

Public Class DownloadForm
    Inherits ExtendedForm

#Region " Designer "

    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    Private components As System.ComponentModel.IContainer

    Friend WithEvents ProgressBar As System.Windows.Forms.ProgressBar
    Friend WithEvents bCancel As System.Windows.Forms.Button
    Friend WithEvents lStatus As System.Windows.Forms.Label
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.ProgressBar = New System.Windows.Forms.ProgressBar
        Me.bCancel = New System.Windows.Forms.Button
        Me.lStatus = New System.Windows.Forms.Label
        Me.SuspendLayout()
        '
        'ProgressBar
        '
        Me.ProgressBar.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.ProgressBar.Location = New System.Drawing.Point(13, 7)
        Me.ProgressBar.Name = "ProgressBar"
        Me.ProgressBar.Size = New System.Drawing.Size(311, 16)
        Me.ProgressBar.TabIndex = 0
        '
        'bCancel
        '
        Me.bCancel.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.bCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.bCancel.Location = New System.Drawing.Point(249, 28)
        Me.bCancel.Name = "bCancel"
        Me.bCancel.Size = New System.Drawing.Size(75, 24)
        Me.bCancel.TabIndex = 1
        Me.bCancel.Text = "Cancel"
        '
        'lStatus
        '
        Me.lStatus.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.lStatus.Location = New System.Drawing.Point(12, 34)
        Me.lStatus.Name = "lStatus"
        Me.lStatus.Size = New System.Drawing.Size(232, 16)
        Me.lStatus.TabIndex = 2
        '
        'DownloadForm
        '
        Me.CancelButton = Me.bCancel
        Me.ClientSize = New System.Drawing.Size(336, 64)
        Me.ControlBox = False
        Me.Controls.Add(Me.lStatus)
        Me.Controls.Add(Me.bCancel)
        Me.Controls.Add(Me.ProgressBar)
        Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog
        Me.MaximizeBox = False
        Me.MinimizeBox = False
        Me.Name = "DownloadForm"
        Me.ShowInTaskbar = False
        Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private Package As Package
    Private FileSize As Integer
    Private Done As Boolean

    WithEvents WebClient As New WebClient

    Public Sub New(ByVal package As Package)
        MyBase.New()
        InitializeComponent()
        Me.Package = package
    End Sub

    Private Sub DownloadForm_ActivatedFirstTime() Handles Me.ActivatedFirstTime
        Dim filename As String = StringHelp.GetRightLast(Package.DownLoadURL, "/")
        Text = filename

        Try
            WebClient.DownloadFileAsync(New Uri(Package.DownLoadURL), Paths.DownloadDir + filename)
        Catch ex As Exception
            If Msg.Show("Download failed, try again?", MsgIcon.Error, MessageBoxButtons.OKCancel) = Windows.Forms.DialogResult.OK Then
                Try
                    WebClient.DownloadFileAsync(New Uri(Package.DownLoadURL), Paths.DownloadDir + filename)
                Catch ex2 As Exception
                    Msg.Show("Download failed again. Please download and install the application manually.", MsgIcon.Error)
                    Close()
                End Try
            Else
                Close()
            End If
        End Try
    End Sub

    Private Function GetMBString(ByVal value As Long) As String
        Return (value / 1024 / 1024).ToString("f1") + " MB"
    End Function

    Private Sub bCancel_Click(ByVal sender As Object, ByVal e As EventArgs) Handles bCancel.Click
        WebClient.CancelAsync()
        Close()
    End Sub

    Private Sub WebClient_DownloadFileCompleted(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs) Handles WebClient.DownloadFileCompleted
        If Done Then
            Package.Install()
            Close()
        End If
    End Sub

    Private Sub WebClient_DownloadProgressChanged(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) Handles WebClient.DownloadProgressChanged
        If e.TotalBytesToReceive > 0 Then 'can be -1 and thus crash
            ProgressBar.Maximum = CInt(e.TotalBytesToReceive)
            ProgressBar.Value = CInt(e.BytesReceived)
            lStatus.Text = GetMBString(e.BytesReceived) + "/" + GetMBString(e.TotalBytesToReceive)

            If e.TotalBytesToReceive = e.BytesReceived Then
                Done = True
            End If
        End If
    End Sub

    Private Sub DownloadForm_FormClosing(ByVal sender As Object, ByVal e As FormClosingEventArgs) Handles Me.FormClosing
        WebClient.Dispose()
    End Sub
End Class