Then("I should see the Collaborators List") do
  on(ReactWorkstation::CollaboratorPage) do |page|
    page.get_started
    page.wait_until { page.collaborators_list_element.present? }
  end
end

And(/^I begin searching (.*?) in my catalogue$/) do |track_name|
  on(ReactWorkstation::CollaboratorPage) do |page|
    page.enter_search_term(track_name)
  end
end

And(/^I search for (.*?) in my catalogue$/) do |track_name|
  on(ReactWorkstation::CollaboratorPage) do |page|
    begin
      page.search_and_select_product(track_name, @session_token)
    rescue Watir::Exception::UnknownObjectException, Watir::Wait::TimeoutError
      step "I am on the ReactWorkstation::CollaboratorPage"
      page.search_and_select_product(track_name, @session_token)
    end
  end
end

Then("I am presented with product options for products containing that track") do
  on(ReactWorkstation::CollaboratorPage) do |page|
    page.wait_until { page.search_bar_options_elements.any? }
  end
end

When(/^I add (\d+) (.*?) collaborators$/) do |number_of_collaborators, collaborator_type|
  on(ReactWorkstation::CollaboratorTrack) do |page|
    number_of_collaborators.times do
      @name = Support::Helpers::UniqueGenerator.unique_string
      page.create_collaborator(@name, collaborator_type)
    end
    page.save_changes
  end
end

And(/^(.*?) track has at least 1 collaborator/) do |track|
  on(ReactWorkstation::CollaboratorPage) do |page|
    page.refresh_until_enabled
  end
  step "I search for #{track} in my catalogue"
  on(ReactWorkstation::CollaboratorTrack) do |page|
    @collaborator = page.first_collaborator
    step "I am on the ReactWorkstation::CollaboratorPage"
  end
end

Then("I should see new collaborator in the list") do
  on(ReactWorkstation::CollaboratorPage) do |page|
    page.wait_until { page.loader_elements.none? }
  end
  on(ReactWorkstation::CollaboratorTrack) do |page|
    page.save_changes
    expect(page.collaborator_added?(@name)).to be true
  end
end

When("I delete all collaborator splits") do
  on(ReactWorkstation::CollaboratorPage) do |page|
    page.wait_until { page.loader_elements.none? }
    on ReactWorkstation::CollaboratorTrack do |track_page|
      track_page.delete_collaborator_splits
    end
  end
end

And(/^I generate a collaborator report$/) do
  on(ReactWorkstation::CollaboratorPage) do |page|
    page.generate_new_report_btn
  end
  on(ReactWorkstation::CollaboratorGenerateReport) do |page|
    page.generate_new_report
  end
end

Then("I should see no collaborators in the list") do
  on(ReactWorkstation::CollaboratorTrack) do |page|
    page.wait_until { page.loader_element.when_not_present }
    page.save_changes(true)
    expect(page.collaborator_element.present?).to be false
  end
end

And("The total percentage for the product adds up to 100%") do
  on(ReactWorkstation::CollaboratorTrack) do |page|
    expect(page.total_percentage_from_elements).to be_within(0.1).of(100)
  end
end

And(/^I set the total percentage to exceed 100%$/) do
  on(ReactWorkstation::CollaboratorTrack) do |page|
    page.set_collaborator_percentage(1, "100%")
    page.set_collaborator_percentage(2, "1%")
  end
end

Then(/^I should see an error that the total exceeds the maximum percentage$/) do
  on(ReactWorkstation::CollaboratorPage) do |page|
    page.wait_until { page.maximum_percentage_error_element.present? }
  end
end

Then("The report should begin generating") do
  on(ReactWorkstation::CollaboratorPage) do |page|
    expect(page.report_status?(@collaborator)).to include("report-in-progress")
      .or include("ReportDownloadButton")
  end
end
