require "rails_helper"

feature "User managing broadcasts" do
  let(:current_user) { create(:user, :manager) }
  let(:project) { create(:project, members: current_user, account: current_user.primary_account) }

  context "managing broadcasts" do
    before do
      sign_in current_user
      allow(MuxLiveStream).to receive(:new).and_return(double(id: "id", key: "key", playback_id: "playback_id"))
    end

    scenario "creating and deleting a broadcast", js: true do
      visit new_project_broadcast_path(project)

      by "filling out the form" do
        fill_in broadcast_name_field, with: "My Broadcast"
      end

      click_button "Create Live Stream"
      expect(page).to have_content("A live stream has been created")
      click_on "Manage"
      expect(page).to have_link("Copy Stream URL", exact: true)
      expect(page).to have_link("Copy Stream Key", exact: true)
      expect(page).to have_link("View", exact: true)
      expect(page).to have_link("Delete", exact: true)

      it_also "Deletes the broadcast" do
        allow_any_instance_of(Broadcast).to receive(:destroy_mux_live_stream).and_return(true)

        accept_alert do
          click_link "Delete"
        end
        
        expect(page).to have_content("A live stream has been deleted")
        expect(page).not_to have_content("My Broadcast")
      end
    end

    scenario "visit show page of broadcast", js: true do
      broadcast = create(:broadcast, :with_stream, :with_files, project: project)
      recording = create(:broadcast_recording, broadcast: broadcast)

      visit project_broadcast_path(project, broadcast)

      expect(page).to have_content("Live stream is waiting to begin.")
      expect(page).to have_content("Copy URL")

      within "#files" do
        expect(page).to have_content("contract.pdf")
      end

      click_on "Previous Sessions"
      expect(page).to have_content(recording.download_file_name)
    end

    scenario "form will not submit if user clicks Add files without selected files", js: true do
      broadcast = create(:broadcast, :with_stream, :with_files, project: project)

      visit project_broadcast_path(project, broadcast)

      expect(page).to have_content("Live stream is waiting to begin.")
      expect(page).to have_content add_file_button

      click_on add_file_button
    end

    scenario "visit multi-view broadcast page", js: true do
      broadcast_one = create(:broadcast, :with_stream, :with_files, name: "Broadcast 1", project: project)
      broadcast_two = create(:broadcast, :with_stream, :with_files, name: "Broadcast 2", project: project)
      
      visit project_broadcasts_path(project)
      click_checkboxes
      
      new_window = window_opened_by { click_link "Multi-View" }
      within_window new_window do
        expect(page).to have_content("Switch View")

        click_on "Switch View"
        expect(page).to have_link("Broadcast 1")
        expect(page).to have_link("Broadcast 2")

        within "#files" do
          click_on "Broadcast 1"
          expect(page).to have_content("contract.pdf")

          click_on "Broadcast 2"
          expect(page).to have_content("contract.pdf")
        end
      end 
    end
  end


  private

  def add_file_button
    'Add File'
  end

  def broadcast_name_field
    "broadcast[name]"
  end

  def click_checkboxes
    all('input[type="checkbox"]')[0].click
    all('input[type="checkbox"]')[1].click
  end
end
