module OA
  class HFAPage < OA::BasePage
    page_url "/accounting/reconcilehfainvoices"

    button(:approve_button, value: "Approve")
    button(:void_button, value: "Void")
    div(:modal_window, class: "overlay-modal")
    button(:approve_reconciliation_yes_button, value: "Yes")
    button(:approve_payment_yes_button, value: "I Approve!")
    button(:void_it_yes_button, value: "Ok! Void it!")
    div(:success_message, id: "hfa_success_message")
    table(:hfa_table, class: "data")
    link(:noi_reconciliation_link) { row_for_invoice("NOI").link_element(text: "Download Reconciliation") }
    link(:hfa_direct_invoice_link) { row_for_invoice("HFA + Direct").link_element(text: "Download Invoice") }

    def row_for_invoice(invoice_type)
      found_row = hfa_table_element.find { |row| row["Invoice Type"].text == invoice_type }
      fail "No column found in hfa table with invoice type #{invoice_type}" if found_row.nil?
      found_row
    end

    def invoice_id_for_button(button_accessor)
      send("#{button_accessor}_element").attribute "id"
    end

    def invoice_period_for_id(html_id)
      cell_element(id: "hfaPeriodCode_#{html_id}").text
    end

    def invoice_status_for_id(html_id)
      cell_element(id: "payStatus_#{html_id}").text
    end

    def download_report(report)
      report_element = case report
                       when :invoice
                         hfa_direct_invoice_link_element
                       when :reconciliation
                         noi_reconciliation_link_element
                       else
                         fail "unknown report type #{report}!"
                       end
      grab_s3_public_url(report_element)
    end

    class << self
      attr_accessor :load_fixtures
    end
  end
end
