package it.smartcommunitylab.aac.apimanager;

import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import it.smartcommunitylab.aac.Config.ROLE_SCOPE;
import it.smartcommunitylab.aac.common.Utils;
import it.smartcommunitylab.aac.model.Role;
import it.smartcommunitylab.aac.model.User;
import it.smartcommunitylab.aac.repository.UserRepository;
import it.smartcommunitylab.aac.wso2.model.DataList;
import it.smartcommunitylab.aac.wso2.model.RoleModel;
import it.smartcommunitylab.aac.wso2.model.Subscription;

@Component
@Transactional
public class APIRoleManager {

	@Autowired
	private UserRepository userRepository;	
	
	public void fillRoles(DataList<Subscription> subs) {
		for (Subscription sub: subs.getList()) {
			String subscriber = sub.getSubscriber();
			String info[] = Utils.extractInfoFromTenant(subscriber);
			final String name = info[0];
			final String domain = info[1];			
			
			List<User> users = userRepository.findByAttributeEntities("internal", "email", name);
			User user = users.get(0);
			
			Set<Role> userRoles = user.getRoles();
			List<String> roleNames = userRoles.stream().filter(x -> domain.equals(x.getContext()) && ROLE_SCOPE.application.equals(x.getScope())).map(r -> r.getRole()).collect(Collectors.toList());
			sub.setRoles(roleNames);
		}
	}	
	
	public List<String> updateLocalRoles(RoleModel roleModel) {
		String info[] = Utils.extractInfoFromTenant(roleModel.getUser());
		
		final String name = info[0];
		final String domain = info[1];
		
		List<User> users = userRepository.findByAttributeEntities("internal", "email", name);
		User user = users.get(0);

		Set<Role> userRoles = new HashSet<Role>(user.getRoles());

		if (roleModel.getRemoveRoles() != null) {
			for (String role : roleModel.getRemoveRoles()) {
				Role r = new Role(ROLE_SCOPE.application, role, domain);
				userRoles.remove(r);
			}
		}
		if (roleModel.getAddRoles() != null) {
			for (String role : roleModel.getAddRoles()) {
				Role r = new Role(ROLE_SCOPE.application, role, domain);
				userRoles.add(r);
			}
		}
		user.getRoles().clear();
		user.getRoles().addAll(userRoles);

		userRepository.save(user);
		
		return userRoles.stream().filter(x -> domain.equals(x.getContext()) && ROLE_SCOPE.application.equals(x.getScope())).map(r -> r.getRole()).collect(Collectors.toList());
	}	
	
}

@@ -1185,16 +1185,73 @@
  name);%0A
+%09%09%09if (users == null %7C%7C users.size() == 0) continue;%0A%09%09%09%0A
 %09%09%09User 

Fix for non-existing users