# frozen_string_literal: true

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
      stub_mux_live_stream
    end

    scenario "splash page is shown if there are no existing streams" do
      visit project_broadcasts_path(project)

      expect(page).to have_content schedule_demo
      expect(page).to have_content create_stream
    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'
        select_time_zone("New Delhi")
      end

      click_button 'Create Live Stream'
      expect(page).to have_content('A live stream has been created')
      expect(page).to have_content('New Delhi')
      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, asset_uid: "asset_uid_1")

      visit project_broadcast_path(project, broadcast)

      expect(page).to have_content('Live stream is waiting to begin.')
      expect(page).to have_content('Share URL')

      click_on "Files"
      expect(page).to have_content('contract.pdf')

      click_on 'Takes'
      expect(page).to have_content(recording.name)
    end

    context 'visit show page of active broadcast' do
      scenario 'loads full live stream playback url if available' do
        broadcast = create(:broadcast, :with_stream, :with_files, project: project, streamer_status: :recording, status: :active)

        visit project_broadcast_path(project, broadcast)

        expect(page.body).not_to match broadcast.stream_playback_url
        expect(page.body).to match broadcast.full_live_stream_playback_url
      end

      scenario 'loads full broadcast asset url if available' do
        broadcast = create(:broadcast, :with_stream, :with_files, project: project, streamer_status: :recording, status: :active, full_live_stream_playback_uid: '')

        visit project_broadcast_path(project, broadcast)

        expect(page.body).to match broadcast.stream_playback_url
      end
    end

    scenario 'Clicking Reset URL regenerates broadcast token' do
      broadcast = create(:broadcast, :with_stream, :with_files, project: project)
      old_token = broadcast.token

      visit project_broadcast_path(project, broadcast)

      expect(page).to have_content reset_url

      click_link reset_url
      expect(Broadcast.last.token).not_to eq old_token
      expect(page).to have_content token_reset_notice
    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.')
      click_on "Files"
      expect(page).to have_content add_file_button

      click_on add_file_button
    end

    scenario 'manager user can click delete button next to the file and delete file', js: true do
      broadcast = create(:broadcast, :with_stream, :with_files, project: project)

      visit project_broadcast_path(project, broadcast)
      click_on "Files"
      expect(page).to have_content delete_file_button, count: 3

      accept_alert do
        first('a', text: delete_file_button).click
      end

      expect(page).to have_content delete_file_button, count: 2
      expect(Broadcast.find(broadcast.id).files.count).to eq 2
    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
        click_on "Files"
        expect(page).to have_content broadcast_one.name
        expect(page).to have_content broadcast_two.name
        
        click_on broadcast_one.name
        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

    context 'When the user is associate' do
      let(:current_user) { create(:user, :associate) }

      it 'does not show button to create new live stream' do
        visit project_broadcasts_path(project)

        expect(page).to have_content schedule_demo
        expect(page).not_to have_content create_stream
      end

      scenario 'associate user does not see delete button next to the file', js: true do
        broadcast = create(:broadcast, :with_stream, :with_files, project: project)

        visit project_broadcast_path(project, broadcast)

        expect(page).to have_content delete_file_button, count: 0
      end
    end

    context 'When the user is account manager' do
      let(:current_user) { create(:user, :account_manager) }

      it 'does show button to create new live stream' do
        visit project_broadcasts_path(project)

        expect(page).to have_content schedule_demo
        expect(page).to have_content create_stream
      end

      scenario 'account manager user can click delete button next to the file and delete file', js: true do
        broadcast = create(:broadcast, :with_stream, :with_files, project: project)

        visit project_broadcast_path(project, broadcast)
        click_on "Files"
        expect(page).to have_content delete_file_button, count: 3

        accept_alert do
          first('a', text: delete_file_button).click
        end

        expect(page).to have_content delete_file_button, count: 2
        expect(Broadcast.find(broadcast.id).files.count).to eq 2
      end
    end
  end

  private

  def add_file_button
    'Add File'
  end

  def broadcast_name_field
    'broadcast[name]'
  end
  
  def select_time_zone(value)
    if value.present?
      select value, from: "broadcast[shoot_location_time_zone]"
    end
  end

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

  def schedule_demo
    t 'broadcasts.splash.actions.book_demo'
  end

  def create_stream
    t 'broadcasts.splash.actions.create_stream'
  end

  def reset_url
    t 'broadcasts.show.actions.reset_url'
  end

  def token_reset_notice
    t 'broadcasts.update.reset_notice'
  end

  def stream_begun_message
    'Live stream has begun, click play to watch it'
  end

  def stream_idle_message
    'Live stream is waiting to begin'
  end

  def delete_file_button
    t 'broadcasts.file.actions.delete_file'
  end


end
