Given(/^I have a new blacklist word$/) do
  @word = "TestWord" + Time.stamp.last(8)
  Support::Services::OwsBlacklistManager.create_blacklist_word @word
end

When(/^I add a valid word to the blacklist$/) do
  on(BlacklistManager::BlacklistManagerPage) do |page|
    @word = "TestWord" + Time.stamp.last(8)
    page.add_word_entry = @word
    page.notes = @word
    page.spinner_element.wait_while(&:present?)
    page.button_element(id: "DropdownButton").click
    # Wait for reason data to populate
    Retriable.retriable(on: NoMethodError, tries: 4, base_interval: 0.5) do
      page.wait_until { page.reason_elements[0].text }
    end
    @reason = page.reason_elements[0].text
    page.reason_elements[0].click
    page.add_word
    page.wait_until { page.success_alert_element.present? }
    page.wait_until { page.success_alert_element.text.include? "Word(s) added sucessfully" }
  end
end

When(/^I search for the (new |)word in the blacklist$/) do |word|
  on(BlacklistManager::BlacklistManagerPage) do |page|
    search_entry = if word == "new "
                     @new_word
                   else
                     @word
                   end
    page.spinner_element.wait_while(&:present?)
    Retriable.retriable(on: Selenium::WebDriver::Error::UnknownError, tries: 3, base_interval: 1) do
      page.svg_element(class: "Icons-close-round").click
    end
    page.spinner_element.wait_while(&:present?)
    page.search_word_entry = search_entry
    page.search_word
    page.spinner_element.wait_while(&:present?)
  end
end

Then(/^I see the word listed in the blacklist$/) do
  on(BlacklistManager::BlacklistManagerPage) do |page|
    eventually do
      expect(page.word_cell).to eq @word
      expect(page.notes_cell).to eq @word
      expect(page.reason_cell).to eq @reason
      expect(page.date_added_cell).to eq Time.now.strftime("%Y-%m-%d")
    end
  end
end

Then(/^I see the new word listed in the blacklist$/) do
  on(BlacklistManager::BlacklistManagerPage) do |page|
    # Wait for cell data to populate
    Retriable.retriable(on: NoMethodError, tries: 4, base_interval: 0.5) do
      page.wait_until { page.word_cell }
    end
    eventually { expect(page.word_cell).to eq @new_word }
  end
end

When(/^I delete a word from the blacklist$/) do
  on(BlacklistManager::BlacklistManagerPage) do |page|
    Retriable.retriable(on: Watir::Exception::LocatorException, tries: 2) do
      page.wait_until { page.delete_word_elements.present? }
    end
    page.wait_until { page.word_cell == @word }
    page.delete_word_elements[0].click
    page.button_element(class: "BlacklistAlertModal-button", text: "Confirm").click
  end
end

Then(/^I don't see the word listed in the blacklist$/) do
  eventually do
    expect(on(BlacklistManager::BlacklistManagerPage)
        .cell_element(class: "BlacklistTable-noResult").present?).to be true
  end
end

When(/^I edit a word in the blacklist$/) do
  on(BlacklistManager::BlacklistManagerPage) do |page|
    @new_word = "TestWord" + Time.stamp.last(8)

    Retriable.retriable(on: Watir::Exception::LocatorException, tries: 2) do
      page.wait_until { page.delete_word_elements.present? }
    end

    number_of_chars = page.word_cell.length

    # Editing requires a triple click of the cell: double_click followed by short pause and single click
    edit_field = proc do
      puts "attempting to edit field"
      page.word_cell_element.parent.double_click
      page.word_cell_element.parent.click
    end
    edit_field
    Retriable.retriable(on: Watir::Wait::TimeoutError, base_interval: 0.5, tries: 5, on_retry: edit_field) do
      page.wait_until(1) { page.word_cell_element.parent.text_field.enabled? }
    end

    page.empty_word_cell number_of_chars

    edit_field
    Retriable.retriable(on: Watir::Wait::TimeoutError, base_interval: 0.5, tries: 5, on_retry: edit_field) do
      page.wait_until(1) { page.word_cell_element.parent.text_field.enabled? }
    end
    page.word_cell_element.parent.text_field.send_keys @new_word
    @browser.send_keys :enter
    page.wait_until { page.success_alert_element.present? }
    page.wait_until { page.success_alert_element.text.include? "Blacklist Word is successfully updated" }
  end
end
