Given(/an admin visits the (.*) chat page/) do |name|
  webinar = Webinar.find_by(name: name)
  @context.visit_page("/admin/webinars/#{webinar.id}/chat", nil)
  @context.wait_for_ajax
end

When(/the admin (privately)? ?replies to the message (.*) with (.*)/) do |privately, post, reply|
  message = @context.find_post(post)
  button_selector = privately.nil? ? '.reply-button' : '.private-button'
  message.find(button_selector).click
  page.find('.reply-modal textarea').fill_in(with: reply)
  page.find('.reply-modal .modal-button', text: 'Reply').click
end

When(/the admin blocks the sender of the message (.*)/) do |message|
  post = @context.find_post(message)
  post.find('.block-button').click
  page.find('.confirm-block-modal .modal-button', text: 'Block').click
end

Then(/the message (.*) should be marked as a teachable message/) do |message|
  post = @context.find_post(message)
  expect(post).to match_selector('.teachable')
end

Then(/the message (.*) should have a (private)? ?reply saying (.*)/) do |post, privately, reply|
  message_selector = privately.nil? ? '.chat-message' : '.chat-message.private'
  posts = page.all(message_selector, text: post, exact_text: false)
  expect(posts.last).to have_text(reply, exact: false)
end

Then(/(.*) should appear as blocked/) do |user_name|
  selector =  ".chat-message[data-name=\"#{user_name}\"]"
  post = page.find(:css, selector)
  expect(post).to match_selector('.user-blocked')
end

Then(/a message reading (.*) should be in the (.*) tab/) do |message, tab_name|
  page.find('.chat-column .categories li', text: tab_name, exact_text: false).click

  post = @context.find_post(message)
  expect(post).not_to be_nil
end
