# 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 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
      @sit_buttons = page.find_all('button', text: 'Sit')
      expect(@sit_buttons.length).to eq 4
      @button = page.find_button('Sit', match: :first)
      @button.click
      expect(page).to have_content "Guest"
      @sit_buttons = page.find_all('button', text: 'Sit')
      expect(@sit_buttons.length).to eq 3
    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: 'Leave')
      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, ".seat.position-1 .player")).to have_content "Guest"
      click_button 'Sit', match: :first
      expect(page).to have_content "Guest"
      expect(page.find(:css, ".seat.position-4 .player")).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, ".seat.position-1")).to have_content "Guest"
      click_button 'Sit', match: :first
      expect(page).to have_content "Guest"
      expect(page.find(:css, ".seat.position-4")).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
      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', js: true do
    scenario 'seated 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
      # accept_confirm do
        click_button 'Leave', match: :first
      # end
      expect(page.find('.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
      click_button 'Sit', match: :first
      expect(page).to have_content @user.email
    end

    scenario 'user can join the current game' do
      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', js: true do
    scenario 'user can leave the current game' do
      click_button 'Sit', match: :first
      expect(page).to have_content @user.email
      expect(page.text.scan(@user.email).count).to eq 1
      # accept_confirm do
        click_button 'Leave', match: :first
      # end
      expect(page.text.scan(@user.email).count).to eq 0
    end

  end
end
