module OA
  class GeneratePPlXMlPage < OA::BasePage
    page_url "/relationships/generatePplXml"

    class << self
      attr_accessor :new_file # uploaded file path
      attr_accessor :first_input_file_path # downloaded file path
    end

    file_field(:choose_file, name: "tuid_file")
    button(:upload_button, name: "submit")
    table(:files_table, id: "available-files")
    link(:first_input_file) { files_table_element[1]["Filename"].link_element }

    def upload_ppl_file(abs_path)
      # upload copied file
      fail "file does not exist at path: #{abs_path}" unless File.exist?(abs_path)
      self.choose_file = abs_path
      upload_button
    end

    def download_input_file
      return self.class.first_input_file_path unless self.class.first_input_file_path.nil?
      first_input_file # click the link, which sets the HTML attrib containing the S3 URL
      wait_until { first_input_file_element.attribute("data-public-s3-url").is_a?(String) }
      first_input_file_element.attribute "data-public-s3-url"
    end
  end
end
