'  
'   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 Rebex.IO.Compression

''' <summary>
''' Miscellaneous helper methods.
''' </summary>
Public NotInheritable Class Util
    Private Sub New()
    End Sub
    ''' <summary>
    ''' Combines two URL path strings.
    ''' </summary>
    ''' <param name="path1">First path.</param>
    ''' <param name="path2">Second path.</param>
    ''' <returns>A string containing the combined paths.</returns>
    Public Shared Function UrlPathCombine(ByVal path1 As String, ByVal path2 As String) As String
        Return String.Format("{0}{1}{2}", path1.TrimEnd(Constants.UrlPathDelimiter), Constants.UrlPathDelimiter, path2.TrimStart(Constants.UrlPathDelimiter))
    End Function

    ''' <summary>
    ''' Determines relative web URL (file name) of an image from the specified icon type.
    ''' </summary>
    ''' <param name="iconType">An icon type.</param>
    ''' <returns>File name of the corresponding image.</returns>
    Public Shared Function GetIcon(ByVal iconType As IconType) As String
        Dim fn As String
        Select Case iconType
            Case iconType.File
                fn = "file.png"
                Exit Select
            Case iconType.Folder
                fn = "folder.png"
                Exit Select
            Case iconType.DirectoryUp
                fn = "directory_up.png"
                Exit Select
            Case iconType.Close
                fn = "close.png"
                Exit Select
            Case Else
                Throw New ArgumentException(String.Format("Unknown IconType value '{0}'.", iconType), "iconType")
        End Select
        Return UrlPathCombine(Constants.ImagesVirtualDirectory, fn)
    End Function

    ''' <summary>
    ''' Returns a size of the specified value as a formated string (including unit).
    ''' </summary>
    ''' <param name="size">Size in bytes.</param>
    ''' <returns>String containing file size or empty string for zero size.</returns>
    Public Shared Function GetSize(ByVal size As Long) As String
        If size = 0 Then
            Return String.Empty
        End If

        If size < 1024 Then
            Return String.Format("{0} B", size)
        End If

        Return String.Format("{0:N} KB", size / 1024)
    End Function

    ''' <summary>
    ''' Returns a compression ratio for the current ZIP item (or empty string if the ZIP item is a directory). 
    ''' </summary>
    ''' <param name="item">A ZIP item.</param>
    ''' <returns>String with compression ratio (including the '%' sign) or empty string.</returns>
    Public Shared Function GetRatio(ByVal item As ZipItem) As String
        If Not item.IsFile OrElse item.Length <= 0 Then
            Return String.Empty
        End If

        Dim ratio As Double = (1 - CDbl(item.CompressedLength) / CDbl(item.Length)) * 100

        Return String.Format("{0:N1}%", ratio)
    End Function

    ''' <summary>
    ''' Determines a relative web URL of an image associated with the specified item's type.
    ''' </summary>
    ''' <param name="item">A ZIP item.</param>
    ''' <returns>Relative web URL of an image associated with the specified item's type.</returns>
    Public Shared Function GetIcon(ByVal item As ZipItem) As String
        If item Is Nothing Then
            Return String.Empty
        End If

        Select Case item.ItemType
            Case ArchiveItemType.Directory
                Return Util.GetIcon(IconType.Folder)
            Case ArchiveItemType.File

                Return Util.GetIcon(IconType.File)
            Case Else

                Throw New InvalidOperationException(String.Format("Unknown ZipItemType '{0}'.", item.ItemType))
        End Select
    End Function
End Class
