require 'spec_helper'

# these test will verify all of the user progression cases that rely on a javascript event (not bothering with instrumented models)

describe "User Progression", :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
    @old_recaptcha=Rails.application.config.recaptcha_enable
    Rails.application.config.recaptcha_enable=false
  end

  after(:all) do
    Rails.application.config.recaptcha_enable=@old_recaptcha
  end


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

  describe "downloaded client" do

    before(:each) do
      FactoryGirl.create(:artifact_update, :product => 'JamClient/Win32')
      FactoryGirl.create(:artifact_update, :product => 'JamClient/MacOSX')
    end

    # right after you signup as a musician, you are downloaded to a page that lets you download the client.
    describe "post-signup" do
      before do
        visit signup_path
        fill_in "jam_ruby_user[first_name]",    with: "Mike"
        fill_in "jam_ruby_user[last_name]", with: "Jones"
        fill_in "jam_ruby_user[email]", with: "user_progression1@jamkazam.com"
        fill_in "jam_ruby_user[password]", with: "jam123"
        fill_in "jam_ruby_user[password_confirmation]", with: "jam123"
        check("jam_ruby_user[instruments][drums][selected]")
        check("jam_ruby_user[terms_of_service]")
        click_button "CREATE ACCOUNT"

        should have_content("Congratulations! Your account is ready.")
        detected_os = find("a.current-os-download")['data-platform']
        if detected_os != "Win32"
          find(".download-others a").trigger(:click)
        end
        find("a.current-os-download").trigger(:click)
        sleep 1 # it's really hard to know if the rest API completed after the click
      end

      it {
        User.find_by_email('user_progression1@jamkazam.com').reload();
        User.find_by_email('user_progression1@jamkazam.com').first_downloaded_client_at.should_not be_nil
      }
    end

    describe "downloads-page" do

      # there is also a generic download pages
      before(:each) do
        sign_in_poltergeist user

        visit '/downloads'
        detected_os = find("a.current-os-download")['data-platform']
        if detected_os != "Win32"
          find(".download-others").trigger(:click)
        end
        find("a.current-os-download").trigger(:click)
        sleep 1 # it's really hard to know if the rest API completed after the click
      end

      it {user.reload(); user.first_downloaded_client_at.should_not be_nil }

    end

  end

  describe "certified gear" do
    before(:each) do
      sign_in_poltergeist user
      FactoryGirl.create(:latency_tester)
      visit '/client#/account/audio'
      # step 1 - intro
      find("div.account-audio a[data-purpose='add-profile']").trigger(:click)
      find('.btn-next').trigger(:click)

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

      sleep 1
    end

    it {user.reload(); user.first_certified_gear_at.should_not be_nil }
  end

  describe "social promoted" do

  end
end
