'  
'   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.IO
Imports System.Windows.Forms

Imports Rebex.Security

Public Partial Class Form1
    Inherits Form
    ' encrypted file extension
    Private Const EncryptionExtension As String = ".enc"

    ' specifies operations to perform
    Private Enum Operation
        Encrypt
        Decrypt
    End Enum

    Public Sub New()
        InitializeComponent()
    End Sub

    ''' <summary>
    ''' Handles source file path changes.
    ''' </summary>
    Private Sub txtSourceFile_TextChanged(sender As Object, e As EventArgs) Handles txtSourceFile.TextChanged
        ' determine path of the output file
        Dim filePath As String = txtSourceFile.Text
        Dim extension As String = Path.GetExtension(filePath)
        If extension.Equals(EncryptionExtension, StringComparison.OrdinalIgnoreCase) Then
            Dim dir As String = Path.GetDirectoryName(filePath)
            Dim destinationFileName As String = Path.GetFileNameWithoutExtension(filePath)
            txtDestinationFile.Text = Path.Combine(dir, destinationFileName)
        Else
            txtDestinationFile.Text = filePath & EncryptionExtension
        End If
    End Sub

    ''' <summary>
    ''' Handles '...' button click.
    ''' </summary>
    Private Sub btnOpen_Click(sender As Object, e As EventArgs) Handles btnOpen.Click
        If openFileDialog.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
            txtSourceFile.Text = openFileDialog.FileName
        End If
    End Sub

    ''' <summary>
    ''' Handles 'Encrypt' button click.
    ''' </summary>
    Private Sub btnEncrypt_Click(sender As Object, e As EventArgs) Handles btnEncrypt.Click
        PerformOperation(Operation.Encrypt)
    End Sub

    ''' <summary>
    ''' Handles 'Decrypt' button click.
    ''' </summary>
    Private Sub btnDecrypt_Click(sender As Object, e As EventArgs) Handles btnDecrypt.Click
        PerformOperation(Operation.Decrypt)
    End Sub

    ''' <summary>
    ''' Encrypts or decrypts specified file.
    ''' </summary>
    ''' <param name="operation">Operation to perform.</param>
    Private Sub PerformOperation(encryptionOperation As Operation)
        ' check input values
        If String.IsNullOrEmpty(txtSourceFile.Text) Then
            ShowError("Select source file.", "File not set")
            Return
        End If

        If String.IsNullOrEmpty(txtPassword.Text) Then
            ShowError("Password must be set.", "Password not set")
            Return
        End If

        If Not File.Exists(txtSourceFile.Text) Then
            ShowError("Source file not found.", "File not found")
            Return
        End If

        If File.Exists(txtDestinationFile.Text) AndAlso Not cbxOverwriteFile.Checked Then
            ShowError("Destination file already exists." & vbLf & "Check the 'Overwrite existing file' checkbox to overwrite it.", "Destination file exists")
            Return
        End If

        ' perform operation
        Dim encryptor As New FileEncryption()
        encryptor.OverwriteExistingFile = cbxOverwriteFile.Checked
        encryptor.SetPassword(txtPassword.Text)

        Try
            If encryptionOperation = Operation.Encrypt Then
                encryptor.Encrypt(txtSourceFile.Text, txtDestinationFile.Text)
            Else
                encryptor.Decrypt(txtSourceFile.Text, txtDestinationFile.Text)
            End If

            ShowInfo("Operation finished successfully.", "Operation done")
        Catch ex As Exception
            ShowError(ex.ToString(), "Error occurred")
        End Try
    End Sub

    ''' <summary>
    ''' Shows error message.
    ''' </summary>
    ''' <param name="message">Message to show.</param>
    ''' <param name="caption">Window caption.</param>
    Private Sub ShowError(message As String, caption As String)
        MessageBox.Show(message, caption, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
    End Sub

    ''' <summary>
    ''' Shows information message.
    ''' </summary>
    ''' <param name="message">Message to show.</param>
    ''' <param name="caption">Window caption.</param>
    Private Sub ShowInfo(message As String, caption As String)
        MessageBox.Show(message, caption, MessageBoxButtons.OK, MessageBoxIcon.Information)
    End Sub
End Class
