Given(/^I am on the vendor page for a valid vendor$/) do
  vendor_id = "7123"
  visit(OA::ViewVendorPage, using_params: { queries: { "vendor_id" => vendor_id } })
end

When(/^I create the label vendor contract$/) do
  on(OA::ViewVendorPage).view_contracts
  on(OA::ViewAllAgreements).new_add_agreement
  on(OA::AddVendContractPage) do |page|
    data = DataHelpers::Contract.contract_fields
    page.populate_page_with data
    page.submit_button
  end
end

Then(/^the label vendor contract should be created$/) do
  on(OA::AddVendContractPage) do |page|
    expected = "Label Contract inserted successfully."
    expect(page.confirmation_message_element.when_present.element.text).to eq expected
  end
end

When(/^I update the active label contract info$/) do
  on(OA::ViewVendorPage).active_contract # navigates to active contract page
  on(OA::ViewVendContractPage).edit_contract_link
  on(OA::EditVendContractPage) do |page|
    page.populate_page_with DataHelpers::Contract.contract_fields
    page.click_with_javascript(page.submit_button_element)
  end
end

Then(/^the label contract info should successfully updated with success message$/) do
  on(OA::EditVendContractPage) do |page|
    expected = "Label Contract updated successfully."
    eventually(timeout: 300) { expect(page.confirmation_message_element.when_present.text).to eq expected }
  end
end

Then(/^should see newly edited contract info reflected$/) do
  on(OA::EditVendContractPage).verify_page_has DataHelpers::Contract.contract_fields
end

When(/^I create the label draft contract$/) do
  on(OA::ViewVendorPage).view_contracts
  on(OA::ViewAllAgreements).create_draft_contract
  on(OA::AddVendorProposedTermPage) do |page|
    page.populate_page_with DataHelpers::Contract.draft_contract_fields
    page.submit_button
    # check for validation error
    fail "validation error: #{page.alert_error}" if page.alert_error_element.element.present?
  end
end

Then(/^the label draft contract should be created$/) do
  on(OA::EditVendorProposedTermPage) do |page|
    expected = "Proposed Term Added Successfully."
    expect(page.confirmation_message_element.when_present.text).to eq expected
  end
end

Then(/^should see newly added draft contract info reflected$/) do
  on(OA::EditVendorProposedTermPage).verify_page_has DataHelpers::Contract.draft_contract_fields
end

When(/^I submit draft contract for approval$/) do
  on(OA::EditVendorProposedTermPage).submit_for_approval_button
end

Then(/^the draft contract should be awaiting approval$/) do
  on(OA::EditVendorProposedTermPage) do |page|
    expected = "Draft Contract is now Waiting for Approval.\nDraft Contract Updated Successfully."
    expect(page.confirmation_message_element.when_present.text).to eq expected
  end
end
