defmodule MusicLibrary.Assets.Image do
  alias Vix.Vips.{Image, Operation}

  fallback_path = Application.app_dir(:music_library, ["priv", "image-not-found.jpg"])
  fallback_data = File.read!(fallback_path)

  @external_resource fallback_path

  @default_size 2000
  @default_format "image/jpeg"

  @spec fallback_data() :: binary()
  def fallback_data, do: unquote(fallback_data)

  @spec resize(binary(), pos_integer(), String.t()) :: {:ok, binary()} | {:error, term()}
  def resize(cover_data, size \\ @default_size, format \\ @default_format) do
    :telemetry.span(
      [:music_library, :assets, :image, :resize],
      %{},
      fn ->
        {:ok, thumb} = Operation.thumbnail_buffer(cover_data, size)
        result = Image.write_to_buffer(thumb, extension(format))
        {result, %{}}
      end
    )
  end

  @spec convert(binary(), String.t(), String.t()) :: {:ok, binary()}
  def convert(cover_data, data_format, target_format) do
    if data_format == target_format do
      {:ok, cover_data}
    else
      :telemetry.span(
        [:music_library, :assets, :image, :convert],
        %{},
        fn ->
          {:ok, image} = Image.new_from_buffer(cover_data)
          result = Image.write_to_buffer(image, extension(target_format))
          {result, %{}}
        end
      )
    end
  end

  defp extension("image/jpeg"), do: ".jpg"
  defp extension("image/webp"), do: ".webp"
end
