package li.strolch.privilege.test.model;

import li.strolch.privilege.base.PrivilegeException;
import li.strolch.privilege.handler.SingleSignOnHandler;
import li.strolch.privilege.model.UserState;
import li.strolch.privilege.model.internal.User;
import li.strolch.privilege.model.internal.UserHistory;

import java.util.*;
import java.util.stream.Collectors;

public class DummySsoHandler implements SingleSignOnHandler {

	private Map<String, String> parameterMap;

	@Override
	public Map<String, String> getParameterMap() {
		return this.parameterMap;
	}

	@Override
	public void initialize(Map<String, String> parameterMap) {
		this.parameterMap = parameterMap;
	}

	@Override
	public User authenticateSingleSignOn(Object data) throws PrivilegeException {

		@SuppressWarnings("unchecked") Map<String, String> map = (Map<String, String>) data;

		Set<String> groups = Arrays.stream(map.get("groups").split(",")).map(String::trim).collect(Collectors.toSet());
		Set<String> roles = Arrays.stream(map.get("roles").split(",")).map(String::trim).collect(Collectors.toSet());
		Map<String, String> properties = new HashMap<>();
		return new User(map.get("userId"), map.get("username"), null, map.get("firstName"), map.get("lastName"),
				UserState.REMOTE, groups, roles, Locale.ENGLISH, properties, false, UserHistory.EMPTY);
	}
}
