# Feature: Seats
#   As a visitor
#   I want to sit in a seat
#   So I can be at a table and play a game
feature 'Seats', :devise, js: true do
  context "As a visitor" do
    before(:each) do
      @table = FactoryGirl.create :table
      visit table_path @table
    end

    it 'visitor can sit in an empty seat' do
      click_button 'Sit', match: :first
      expect(page).to have_content "Guest"
    end

    it 'sit button is disabled for unavailable seats' do
      @button = page.find_button('Sit', match: :first)
      @button.click
      expect(page).to have_content "Guest"
      expect(@button.disabled?).to eq true
    end

    it 'has a stand button if current user is seated' do
      @button = page.find_button('Sit', match: :first)
      @button.click
      expect(page).to have_content "Guest"
      @buttons = page.find_all('button', text: 'Stand')
      expect(@buttons.length).to eq 1
    end

    scenario 'table seats match up with game seats' do
      click_button 'Sit', match: :first
      expect(page).to have_content "Guest"
      expect(page.find(:css, ".game-seat.position-1")).to have_content "Guest"
      click_button 'Sit', match: :first
      expect(page).to have_content "Guest"
      expect(page.find(:css, ".game-seat.position-2")).to have_content "Guest"
    end

    #TODO: User can only occupy one seat per table
    scenario 'visitor changes seats' do
      click_button 'Sit', match: :first
      expect(page).to have_content "Guest"
      expect(page.find(:css, ".game-seat.position-1")).to have_content "Guest"
      click_button 'Sit', match: :first
      expect(page).to have_content "Guest"
      expect(page.find(:css, ".game-seat.position-2")).to have_content "Guest"
      expect(page.text.scan('Guest').count).to eq 1
    end

    scenario 'visitor can join the current game' do
      click_button 'Sit', match: :first
      sleep 3
      expect(page).to have_content "Guest"
      expect(page.text.scan('Guest').count).to eq 1
    end

    def sit_buttons
      find_all(:button, "Sit")
    end

    scenario 'visitor can leave the current game' do
      click_button 'Sit', match: :first
      expect(page).to have_content 'Guest'
      expect(page.text.scan('Guest').count).to eq 1
      click_button 'Stand', match: :first
      expect(page.find('.game-seat.position-1')).to have_content 'Sit'
      expect(sit_buttons.length).to eq 4
      expect(page.text.scan('Guest').count).to eq 0
    end

  end

  context "As a user" do
    before(:each) do
      @table = FactoryGirl.create :table
      @user = FactoryGirl.create :user
      signin(@user.email, @user.password)
      visit table_path @table
    end

    scenario 'user can sit at the table' do
      #TODO: Fix race condition
      expect(page).to have_content "Sit"
      click_button 'Sit', match: :first
      expect(page).to have_content @user.email
    end

    scenario 'user can join the current game' do
      expect(page).to have_content "Sit"
      click_button 'Sit', match: :first
      expect(page).to have_content @user.email
      expect(page.text.scan(@user.email).count).to eq 1
    end

    scenario 'user can leave the current game' do
      expect(page).to have_content "Sit"
      click_button 'Sit', match: :first
      expect(page).to have_content @user.email
      expect(page.text.scan(@user.email).count).to eq 1
      click_button 'Stand', match: :first
      expect(page).to have_content "Sit"
      sleep 1
      expect(page.text.scan(@user.email).count).to eq 0
    end

  end
end
