def build_document
      document.child << metadata

      document.child << document.create_element('trk')
      document.css('trk').first << document.create_element('name')
      document.css('name').first.content = name

      if single_segment
        document.css('trk').first << document.create_element('trkseg')

        files.each_with_object(document.css('trk').first) do |e, a|
          segment = a.css('trkseg').last
          segment << "<!-- #{e.file_name} -->"
          segment << (reverse ? e.nodes.reverse : e.nodes)
        end
      else
        files.each_with_object(document.css('trk').first) do |e, a|
          a << "<!-- #{e.file_name} -->"
          a << document.create_element('trkseg')

          segment = a.css('trkseg').last
          segment << (reverse ? e.nodes.reverse : e.nodes)
        end
      end

      if reverse
        document.css('trkpt').reverse.each_with_index { |e, i| e.css('name').first.content = "WP #{i + 1}" }
      else
        document.css('trkpt').each_with_index { |e, i| e.css('name').first.content = "WP #{i + 1}" }
      end

      document.dup.human
    end
def build_document
      document.child << metadata

      document.child << document.create_element('trk')
      document.css('trk').first << document.create_element('name')
      document.css('name').first.content = name

      if single_segment
        document.css('trk').first << document.create_element('trkseg')

        files.each_with_object(document.css('trk').first) do |e, a|
          segment = a.css('trkseg').last
          segment << "<!-- #{e.file_name} -->"
          segment << (reverse ? e.nodes.reverse : e.nodes)
        end
      else
        files.each_with_object(document.css('trk').first) do |e, a|
          a << "<!-- #{e.file_name} -->"
          a << document.create_element('trkseg')

          segment = a.css('trkseg').last
          segment << (reverse ? e.nodes.reverse : e.nodes)
        end
      end

      if reverse
        document.css('trkpt').reverse.each_with_index { |e, i| e.css('name').first.content = "WP #{i + 1}" }
      else
        document.css('trkpt').each_with_index { |e, i| e.css('name').first.content = "WP #{i + 1}" }
      end

      document.dup.human
    end
rubocop:disable Metrics/PerceivedComplexity