module Accounting
  class RequestReportPage < UniversalPage
    include PageHelpers::ReactHelpers
    page_url("accounting/statementshistory/period/<%=params[:report_period]%>/
                      <%=params[:period]%>/<%=params[:period]%>/request_report")

    div(:page_header) { div_element(class: "report_form_container").h2_element(index: 0) }
    button(:request_report_button, class: "LoadingButton")
    link(:cancel_link, class: %w[btn btn-link])
    link(:download_link, class: "span-link")
    h4(:error_dialog_header, class: "modal-title")
    divs(:transaction_type_checkbox, class: "checkbox")

    def on_label(type_of_file)
      radio_button_element(value: type_of_file).parent
    end

    def select_transactions_types
      custom_transaction_type = []
      checkbox_elements = transaction_type_checkbox_elements.count
      rand_number_loop = rand(2..checkbox_elements)
      rand_number_loop.times do
        random_type = rand(checkbox_elements)
        next if transaction_type_checkbox_elements[random_type].checkbox.set?
        transaction_type_checkbox_elements[random_type].label.click
        custom_transaction_type << transaction_type_checkbox_elements[random_type].text.split(" ").first
      end
      custom_transaction_type.sort.join(", ")
    end

    def wait_for_transaction_section
      wait_until { transaction_type_checkbox_elements.count >= 1 }
    end
  end
end
