require 'spec_helper'

describe "Gear Wizard", :js => true, :type => :feature, :capybara_feature => true do

  subject { page }

  let(:user) { FactoryGirl.create(:user) }

  before(:each) do
    emulate_client
    LatencyTester.delete_all
  end

  before(:all) do
    @old_video_available=Rails.application.config.video_available    
  end

  after(:all) do
    Rails.application.config.video_available=@old_video_available
  end

  def walk_wizard(has_video=false)
    FactoryGirl.create(:latency_tester)
    fast_signin user, '/client#/account/audio'
    find("div.account-audio a[data-purpose='add-profile']").trigger(:click)
    
    # step 1 - intro
    find('.btn-next').trigger(:click)

    # step 2 - select gear
    find('.ftue-step-title', text: 'Select & Test Audio Gear')
    should_not have_selector('.resync-status') # when you enter this step,
    jk_select('Built-in', 'div[layout-wizard-step="1"] select.select-audio-input-device')
    find('.btn-next.button-orange:not(.disabled)').trigger(:click)

    # step 3 - configure tracks
    find('.ftue-step-title', text: 'Configure Tracks')

    # drag one input over to tracks area http://rubydoc.info/github/jnicklas/capybara/master/Capybara/Node/Element#drag_to-instance_method
    input = first('.ftue-input')
    track_slot = first('.track-target')
    input.drag_to(track_slot)

    find('.btn-next.button-orange:not(.disabled)').trigger(:click)

    # step 4 - configure voice chat
    find('.ftue-step-title', text: 'Configure Voice Chat')
    find('.btn-next.button-orange:not(.disabled)').trigger(:click)

    # step 5 - configure direct monitoring
    find('.ftue-step-title', text: 'Turn Off Direct Monitoring')

    # make a diversion into the 'adjust gear speed' dialog
    find('.adjust-settings-direct-monitor').trigger(:click)
    # should see dialog header
    find('h1', text: 'Adjust Gear Speed')
    # change to 'moderate' speed
    find('.speed-option.setting-fair ins').trigger(:click)
    # should cause a spinner/io test, and then save button comes up as clickable
    find('.btnSave.button-orange:not(.disabled)').trigger(:click)

    find('.btn-next.button-orange:not(.disabled)').trigger(:click)

    # Optional step 6 - configure video
    if(has_video)
      #find('.ftue-step-title', text: 'Select Video Gear')
      find('.btn-next.button-orange:not(.disabled)').trigger(:click)
    end

    # step 6 - Test Router & Network
    find('.ftue-step-title', text: 'Test Router & Network')
    find('.button-orange.start-network-test').trigger(:click)
    find('.user-btn', text: 'RUN NETWORK TEST ANYWAY').trigger(:click)
    find('.button-orange.start-network-test')
    find('.btn-next.button-orange:not(.disabled)').trigger(:click)

    # step 7 - Success
    find('.ftue-step-title', text: 'Success!')
    find('.btn-close.button-orange').trigger(:click)

    # should see prompt afterwards about joining a test session
    find('h1', text: 'join test session')
    find('.join-test-session').trigger(:click)

    # and should now be in session
    find('h2', text: 'my live tracks')
  end

  it "success path" do
    Rails.application.config.video_available="none"
    walk_wizard(false)
  end

  it "success path with video" do
    Rails.application.config.video_available="mvp"
    walk_wizard(true)
  end
end

