# Feature: Play now
#   As a visitor
#   I want to quickly join a game
#   So I can play
feature 'Play now', :devise, js: true do
  # Scenario: Visitor can play now
  #   Given I am not signed in
  #   When I click Play Now
  #   Then I join a game
  describe 'visitor can Play Now' do
    context 'there are no joinable games' do
      before do
        expect(Game.all.count).to eq 0
        visit root_path
        click_link 'Play Now'
      end
      it 'creates and joins a new game' do
        #TODO: Fix race condition
        expect(page).to have_content "Hand to beat"
        #Game title derived from visitor soft_token
        expect(page).to have_content "#{Game.last.players.first.soft_token[0..4]}"
        #Game count increased by 1
        expect(Game.all.count).to eq 1
      end
    end

    context 'visitor joins an existing game' do
      before do
        FactoryGirl.create :game
        @game = Game.last
        visit root_path
        expect(Game.all.count).to eq 1
        click_link 'Play Now'
      end

      it 'shows and starts the game' do
        #TODO: Deprecated it shows the game title
        #expect(page).to have_content "Test Game"
        expect(page).to have_content "Hand to beat"
        #it 'is located at the correct url' do
          expect(current_path).to eq game_path(@game)

        #it 'shows the visitor as a player' do
          @player = @game.players.last
          @playerComponent = page.find("#player-1")
          #TODO: have_content @player.display_name
          expect(@playerComponent.text).to have_content("Guest#{@player.soft_token[0..5]}")

        #it 'shows other players' do
          # Add another player
          @user2 = FactoryGirl.create :user
          @seats = @game.table.seats.map { |s| s.occupied? ? nil : s}
          @seats.compact.first.sit(@user2)
          #TODO: Remove race condition
          visit current_path
          expect(page).to have_content @user2.display_name
        #it 'the game is startable' do
          click_button 'Start'
          #TODO: Remove race condition
          visit current_path
          visit current_path
          visit current_path
          visit current_path
          expect(page).to have_content 'Inventory'
          expect(Game.all.count).to eq 1
      end
    end
  end
end
