'  
'   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.Collections.Generic
Imports System.Text

''' <summary>
''' Specifies operations to perform.
''' </summary>
Public Enum Operation
    Encrypt
    Decrypt
End Enum

''' <summary>
''' Command line arguments parser and storage.
''' </summary>
Public Class Arguments
    ' keyword for overwrite option
    Private Const OverwriteOption As String = "overwrite"

    ''' <summary>
    ''' Gets the selected operation.
    ''' </summary>
    Public Property Operation() As Operation
        Get
            Return m_Operation
        End Get
        Private Set
            m_Operation = Value
        End Set
    End Property
    Private m_Operation As Operation

    ''' <summary>
    ''' Gets the source file path.
    ''' </summary>
    Public Property SourceFile() As String
        Get
            Return m_SourceFile
        End Get
        Private Set
            m_SourceFile = Value
        End Set
    End Property
    Private m_SourceFile As String

    ''' <summary>
    ''' Gets the destination path.
    ''' </summary>
    Public Property DestinationFile() As String
        Get
            Return m_DestinationFile
        End Get
        Private Set
            m_DestinationFile = Value
        End Set
    End Property
    Private m_DestinationFile As String

    ''' <summary>
    ''' Gets the password to apply.
    ''' </summary>
    Public Property Password() As String
        Get
            Return m_Password
        End Get
        Private Set
            m_Password = Value
        End Set
    End Property
    Private m_Password As String

    ''' <summary>
    ''' Gets a value indicating whether to overwrite existing file.
    ''' </summary>
    Public Property Overwrite() As Boolean
        Get
            Return m_Overwrite
        End Get
        Private Set
            m_Overwrite = Value
        End Set
    End Property
    Private m_Overwrite As Boolean

    ''' <summary>
    ''' Creates the instance from command line arguments.
    ''' </summary>
    ''' <param name="args">Command line arguments.</param>
    Public Sub New(args As String())
        ' check number of arguments
        If args.Length < 4 OrElse args.Length > 5 Then
            Throw New SampleException("Invalid number of arguments.")
        End If

        ' process arguments
        Try
            Operation = CType([Enum].Parse(GetType(Operation), args(0), True), Operation)
        Catch generatedExceptionName As ArgumentException
            Throw New SampleException(String.Format("Unknown operation specified: {0}", args(0)))
        End Try

        SourceFile = args(1)
        DestinationFile = args(2)
        Password = args(3)

        If args.Length = 5 Then
            If args(4).Equals(OverwriteOption, StringComparison.OrdinalIgnoreCase) Then
                Overwrite = True
            Else
                Throw New SampleException(String.Format("Unknown option specified: {0}", args(4)))
            End If
        End If
    End Sub
End Class
