# 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
      allow(MuxLiveStream).to receive(:new).and_return(double(id: 'id', key: 'key', playback_id: 'playback_id'))
    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)

      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 '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
      expect(page).to have_xpath "//input[@readonly][@value='#{broadcast_url(old_token)}']"

      click_link reset_url
      expect(Broadcast.last.token).not_to eq old_token
      expect(page).to have_xpath "//input[@readonly][@value='#{broadcast_url(Broadcast.last.token)}']"
      expect(page).to have_content token_reset_notice
    end

    scenario 'Player will not reload if stream is reactivated while user is watching previous recording', 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 stream_idle_message

      broadcast.streamer_status = :recording
      broadcast.status = :active
      BroadcastsChannel.broadcast_stream_updates(broadcast)

      expect(page).to have_content stream_begun_message
      expect(page).to have_selector('div#broadcast_video', count: 2)

      broadcast.streamer_status = :idle
      broadcast.status = :idle
      BroadcastsChannel.broadcast_stream_updates(broadcast)

      click_on switch_view_dropdown
      click_on recording.download_file_name

      expect(page).to have_content stream_idle_message
      expect(page).to have_selector('div#broadcast_video', count: 1)

      broadcast.streamer_status = :recording
      broadcast.status = :active
      BroadcastsChannel.broadcast_stream_updates(broadcast)

      expect(page).to have_content stream_begun_message
      expect(page).to have_selector('div#broadcast_video', count: 1)
    end

    scenario 'user can go back and forth between live session and previous sessions', 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 broadcast.name.titleize, count: 1
      expect(page).to have_content recording.download_file_name, count: 0

      click_on switch_view_dropdown
      expect(page).to have_content broadcast.name.titleize, count: 2
      expect(page).to have_content recording.download_file_name, count: 1

      live_stream_nav_item = page.find('.dropdown-item', text: broadcast.name.titleize)
      recording_nav_item = page.find('.dropdown-item', text: recording.download_file_name)

      expect(live_stream_nav_item[:class].include?('active')).to eq true
      expect(recording_nav_item[:class].include?('active')).to eq false

      click_on recording.download_file_name

      expect(page).to have_content broadcast.name.titleize, count: 1
      expect(page).to have_content recording.download_file_name, count: 0
      expect(live_stream_nav_item[:class].include?('active')).to eq false
      expect(recording_nav_item[:class].include?('active')).to eq true

      click_on switch_view_dropdown
      click_on broadcast.name.titleize

      expect(page).to have_content broadcast.name.titleize, count: 1
      expect(page).to have_content recording.download_file_name, count: 0

      # Page is reloaded, we need to get dropdown items again
      live_stream_nav_item = page.find('.dropdown-item', text: broadcast.name.titleize, visible: :all)
      recording_nav_item = page.find('.dropdown-item', text: recording.download_file_name, visible: :all)

      expect(live_stream_nav_item[:class].include?('active')).to eq true
      expect(recording_nav_item[:class].include?('active')).to eq false
    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_dropdown

        click_on switch_view_dropdown
        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
    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
    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 switch_view_dropdown
    'Switch View'
  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


end
