require 'spec_helper'

describe "Home Page", :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

  before(:each) do
    Feed.delete_all
    MusicSessionUserHistory.delete_all
    MusicSession.delete_all
    Recording.delete_all

    emulate_client
    visit "/"
    find('h1', text: 'Live music platform & social network for musicians')

  end

  let(:user) { FactoryGirl.create(:user) }
  let(:fb_auth) {
  {     :provider => "facebook",
        :uid      => "1234",
        :info   => {:name       => "John Doe",
                    :email      => "johndoe@email.com"},
        :credentials => {:token => "testtoken234tsdf", :expires_at => 2391456019},
        :extra => { :raw_info => {:first_name => 'John', :last_name => 'Doe', :email => 'facebook@jamkazam.com', :gender => 'male'}} }
  }

  it "links work" do

    # learn more about JamTracks
    find('.learn-more-jamtracks').trigger(:click)
    find('h1.product-headline', text: 'JamTracks by JamKazam')

    visit '/'

    # learn more about the platform
    find('.learn-more-platform').trigger(:click)
    find('h1.product-headline', text: 'The JamKazam Platform')

    visit '/'

    # learn more about the platform
    find('.learn-more-jamblaster').trigger(:click)
    find('h1.product-headline', text: 'The JamBlaster by JamKazam')

    visit '/'

    # try to sign in
    find('a.sign-in').trigger(:click)
    find('h1', text: 'sign in or register')

    visit '/'

    # try to click jamtrack CTA button
    find('a.cta-button.jamtracks').trigger(:click)
    find('h1', text: 'jamtracks')

    visit '/'

    # try to click platform CTA button
    find('a.cta-button.platform').trigger(:click)
    find('h2', text: '1Create your free JamKazam account')

    visit '/'

    # try to click jamblaster CTA button
    find('a.cta-button.jamblaster').trigger(:click)
    find('h1.product-headline', text: 'The JamBlaster by JamKazam')

  end

  it "signed in user gets redirected to app home page" do
    fast_signin(user,'/')
    find('h2', text: 'create session')
  end


  describe "feed" do

    it "data" do
      claimedRecording1 = FactoryGirl.create(:claimed_recording)
      MusicSession1 = claimedRecording1.recording.music_session.music_session

      visit "/"
      find('h1', text: 'Live music platform & social network for musicians')
      find('.feed-entry.music-session-history-entry .description', text: MusicSession1.description)
      find('.feed-entry.music-session-history-entry .session-status', text: 'BROADCASTING OFFLINE')
      find('.feed-entry.music-session-history-entry .session-controls.inprogress', text: 'BROADCASTING OFFLINE')
      find('.feed-entry.music-session-history-entry .artist', text: MusicSession1.creator.name)
      should_not have_selector('.feed-entry.music-session-history-entry .musician-detail')

      find('.feed-entry.recording-entry .name', text: claimedRecording1.name)
      find('.feed-entry.recording-entry .description', text: claimedRecording1.description)
      find('.feed-entry.recording-entry .title', text: 'RECORDING')
      find('.feed-entry.recording-entry .artist', text: claimedRecording1.user.name)
      should_not have_selector('.feed-entry.recording-entry .musician-detail')

      # try to hide the recording
      claimedRecording1.is_public = false
      claimedRecording1.save!

      visit "/"
      find('h1', text: 'Live music platform & social network for musicians')
      find('.feed-entry.music-session-history-entry .description', text: MusicSession1.description)
      should_not have_selector('.feed-entry.recording-entry')


      # try to hide the music session
      MusicSession1.fan_access = false
      MusicSession1.save!

      visit "/"
      find('h1', text: 'Live music platform & social network for musicians')
      should have_selector('.feed-entry.music-session-history-entry')

      # try to mess with the music session history by removing all user histories (which makes it a bit invalid)
      # but we really don't want the front page to ever crash if we can help it
      MusicSession1.fan_access = true
      MusicSession1.music_session_user_histories.delete_all
      MusicSession1.reload
      MusicSession1.music_session_user_histories.length.should == 0

      visit "/"
      find('h1', text: 'Live music platform & social network for musicians')
      should_not have_selector('.feed-entry.music-session-history-entry')
    end
  end


=begin
    describe "signin with facebook" do

      before(:each) do
        user.user_authorizations.build provider: 'facebook', uid: '1234', token: 'abc', token_expiration: 1.days.from_now
        user.save!
        OmniAuth.config.mock_auth[:facebook] = OmniAuth::AuthHash.new(fb_auth)
      end

      it "click will redirect to facebook for authorization" do
        pending "move elsewhere"

        find('.signin-facebook').trigger(:click)

        wait_until_curtain_gone

        find('h2', text: 'musicians')
      end
    end
=end
end

