Then(/^I should land on the breakdown for the current period$/) do
  puts "Label info: #{@label}"

  # This isn't very good; it doesn't actually verify the new period is enabled for a monthly label
  # But, we will do this until we have a visual indicator of current period in the UI
  exp_start, exp_end = case @label["type"]
                       when "monthly"
                         [ENVied.PERIOD, ENVied.PERIOD]
                       when "quarterly"
                         [quarter_start_for_period(ENVied.PERIOD), quarter_end_for_period(ENVied.PERIOD)]
                       else
                         fail "Label is neither monthly nor quarterly! #{@label}"
                       end

  on(BreakdownPage) do |page|
    page.statement_table_element.when_present # Making sure we're on the correct page

    puts "Expected current period (set by env var PERIOD): #{ENVied.PERIOD}"
    puts "Expected URL query string values: #{exp_start}, #{exp_end}"

    # TODO: replace with Addressable::URI parsing if we want a more sophisticated check here (prob not needed)
    require "uri"
    require "cgi"
    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(BreakdownPage) 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}"
    expect(actual_dec).to be_within(1.00).of(expected_dec), "Outstanding balance on the page (#{actual_2d}) "\
    "does not match expected (#{expected_2d}). Difference: #{actual_dec - expected_dec}"
  end
end
