def download_to(dir = nil, opts = { force_download: false })
      failed = []
      succeeded = []
      skipped = []

      setup_download_dir!(dir)
      if opts[:force_download]
        FileUtils.rm_r(to_path)
        setup_download_dir!(dir)
      end

      Tools.hydra_streaming(
        pages,
        chapter.hydra_opts
      ) do |stage, page, data = nil|
        case stage
        when :failed
          failed << [page, data]
        when :succeeded
          succeeded << page
        when :before
          !(page.file_exist?(to_path) && skipped << page)
        when :body
          page.append_file_data(to_path, data) unless failed.include?(page)
        when :complete
          page.append_file_ext(to_path) unless failed.include?(page)
        end
      end

      FileUtils.rm_r(to_path) if succeeded.empty? && skipped.empty?

      { failed: failed, succeeded: succeeded, skipped: skipped }
    end
def download_to(dir = nil, opts = { force_download: false })
      failed = []
      succeeded = []
      skipped = []

      setup_download_dir!(dir)
      if opts[:force_download]
        FileUtils.rm_r(to_path)
        setup_download_dir!(dir)
      end

      Tools.hydra_streaming(
        pages,
        chapter.hydra_opts
      ) do |stage, page, data = nil|
        case stage
        when :failed
          failed << [page, data]
        when :succeeded
          succeeded << page
        when :before
          !(page.file_exist?(to_path) && skipped << page)
        when :body
          page.append_file_data(to_path, data) unless failed.include?(page)
        when :complete
          page.append_file_ext(to_path) unless failed.include?(page)
        end
      end

      FileUtils.rm_r(to_path) if succeeded.empty? && skipped.empty?

      { failed: failed, succeeded: succeeded, skipped: skipped }
    end
rubocop:disable Metrics/AbcSize
 rubocop:disable Metrics/CyclomaticComplexity
 rubocop:disable Metrics/MethodLength
 rubocop:disable Metrics/PerceivedComplexity