require 'rails_helper'

RSpec.feature "EndOfGames", type: :feature do
  # Given I am in a started game
  before(:all) do
    # If test is unable to click correct card, use wider window
    # page.driver.browser.manage.window.resize_to(1640, 1090)
    @game = setup_game
    @game.reload
    @player = @game.controlling_player
    signin(@player.user.email,'password')
    visit table_path @game.table

    # 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
  end

  # And it is my turn
  # And I play my last cards
  # Then I win the game
  scenario 'I win the game' do
    # After I play my last card(s)
    # Expect that I am the winner
    @game.reload
    expect(@game.winner).to eq @player
    click_button "New Game"
    expect(page).to have_content "Game started"
  end

  scenario 'I start a new game at the same table' do
    click_button "New Game"
    expect(page).to have_content "Game started"
  end

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

  describe 'I win 2nd place' do
    pending
    # Another player plays their last card(s)
    # Expect that they are the winner
  end

  describe 'Play to beat clears appropriately' do
    pending
    # Another player plays their last card(s)
    # Expect that they are the winner
  end
end
