   alias BattleSnake.Snake
   alias BattleSnake.Api
   alias BattleSnake.Move
+  alias BattleSnake.Point
 
   require Logger
 

  defmodule Worker do
    @api Application.get_env(:battle_snake, :snake_api)

    @spec run(BattleSnake.World.t, BattleSnake.Snake.t) :: BattleSnake.Snake.t
    def run(%Snake{} = snake, %World{} = world) do
      response = @api.request_move(snake, world)

      point = response
      |> process_response
      |> Move.to_point

      Snake.move(snake, point)
    end

    def process_response(val, acc \\ [])

      :run,
      [world])
      |> Stream.zip(snakes)
      |> Stream.map(&get_move_for_snake/1)
      |> Stream.map(&move_snake/1)
      |> Enum.to_list

    put_in(world.snakes, snakes)
  end

  defp move_snake({%Point{} = point, %Snake{} = snake}) do
    Snake.move(snake, point)
  end

  defp get_move_for_snake({{:ok, point}, snake}) do
    {point, snake}
  end

  defp get_move_for_snake({{:exit, e}, snake}) do
    Logger.debug """
    [#{snake.url}] failed to respond to /move
    #{inspect e, pretty: true}
