Given(/^An In Content release belonging to a D3 account$/) do
  @upc = "5907717470049"
  expected_status = "in_content"

  visit(OA::ViewReleasePage, using_params: { queries: { "upc" => @upc } }) do |page|
    expect(page.release_status).to eql expected_status
    # add expectation for release status
    @old_subaccount = page.breadcrumb_link_elements.last.text
  end

  puts "original subaccount (from breadcrumb): #{@old_subaccount}"
end

Given(/^I transfer it to a different subaccount$/) do
  on(OA::ViewReleasePage).transfer_subaccount
  on(OA::SubaccountTransferModal) do |page|
    # modal
    page.transfer_table_element.when_present
    from_modal = page.transfer_table_element["UPC"].text.split("UPC: ").last
    expect(from_modal).to eql @upc

    current = page.current.split("Current Sub-account: ").last # so we are just left with the name
    puts "current subaccount assignment: #{current}"
    expect(current).to eql @old_subaccount

    @new_subaccount = page.destination
    puts "new subaccount assignment: #{@new_subaccount}"
    expect(@new_subaccount).not_to eql(@old_subaccount), "Shouldn't be able to reassign old subaccount!"

    page.submit_transfer
    # This sometimes fails on the grid due to the iframe disappearing before
    # it can be observed.
    # message = page.transfer_success_element.when_present(120).text
    # expect(message).to eq success_message
  end
end

Given(/^It should belong to the new subaccount$/) do
  on(OA::ViewReleasePage) do |page|
    # can't wait for page to load because there are unpredictable ajax requests
    eventually do
      expect(page.breadcrumb_link_elements.last.text).to eql @new_subaccount
    end
  end
end
