require 'spec_helper'

describe "Launch App", :js => true, :type => :feature, :capybara_feature => true do

  subject { page }

  let(:user) { FactoryGirl.create(:user) }
  let(:ams)  { FactoryGirl.create(:active_music_session, creator: user) }

  before(:each) do
    ActiveMusicSession.delete_all
    MusicSession.delete_all
  end

  shared_examples_for :launch_not_supported do |options|
    it "should indicate not supported" do
      sign_in_poltergeist user
      ams.touch if options[:need_session]
      visit options[:screen_path]
      if options[:actions]
        options[:actions].call(page)
      end
      should have_selector('h1', text: 'Application Notice')
      if options[:gear_modal] && options[:gear_modal] == true
        should have_selector('p', text: 'To configure your audio gear, you must use the JamKazam application. Please download and install the application if you have not done so already.')
      else
        should have_selector('p', text: 'To create or find and join a session, you must use the JamKazam application. Please download and install the application if you have not done so already.')
      end

      find('a.btn-go-to-download-page').trigger(:click)
      find('h2.create-account-header', text: 'Download the free JamKazam app')
    end
  end

  shared_examples_for :launch_supported do |options|
    it "should indicate supported" do
      sign_in_poltergeist user
      ams.touch if options[:need_session]
      visit options[:screen_path]
      if options[:actions]
        options[:actions].call(page)
      end
      should have_selector('h1', text: 'Application Notice')
      if options[:gear_modal] && options[:gear_modal] == true
        should have_selector('p', text: 'To configure your audio gear, you must use the JamKazam application.')
      else
        should have_selector('p', text: 'To create or find and join a session, you must use the JamKazam application.')
      end
      should have_selector('.btn-launch-app')
      find('.btn-launch-app')['href'].start_with?('jamkazam:')
    end
  end

  describe "unsupported" do

    before do
      # emulate mac safari
      page.driver.headers = { 'User-Agent' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.74.9 (KHTML, like Gecko) Version/7.0.2 Safari/537.74.9'}
    end
    it_behaves_like :launch_not_supported, screen_path: '/client#/createSession', actions: lambda { |page| page.find('li[create-type="quick-start"] ins').trigger(:click); page.find('.btn-next').trigger(:click) }
    it_behaves_like :launch_not_supported, screen_path: '/client#/createSession', actions: lambda { |page| page.find('li[create-type="immediately"] ins').trigger(:click); page.find('.btn-next').trigger(:click) }
    it_behaves_like :launch_not_supported, screen_path: '/client#/createSession', actions: lambda { |page| page.find('li[create-type="start-scheduled"] ins').trigger(:click); page.find('.btn-next').trigger(:click) }
    it_behaves_like :launch_not_supported, screen_path: '/client#/findSession',  actions: lambda { |page| page.find('.join-link').trigger(:click) }, need_session: true
    it_behaves_like :launch_not_supported, screen_path: '/client#/account/audio', gear_modal: true
  end

  describe "supported" do
    before do
      # emulate chrome
      page.driver.headers = { 'User-Agent' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.152 Safari/537.36'}
    end
    it_behaves_like :launch_supported, screen_path: '/client#/createSession', actions: lambda { |page| page.find('li[create-type="quick-start"] ins').trigger(:click); page.find('.btn-next').trigger(:click) }
    it_behaves_like :launch_supported, screen_path: '/client#/createSession', actions: lambda { |page| page.find('li[create-type="immediately"] ins').trigger(:click); page.find('.btn-next').trigger(:click) }
    it_behaves_like :launch_supported, screen_path: '/client#/createSession', actions: lambda { |page| page.find('li[create-type="start-scheduled"] ins').trigger(:click); page.find('.btn-next').trigger(:click) }
    it_behaves_like :launch_supported, screen_path: '/client#/findSession',  actions: lambda { |page| page.find('.join-link').trigger(:click) }, need_session: true
    it_behaves_like :launch_supported, screen_path: '/client#/account/audio', gear_modal: true
  end
end


