
When(/^I create a new auth0 user$/) do
  on(ReactWorkstation::ReactHomePage).go_to_admin_page
  on(Workstation::AdminPage) do |page|
    wait_for_dots = proc do |exception, try|
      Kernel.puts "#{exception.class}: '#{exception.message}' - Attempt: #{try}"
      page.wait_until { page.div_elements(class: "LoadingIndicator-dots").none? }
    end
    Retriable.retriable(on: Selenium::WebDriver::Error::UnknownError, on_retry: wait_for_dots) do
      page.create_new_user
    end
  end
  @timestamped_test_name = Time.stamp
  @new_auth0_username = "auth0+" + @timestamped_test_name + "@test.theorchard.io"
  on(Workstation::CreateNewUserPage) do |page|
    page.user_name = @new_auth0_username
    page.contact_first_name = @timestamped_test_name
    page.contact_last_name = @timestamped_test_name
    data = DataHelpers::WorkstationManageUsersData.create_auth0_users
    page.populate_auth0_user_basics(data)
    page.role_id_element.parent.click
    page.create_user_button
  end
end

Then(/^the user should be created$/) do
  on(Workstation::AdminPage) do |page|
    page.vendor_contact_table_element.element.wait_until(timeout: 60, &:present?)
    puts "created user: #{@timestamped_test_name}"
    next_page = proc do
      puts "User not found on current page. Verify if DB refresh is currently disabled."
      page.next_page_link
      page.vendor_contact_table_element.element.wait_until(timeout: 60, &:present?)
    end
    Retriable.retriable(on: RSpec::Expectations::ExpectationNotMetError, tries: 6, on_retry: next_page) do
      expect(page.manage_users_table).to include(@timestamped_test_name)
    end
  end
end

When(/^I disable the created user$/) do
  on(Workstation::AdminPage) do |page|
    @number_of_vendors = page.vendor_contact_table_element.rows
    puts "original count = #{@number_of_vendors}"
    contact_table_element = page.vendor_contact_table_element.find do |x|
      x.text.include?(@timestamped_test_name)
    end
    contact_table_element.link_element(class: "btn-danger").click
    page.confirm_disable
  end
end

Then(/^the created user should not display on the admin page$/) do
  on(Workstation::AdminPage) do |page|
    Retriable.retriable(on: [Watir::Exception::LocatorException,
                             RSpec::Expectations::ExpectationNotMetError], tries: 3, base_interval: 10) do
      puts "current count: #{page.vendor_contact_table_element.rows}"
      expect(page.manage_users_table).not_to include(@timestamped_test_name)
    end
  end
end

When(/^I perform a user search for the term: (.*?)$/) do |search_string|
  on(Users::ManageUsersPage) do |page|
    page.users_loader_element.wait_while(&:present?)
    page.search_field_element.wait_until(&:present?)
    page.throttled_input(page.search_field_element, search_string, 0.5, false)
  end
end

Then(/^all the user search results should contain (.*?)$/) do |search_string|
  on(Users::ManageUsersPage) do |page|
    page.users_loader_element.wait_while(&:present?) if page.users_loader?
    page.search_result_elements.each do |search_result|
      eventually(timeout: 5) { expect(search_result.text.downcase).to include(search_string.downcase) }
    end
  end
end
