module PublishingCukeHelpers
  def make_post(text)
    fill_in 'status_message_fake_text', :with => text
    click_button :submit
    wait_for_ajax_to_finish
  end

  def click_and_post(text)
    click_publisher
    make_post(text)
  end

  def click_publisher
    page.execute_script('
      $("#publisher").removeClass("closed");
      $("#publisher").find("textarea").focus();
    ')
  end

  def expand_first_post
    find(".stream_frame:first .expander").click
    wait_until{ !find(".expander").visible? }
  end

  def first_post_collapsed?
    find(".stream_frame:first .collapsible").should have_css(".expander")
    find(".stream_frame:first .collapsible").has_selector?(".collapsed")
  end

  def first_post_expanded?
    find(".stream_frame:first .expander").should_not be_visible
    find(".stream_frame:first .collapsible").has_no_selector?(".collapsed")
    find(".stream_frame:first .collapsible").has_selector?(".opened")
  end

  def first_post_text
    find(".stream-frame:nth-child(1) .text-content").text()
  end

  def frame_numbers_content(position)
    find(".stream-frame:nth-child(#{position}) .content")
  end

  def find_frame_by_text(text)
    find(".stream-frame:contains('#{text}')")
  end

  def find_post_by_text(text)
    find(".stream-frame:contains('#{text}')")
  end

  def like_post(post_text)
    within_post(post_text) do
      click_link 'Like'
    end
    wait_for_ajax_to_finish
  end

  def within_post(post_text)
    within find_post_by_text(post_text) do
      yield
    end
  end

  def stream_posts
    all('.stream-frame')
  end

  def comment_on_post(post_text, comment_text)
    within_post(post_text) do
      focus_comment_box
      make_comment(comment_text)
    end
    wait_for_ajax_to_finish
  end

  def comment_on_show_page(comment_text)
    within("#post-interactions") do 
      focus_comment_box(".label.comment")
      make_comment(comment_text, "new-comment-text")
    end
    wait_for_ajax_to_finish
  end

  def make_comment(text, elem="text")
    fill_in elem, :with => text
    click_button :submit
  end

  def focus_comment_box(elem="a.focus_comment_textarea")
    find(elem).click
  end

  def wait_for_ajax_to_finish(wait_time=30)
    wait_until(wait_time) do
      evaluate_script("$.active") == 0
    end
  end

  def assert_nsfw(text)
    post = find_post_by_text(text)
    post.find(".nsfw-shield").should be_present
  end
end

World(PublishingCukeHelpers)
