When(/^I log out of Workstation$/) do
  on(ReactWorkstation::ReactHomePage).logout
end

Then(/^I should be logged out of Workstation$/) do
  # The if statement below will be removed when the login v2 replace the old login page
  # If is responsible to validate the presence of Login v2 page
  if on(Workstation::LoginPage).login_form? == false
    on(Workstation::LoginPage) do |page|
      expect(page.login_form?).to be true
    end
  end
end

Given(/^I am Workstation unauthenticated$/) do
  visit(Workstation::BasePage) do |page|
    page.logout if page.logged_in?
  end
  on(Workstation::LoginPage) do |page|
    expect(page.current_url).to include "qa-orchard.auth0.com"
  end
end

When(/^I attempt to log in to Workstation with invalid credentials$/) do
  user = DataHelpers::UserConfig.lookup(:workstation, :invalid)
  on(Workstation::LoginPage).login_with_user_data(user)
end

Then(/^I should see a Workstation error message$/) do
  error_text = "Sorry, that username/password combination does not match our records"
  expect(on(Workstation::LoginPage).alert_text).to include error_text
end

Then(/^I should be logged in to Workstation$/) do
  on(ReactWorkstation::ReactHomePage) do |page|
    page.expect_to_be_on_page
    expect(page.logged_in_as?(@users.workstation_user.info)).to be true
  end
end

When(/^I visit a specific Workstation URL$/) do
  @desired_final_url = on(Workstation::AdminPage).page_url_value
  visit Workstation::AdminPage
end

Then(/^I should be redirected to the correct page after a successful login$/) do
  default = DataHelpers::UserConfig.lookup(:workstation, :label_7123)
  on(Workstation::LoginPage) do |page|
    page.login_with(default[:username], default[:password])
    eventually do
      expect(page.current_url).to eq @desired_final_url
    end
  end
end

#################################
# Begin Workstation login stuff #
#################################
Given(/^Workstation (admin|film|tv) impersonation is enabled$/) do |user|
  yml_name = :"label_7123_#{user}"
  @users.record_workstation_user(
    name: yml_name,
    user_data: DataHelpers::UserConfig.lookup(:workstation, yml_name)
  )
  unless on(OA::ViewVendorPage).send "login_as_#{user}_user?"
    fail "Cannot reenable '#{user}' user!" if user.to_s.downcase == "admin"
    on(OA::ViewVendorPage) do |page|
      page.label_contact_link(user).click
    end
    on(OA::EditLabelContactPage) do |page|
      page.active_selector = "Y"
      page.submit
      page.label_id_link
    end
  end
end

# assumes we are on a label page
When(/^I impersonate Workstation (admin|film|tv) login for that label$/) do |user|
  on(OA::ViewVendorPage).impersonate_login_for(user: user.to_sym)
  yml_name = :"label_7123_#{user}"
  @users.record_workstation_user(
    name: yml_name,
    user_data: DataHelpers::UserConfig.lookup(:workstation, yml_name)
  )
  on(ReactWorkstation::ReactHomePage) do |page|
    page.goto # this clears the url queries so expect_to_be_on_page doesnt fail
    page.wait_until { page.logged_in? }
  end
end
