require 'rails_helper'

RSpec.feature "EndOfGames", type: :feature, js: true do
  # Given I am in a started game
  before(:each) do
    @game = setup_game
    @game.reload
    @player = @game.controlling_player
    signin(@player.user.email,'password')
    visit table_path @game.table
  end
  # And it is my turn
  # And I play my last cards
  # Then I win the game
  scenario 'I win the game' do
    # Play lowest card
    # Other players always pass
    # Play next lowest card
    # Repeat until out of cards
    while (@player.inventory.count > 0) do
      card = @player.inventory.first
      find_card(card).click

      # Try to click the Play Hand button
      # If this fails, reload the page
      begin
        click_button "Play Hand"
        visit current_url
        @game.reload
      rescue
        @game.reload
        visit current_url
      end

      if @game.controlling_player_id != @player.id
        # Simulate other players passing
        @game.controlling_player_id = @player.id
        @game.save
        @game.reload
      end
    end

    # After I play my last card(s)
    # Expect that I am the winner
    @game.reload
    expect(@game.winner).to eq @player
  end

  describe 'I lose the game' do
    # Another player plays their last card(s)
    # Expect that they are the winner
  end
end
