package me.km.playerbank;

import java.util.HashMap;
import java.util.UUID;

public class DummyPlayerBank implements IPlayerBank {
    private final HashMap<UUID, String> uuidToName = new HashMap<>();
    private final HashMap<UUID, Integer> uuidToId = new HashMap<>();
    private final HashMap<Integer, UUID> idToUuid = new HashMap<>();
    private int id = 0;

    @Override
    public boolean add(UUID uuid, String name) {
        if(uuidToName.put(uuid, name) == null) {
            int c = id++;
            uuidToId.put(uuid, c);
            idToUuid.put(c, uuid);
        }
        return true;
    }

    @Override
    public void changeName(UUID uuid, String name) {
        uuidToName.put(uuid, name);
    }

    @Override
    public int getId(UUID uuid) {
        return uuidToId.getOrDefault(uuid, -1);
    }

    @Override
    public UUID getUUIDfromID(int id) {
        return idToUuid.getOrDefault(id, null);
    }

    @Override
    public String getNamefromID(int id) {
        UUID uuid = idToUuid.get(id);
        if(uuid == null) {
            return null;
        }
        return uuidToName.getOrDefault(uuid, "");
    }

    @Override
    public UUID getUUID(String name) {
        return uuidToName.entrySet().stream().filter(e -> name.equals(e.getValue())).map(e -> e.getKey()).findFirst().orElse(null);
    }
}
