# typed: strict
# frozen_string_literal: true

class LoadedMedia::Gif < LoadedMedia
  sig { params(media_path: String).void }
  def initialize(media_path)
    @vips_image = T.let(Vips::Image.gifload(media_path, n: -1), Vips::Image)
    @num_frames = T.let(@vips_image.get("n-pages"), Integer)
    @width = T.let(@vips_image.width, Integer)
    @height = T.let(@vips_image.height / @num_frames, Integer)
  end

  sig { override.returns(Integer) }
  def num_frames
    @num_frames
  end

  sig do
    override
      .params(frame: Integer, path: String, options: ThumbnailOptions)
      .void
  end
  def write_frame_thumbnail(frame, path, options)
    # crop out the frame we're interested in
    # then save it to the path
    image = @vips_image.crop(0, frame * @height, @width, @height)
    write_image_thumbnail(image, path, options)
  end
end
