require "rails_helper"

feature "User managing task requests" do
  let(:current_user) { create(:user) }
  let(:project) { create(:project, account: current_user.primary_account) }

  before :each do
    sign_in current_user
  end

  scenario "task requests table is visible" do
    visit project_task_requests_path(project)

    expect(page).to have_content "Created On"
    expect(page).to have_content "Deadline"
    expect(page).to have_content description_column
    expect(page).to have_content "Time Allowed"
    expect(page).to have_content "Status"
  end

  scenario "sees list of task requests" do
    visit project_task_requests_path(project)

    expect(page).to have_content no_task_requests_label

    task_request = create(:task_request, project: project)

    visit project_task_requests_path(project)

    expect(page).not_to have_content no_task_requests_label

    expect(page).to have_content task_request.created_at.try(:strftime, '%D')
    expect(page).to have_content task_request.deadline.try(:strftime, '%D')
    expect(page).to have_content task_request.time_allowed
    expect(page).to have_content task_request.status.capitalize
  end

  scenario "full description is shown if description text is not truncated" do
    visit project_task_requests_path(project)

    task_request = create(:task_request, project: project, description: 'Short Desc')
    visit project_task_requests_path(project)

    expect(page).to have_content task_request.description
  end

  scenario "truncated description is shown if description text is too long" do
    visit project_task_requests_path(project)

    task_request = create(:task_request,
                          project: project,
                          description: long_description_text)
    visit project_task_requests_path(project)

    expect(page).not_to have_content task_request.description
    truncated_text = "#{task_request.description[0..26]}...read more"
    expect(page).to have_content truncated_text
  end

  scenario "full description is shown when user hovers over read more link in description colum", js: true do
    visit project_task_requests_path(project)

    task_request = create(:task_request,
                          project: project,
                          description: long_description_text)
    visit project_task_requests_path(project)

    page.execute_script '$("a[data-toggle=popover]").trigger("mouseover")'
    expect(page).to have_content task_request.description
  end

  scenario "user does not see open deliverable action in manage dropdown if task status is not completed" do
    task_request = create(:task_request, project: project)
    visit project_task_requests_path(project)

    click_on manage_button
    expect(page).not_to have_content open_deliverable_action_label
  end

  scenario "user can click deliverable action in manage dropdown if task status is completed", js: true do
    create(:task_request, project: project, status: "completed", deliverable_url: "/")
    visit project_task_requests_path(project)

    click_on manage_button
    expect(page).to have_content open_deliverable_action_label
    click_link open_deliverable_action_label
    switch_to_window(windows.last)
    expect(page).to have_content add_new_project_label
  end

  private

  def no_task_requests_label
    "Task requests will appear here"
  end

  def manage_button
    t "task_requests.task_request.actions.manage"
  end

  def open_deliverable_action_label
    t "task_requests.task_request.actions.open_deliverable"
  end

  def add_new_project_label
    t "projects.index.actions.new"
  end

  def description_column
    t 'task_requests.index.table_headers.task_request_description'
  end

  def long_description_text
    'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.'
  end
end
