package com.insa.app.server;

import com.insa.app.model.User;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

public class ServerDatabase {

    private ArrayList<User> clients;

    public ServerDatabase() {
        this.clients = new ArrayList<>();
    }

    public List<User> getConnectedClients() {
        return (List<User>) clients.clone();
    }

    /** Append the user to the list of client, updating it if it is already present **/
    public void addClient(User user) {
        if (!clients.contains(user)) {
            Logger.getAnonymousLogger().log(Level.INFO, user + " logged in for the first time");
            clients.add(user);
        }
        else {
            Logger.getAnonymousLogger().log(Level.INFO, user + " is back");
            clients.remove(user);
            clients.add(user);
        }
    }

    /** Remove the user from the list of clients **/
    public void disconnectClient(User user) {
        if (!clients.remove(user)) {
            Logger.getAnonymousLogger().log(Level.WARNING, "Failed to remove user " + user);
        }
    }
}
