require "rails_helper"

feature "User managing videos" do
  let(:current_user) { create(:user) }
  let(:project) { create(:project, account: current_user.primary_account) }
  let!(:video) do
    create(:video,
           :with_graphics_only_edl_file,
           :with_audio_only_edl_file,
           project: project,
           name: "Star Wars",
           number: "1",
           video_editing_system: "adobe_premiere",)
  end

  before :each do
    sign_in current_user
  end

  scenario "creating a video", js: true do
    visit project_videos_path(project)
    click_link "Upload New Video"

    expect(page).to have_content "Upload Video"
    expect(page).to have_link("Avid")
    expect(page).to have_link("Adobe Premiere")

    click_link "Adobe Premiere"

    expect(page).to have_content "Upload Video"
    expect(page).to have_selector "#video_file", visible: :all
    expect(page).to have_selector "#video_edl_file", visible: :all
    expect(page).to have_selector "#video_graphics_only_edl_file", visible: :all
    expect(page).to have_selector "#video_audio_only_edl_file", visible: :all
    expect(page).to have_link "click here", href: /mailto:info@bigmedia\.ai/

    fill_in_video_fields name: "New name", number: "99"

    # TODO: Figure out how to test video gets created and shows up in project page
  end

  scenario "editing a video", js: true do
    create(:video,
           :with_graphics_only_edl_file,
           :with_audio_only_edl_file,
           project: project,
           name: "Star Wars",
           number: "1",
           video_editing_system: "adobe_premiere",)
    video_data = {
      name: "Star Wars",
      number: "1",
    }

    visit project_videos_path(project)
    all('a', :text => /Edit/)[1].click

    within "#current-edl-file" do
      expect(page).to have_content("sample-edl.edl")
    end

    within "#current-graphics-only-edl-file" do
      expect(page).to have_content("sample-edl.edl")
    end

    within "#current-audio-only-edl-file" do
      expect(page).to have_content("sample-edl.edl")
    end

    expect(page).to have_filled_in_data(video_data)

    fill_in_video_fields name: "New name", number: "99"

    accept_alert do
      click_button "Save Changes"
    end

    expect(page).to have_content(update_video_notice)
  end

  scenario "searching for a video", js: true do
    create(:video, project: project, name: "First Video")
    create(:video, project: project, name: "Second Video")

    visit project_videos_path(project)

    fill_in "query", with: "First"
    click_on "search-button"

    expect(page).to have_content("First Video")
    expect(page).not_to have_content("Second Video")

    fill_in "query", with: "Second"
    click_on "search-button"
    expect(page).not_to have_content("First Video")
    expect(page).to have_content("Second Video")
  end

  private

  def fill_in_video_fields(data)
    fill_in "video[name]", with: data[:name]
    fill_in "video[number]", with: data[:number]
  end

  def have_filled_in_data(data)
    have_selector("#video_name[value='#{data[:name]}']")
    have_selector("#video_number[value='#{data[:number]}']")
  end

  def create_video_notice
    t "videos.update.notice"
  end

  def update_video_notice
    t "videos.update.notice"
  end
end
