//  
//   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.
// 

using System;

using Rebex.IO.Compression;

namespace Rebex.Samples
{
    /// <summary>
    /// Miscellaneous helper methods.
    /// </summary>
    public static class Util
    {
        /// <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 static string UrlPathCombine(string path1, string path2)
        {
            return string.Format("{0}{1}{2}",
                path1.TrimEnd(Constants.UrlPathDelimiter),
                Constants.UrlPathDelimiter,
                path2.TrimStart(Constants.UrlPathDelimiter));
        }

        /// <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 static string GetIcon(IconType iconType)
        {
            string fn;
            switch (iconType)
            {
                case IconType.File:
                    fn = "file.png";
                    break;
                case IconType.Folder:
                    fn = "folder.png";
                    break;
                case IconType.DirectoryUp:
                    fn = "directory_up.png";
                    break;
                case IconType.Close:
                    fn = "close.png";
                    break;
                default:
                    throw new ArgumentException(string.Format("Unknown IconType value '{0}'.", iconType), "iconType");
            }
            return UrlPathCombine(Constants.ImagesVirtualDirectory, fn);
        }

        /// <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 static string GetSize(long size)
        {
            if (size == 0)
                return string.Empty;

            if (size < 1024)
                return string.Format("{0} B", size);

            return string.Format("{0:N} KB", size / 1024.0);
        }

        /// <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 static string GetRatio(ZipItem item)
        {
            if (!item.IsFile || item.Length <= 0)
                return string.Empty;

            double ratio = (1 - (double)item.CompressedLength / (double)item.Length) * 100;

            return string.Format("{0:N1}%", ratio);
        }

        /// <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 static string GetIcon(ZipItem item)
        {
            if (item == null)
                return string.Empty;

            switch (item.ItemType)
            {
                case ArchiveItemType.Directory:
                    return Util.GetIcon(IconType.Folder);

                case ArchiveItemType.File:
                    return Util.GetIcon(IconType.File);

                default:
                    throw new InvalidOperationException(string.Format("Unknown ZipItemType '{0}'.", item.ItemType));
            }
        }
    }
}
