require 'spec_helper'

describe "Home Screen", :js => true, :type => :feature, :capybara_feature => true  do

  subject { page }

  before(:all) do
    Capybara.javascript_driver = :poltergeist
    Capybara.current_driver = Capybara.javascript_driver
    Capybara.default_max_wait_time = 10
  end

  let(:user) { FactoryGirl.create(:user, show_whats_next: true, has_redeemable_jamtrack: true) }

  describe "in normal browser" do
    before(:each) do
      sign_in_poltergeist user
      visit "/client"
      should have_selector('h1',    text: 'getting started')
    end

    it "should show launch app dialog if clicked setup gear" do
      find('#getting-started-dialog .setup-gear-btn').trigger('click')
      should have_selector('p', text: 'To configure your audio gear, you must use the JamKazam application.')
    end

    it "should show jam track browsing page" do
      find('#getting-started-dialog span.jamtracks-limited-time')
      find('#getting-started-dialog h2.get-a-free-jamtrack')
      expect(page).to have_selector('#getting-started-dialog .browse-jamtracks', visible: false)

      find('#getting-started-dialog a.browse-jamtrack').trigger('click')
      should have_selector('h1', text: 'jamtracks')
    end

  end

  describe "in normal browser with redeemed jamtrack" do
    let(:redeemed_user) { FactoryGirl.create(:user, show_whats_next: true, has_redeemable_jamtrack: false) }

    before(:each) do
      sign_in_poltergeist redeemed_user
      visit "/client"
      should have_selector('h1',    text: 'getting started')
    end

    it "should show jam track browsing page" do
      find('#getting-started-dialog h2.browse-jamtracks')
      expect(page).to have_selector('#getting-started-dialog h2.get-a-free-jamtrack', visible: false)
      expect(page).to have_selector('#getting-started-dialog span.jamtracks-limited-time', visible: false)

      find('#getting-started-dialog .browse-jamtrack').trigger('click')
      should have_selector('h1', text: 'jamtracks')
    end

  end

  describe "in native client" do

    before(:each) do
      sign_in_poltergeist user
      emulate_client
      visit "/client"
      should have_selector('h1',    text: 'getting started')
    end

    describe "new user in the native view should see the getting started dialog" do

      it "should show gear page if clicked setup gear" do
        find('#getting-started-dialog .setup-gear-btn').trigger('click')
        should have_selector('.ftue-step-title', text: 'Understand Your Gear')
      end

      describe "open invitation dialog for email" do
        before(:each) do
          find('#getting-started-dialog .email-invite').trigger(:click)
        end

        it {should have_selector('label',    text: 'Enter email address(es). If multiple addresses, separate with commas.')}
      end


      describe "launches youtube tutorial site" do

        it {
          find("#getting-started-dialog a[purpose='youtube-tutorials']")['href'].should eq('https://www.youtube.com/channel/UC38nc9MMZgExJAd7ca3rkUA')
        }
      end


      describe "close hides the screen" do

        it {
          find('#getting-started-dialog a[layout-action="close"]').trigger(:click)
          should have_no_selector('h1',    text: 'getting started')
        }
      end

      describe "user can make prompt go away forever" do

        it {
          find('#getting-started-dialog ins.iCheck-helper').trigger(:click)
          find('#getting-started-dialog a[layout-action="close"]').trigger(:click)

          # needed because we poke the server with an updateUser call, but their is no indication in the UI that it's done
          wait_for_ajax
          emulate_client
          sleep 1
          visit "/client"
          wait_until_curtain_gone
          should_not have_selector('h1',    text: 'getting started')
        }
      end

    end
  end
end

