package com.mazebert.gateways.fake;

import com.mazebert.entities.Game;
import com.mazebert.gateways.GameGateway;

import java.util.List;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.stream.Collectors;

public class FakeGameGateway implements GameGateway {
    private final ConcurrentMap<UUID, Game> byId = new ConcurrentHashMap<>();

    @Override
    public void add(Game game) {
        byId.put(game.id, game);
    }

    @Override
    public Game findById(UUID id) {
        return byId.get(id);
    }

    @Override
    public List<Game> getGames() {
        return byId.values().stream().sorted((o1, o2) -> Long.compare(o2.created, o1.created)).collect(Collectors.toList());
    }
}
