'  
'   Rebex Sample Code License
' 
'   Copyright 2025, REBEX CR s.r.o.
'   All rights reserved.
'   https://www.rebex.net/
' 
'   Permission to use, copy, modify, and/or distribute this software for any
'   purpose with or without fee is hereby granted.
' 
'   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
'   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
'   OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
'   NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
'   HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
'   WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
'   FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
'   OTHER DEALINGS IN THE SOFTWARE.
' 

Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Text
Imports System.Windows.Forms
Imports Rebex.IO.Compression

''' <summary>
''' A dialog that displays a progress of the current operation.
''' </summary>
Partial Public Class ProgressDialog
    Inherits Form

    ' ZIP archive
    Private ReadOnly _archive As ZipArchive

    ''' <summary>
    ''' Initializes a new instance of the dialog.
    ''' </summary>
    ''' <param name="archive">Archive performing the operation.</param>
    ''' <param name="title">Dialog title.</param>
    Public Sub New(ByVal archive As ZipArchive, ByVal title As String)
        InitializeComponent()

        Me.Text = title

        lblFileIndex.Text = ""
        lblFileArchivePath.Text = ""
        lblFileExternalPath.Text = ""

        _archive = archive
    End Sub

    ''' <summary>
    ''' Adjusts the dialog controls according to the specified event arguments.
    ''' </summary>
    ''' <param name="e"></param>
    Public Sub ProgressChanged(ByVal e As ZipProgressChangedEventArgs)
        If e.OperationStep = ArchiveOperationStep.HierarchyRetrieving Then
            lblFileIndex.Text = "Retrieving hierarchy..."
        Else
            If progressBarOverall.Value <> e.ProgressPercentage Then
                progressBarOverall.Value = e.ProgressPercentage
            End If

            If progressBarCurrentFile.Value <> e.CurrentFileProgressPercentage Then
                progressBarCurrentFile.Value = e.CurrentFileProgressPercentage
            End If

            If e.OperationStep = ArchiveOperationStep.FileChecking OrElse e.OperationStep = ArchiveOperationStep.FileProcessing Then
                lblFileIndex.Text = String.Format("{0}. / {1}", e.FilesProcessed + 1, e.FilesTotal)
                lblFileArchivePath.Text = e.ArchiveItemPath
                lblFileExternalPath.Text = e.ExternalItemPath
            End If
        End If
    End Sub

    ''' <summary>
    ''' Handles a Cancel click.
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    Private Sub btnCancel_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnCancel.Click
        _archive.Cancel()
    End Sub

    ''' <summary>
    ''' Hanldes a FormClosing event.
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    Private Sub ProgressDialog_FormClosing(ByVal sender As Object, ByVal e As FormClosingEventArgs) Handles MyBase.FormClosing
        _archive.Cancel()
    End Sub
End Class
