package org.academiadecodigo.enuminatti.helloworldspringmvc.services;

import org.academiadecodigo.enuminatti.helloworldspringmvc.models.User;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * MIT License
 * (c) 2017 Ricardo Constantino
 */

public class MockUserService implements UserService {
    private Map<String, User> users;

    public MockUserService() {
        users = new HashMap<>();
    }

    @Override
    public void addUser(User user) {
        users.putIfAbsent(user.getUsername(), user);
    }

    @Override
    public void editUser(User user) {
        users.put(user.getUsername(), user);
    }

    @Override
    public User findByName(String username) {
        return users.get(username);
    }

    @Override
    public Long count() {
        return (long) users.size();
    }

    @Override
    public List<User> getUserList() {
        return new ArrayList<>(users.values());
    }

    @Override
    public void remove(String username) {
        users.remove(username);
    }

    public boolean authenticate(String name, String password) {
        return name != null && password != null
                && users.containsKey(name)
                && password.equals(users.get(name).getPassword());

    }
}
