'  
'   Rebex Sample Code License
' 
'   Copyright 2026, 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 ZIP archive options dialog.
''' </summary>
Partial Public Class OptionsDialog
    Inherits Form

    ' values of form controls (set when the form is shown)
    Private _compressionLevel As Integer
    Private _compressionMethod As Integer
    Private _encryptionAlgorithm As Integer
    Private _password As String
    Private _comment As String

    ''' <summary>
    ''' Gets the selected compression method.
    ''' </summary>
    Private ReadOnly Property CompressionMethod() As CompressionMethod
        Get
            Select Case comboCompression.SelectedIndex
                Case 1
                    Return CompressionMethod.Deflate
                Case 2
                    Return CompressionMethod.EnhancedDeflate
                Case Else
                    Return CompressionMethod.NoCompression
            End Select
        End Get
    End Property

    ''' <summary>
    ''' Gets the selected encryption algorithm.
    ''' </summary>
    Private ReadOnly Property EncryptionAlgorithm() As EncryptionAlgorithm
        Get
            Select Case comboEncryption.SelectedIndex
                Case 1
                    Return EncryptionAlgorithm.Aes256
                Case 2
                    Return EncryptionAlgorithm.Aes192
                Case 3
                    Return EncryptionAlgorithm.Aes128
                Case 4
                    Return EncryptionAlgorithm.Zip20
                Case Else
                    Return EncryptionAlgorithm.None
            End Select
        End Get
    End Property

    ''' <summary>
    ''' Initializes a new instance of the dialog.
    ''' </summary>
    Public Sub New()
        InitializeComponent()

        ' select default values
        trackCompressionLevel.Value = 6
        ' Level 6
        comboCompression.SelectedIndex = 1
        ' Deflate
        ' No encryption
        comboEncryption.SelectedIndex = 0
    End Sub

    ''' <summary>
    ''' Initializes the dialog with the specified ZIP archive.
    ''' </summary>
    ''' <param name="archive">ZIP archive to use for initialization.</param>
    Public Sub Initialize(ByVal archive As ZipArchive)
        txtComment.Text = archive.Comment

        ApplyFormValues(archive)
    End Sub

    ''' <summary>
    ''' Displays the dialog with the specified message.
    ''' </summary>
    ''' <param name="owner">Owner of the dialog.</param>
    ''' <param name="archive">Archive to set the selected values to.</param>
    Public Overloads Sub ShowDialog(ByVal owner As Control, ByVal archive As ZipArchive)
        If archive Is Nothing OrElse archive.IsReadOnly Then
            txtComment.Enabled = False
        Else
            txtComment.Enabled = True
        End If

        RememberFormValues()

        If MyBase.ShowDialog(owner) = DialogResult.OK Then
            ApplyFormValues(archive)
        Else
            RestoreFormValues()
        End If
    End Sub

    ''' <summary>
    ''' Saves the form values (to be able to restore them when the Cancel button is pressed).
    ''' </summary>
    Private Sub RememberFormValues()
        _compressionLevel = trackCompressionLevel.Value
        _compressionMethod = comboCompression.SelectedIndex
        _encryptionAlgorithm = comboEncryption.SelectedIndex
        _password = txtPassword.Text
        _comment = txtComment.Text
    End Sub

    ''' <summary>
    ''' Restores the form values. (Called when the Cancel button is pressed.)
    ''' </summary>
    Private Sub RestoreFormValues()
        trackCompressionLevel.Value = _compressionLevel
        comboCompression.SelectedIndex = _compressionMethod
        comboEncryption.SelectedIndex = _encryptionAlgorithm
        txtPassword.Text = _password
        txtComment.Text = _comment
    End Sub

    ''' <summary>
    ''' Sets ZIP archive options to the dialog values.
    ''' </summary>
    ''' <param name="archive">ZIP archive whose optiosn to set.</param>
    Private Sub ApplyFormValues(ByVal archive As ZipArchive)
        If archive Is Nothing Then
            Return
        End If

        archive.CompressionLevel = trackCompressionLevel.Value
        archive.CompressionMethod = Me.CompressionMethod
        archive.EncryptionAlgorithm = Me.EncryptionAlgorithm

        If txtPassword.Text.Length = 0 Then
            archive.Password = Nothing
        Else
            archive.Password = txtPassword.Text
        End If

        If Not archive.IsReadOnly Then
            archive.Comment = txtComment.Text
        End If
    End Sub

    ''' <summary>
    ''' Handles an event to display correct ToolTip of compression level.
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    Private Sub trackCompressionLevel_ValueChanged(ByVal sender As Object, ByVal e As EventArgs)
        Dim msg As String = trackCompressionLevel.Value.ToString()
        Select Case trackCompressionLevel.Value
            Case 0
                msg = msg + " No compression"
                Exit Select
            Case 1
                msg = msg + " Worst compression (fast)"
                Exit Select
            Case 6
                msg = msg + " Default value"
                Exit Select
            Case 9
                msg = msg + " Best compression (slow)"
                Exit Select
            Case Else
                Exit Select
        End Select
        toolTip.SetToolTip(trackCompressionLevel, msg)
    End Sub
End Class
