using PicView.Core.DebugTools;
using PicView.Core.ImageDecoding;

namespace PicView.Core.Conversion;

public static class ConversionHelper
{
    public static async Task<bool> ResizeByWidth(FileInfo fileInfo, double width)
    {
        if (width <= 0)
        {
            return false;
        }

        return await SaveImageFileHelper.ResizeImageAsync(fileInfo, (uint)width, 0).ConfigureAwait(false);
    }

    public static async Task<bool> ResizeByHeight(FileInfo fileInfo, double height)
    {
        if (height <= 0)
        {
            return false;
        }

        return await SaveImageFileHelper.ResizeImageAsync(fileInfo, 0, (uint)height).ConfigureAwait(false);
    }

    public static bool DetermineIfOptimizeImageShouldBeEnabled(FileInfo fileInfo)
    {
        if (fileInfo is null)
        {
            return false;
        }

        try
        {
            return fileInfo.Extension.Equals(".jpg", StringComparison.InvariantCultureIgnoreCase)
                   || fileInfo.Extension.Equals(".jpeg", StringComparison.InvariantCultureIgnoreCase)
                   || fileInfo.Extension.Equals(".png", StringComparison.InvariantCultureIgnoreCase)
                   || fileInfo.Extension.Equals(".gif", StringComparison.InvariantCultureIgnoreCase);
        }
        catch (Exception e)
        {
            DebugHelper.LogDebug(nameof(ConversionHelper), nameof(DetermineIfOptimizeImageShouldBeEnabled), e);
            return false;
        }
    }
}