Then(/^I should land on the breakdown for the current period$/) do
  label = @users.workstation_user.info
  exp_start, exp_end = case label[:type]
                       when "monthly"
                         [@accounting_period, @accounting_period]
                       when "quarterly"
                         [quarter_start_for_period(@accounting_period),
                          quarter_end_for_period(@accounting_period)]
                       else
                         fail "Label is neither monthly nor quarterly! #{label}"
                       end

  on(Workstation::AccountingBreakdownPage) do |page|
    page.opening_balance_element.when_present
    puts "Expected current period (set by env var PERIOD): #{@accounting_period}"
    puts "Expected URL query string values: #{exp_start}, #{exp_end}"
    params = CGI.parse(URI.parse(page.current_url).query)
    start_period_from_url = params["accounting_start"][0]
    end_period_from_url = params["accounting_end"][0]
    puts "Actual URL start period: #{start_period_from_url}"
    puts "Actual URL end period: #{end_period_from_url}"

    expect(start_period_from_url).to eq(exp_start.to_s)
    expect(end_period_from_url).to eq(exp_end.to_s)
  end
end

Then(/^the totals for the breakdown page should add up$/) do
  on(Workstation::AccountingBreakdownPage) do |page|
    expected_dec = page.expected_outstanding_balance # BigDecimal
    expected_2d = formatted_to_2d_precision(expected_dec)

    # Actual amount, formatted in different ways
    actual_string = page.outstanding_balance # String
    actual_dec = currency_amount_from(actual_string) # BigDecimal
    actual_2d = formatted_to_2d_precision(actual_dec) # String with 2 digit precision

    puts "Calculated Expected Balance: #{expected_2d}"
    puts "Outstanding Balance on Page: #{actual_2d}"
    error_msg = "Outstanding balance on the page (#{actual_2d}) "\
    "does not match expected (#{expected_2d}). Difference: #{actual_dec - expected_dec}"
    expect(actual_dec).to be_within(1.00).of(expected_dec), error_msg
  end
end

Then(/^the statement for the current period should be visible$/) do
  label = @users.workstation_user.info
  puts "Label info: #{label}"
  on(Workstation::AccountingStatementsPage) do |page|
    page.wait_for_loading_bar_to_disappear
    page.wait_until { page.request_report_elements.count >= 1 }
    latest_period_row = page.statement_table_element.when_present[1]
    latest_period = latest_period_row[0].text
    puts "Latest Period on page: #{latest_period}"
    if label[:type] == "monthly"
      expected = period_to_month_string(@accounting_period)
      puts "Expected Period based on PERIOD env var: #{expected}"
    elsif label[:type] == "quarterly"
      expected = period_to_quarter_string(@accounting_period)
      puts "Expected Period based on PERIOD env var: #{expected}"
    else
      fail "Cannot determine payment interval type for label: #{label}"
    end
    expect(latest_period).to eq(expected)
    @exp_revenue = latest_period_row[1].text
  end
end

When(/^the Revenue listed should match that of the BreakdownPage$/) do
  on(Workstation::AccountingBreakdownPage) do |page|
    @actual_revenue = page.revenue_element.when_present.text
    puts "Revenue from Breakdown page: #{@actual_revenue}"
  end
  expect(currency_amount_from(@actual_revenue)).to eq(currency_amount_from(@exp_revenue))
end

When(/^I request a custom report for the last period$/) do
  on(Accounting::StatementsPage) do |page|
    page.wait_until { page.request_report_elements.count >= 1 }
    page.request_report_elements.first.click
  end

  on(Accounting::RequestReportPage) do |page|
    @report_period = page.page_header.split(" ").last(2).join
    @selected_report_number_format = "US"
    @selected_report_file_type = "xls"
    @selected_report_type = "custom"

    page.on_label(@selected_report_type).click
    page.on_label("en_#{@selected_report_number_format}").click
    page.wait_for_transaction_section
    @expected_transaction_type = page.select_transactions_types
    page.request_report_button
  end
end

Then(/^the last report should be displayed on the requested reports history page$/) do
  on(Accounting::ViewRequestedReports) do |page|
    expect(page.alert_msg_element.present?).to eq(true)
    page.wait_for_progress_bar
    last_report_details = page.report_details(page.reports_elements.first)
    if @selected_report_type == "custom"
      expect(last_report_details[:transaction_types]).to eq(@expected_transaction_type)
    end
    expect(Date.parse(last_report_details[:date])).to eq(Date.today)
    expect(last_report_details[:number_format]).to eq(@selected_report_number_format)
    expect(last_report_details[:file_type]).to eq(@selected_report_file_type)
  end
end
