package io.github.likcoras.asuka;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.pircbotx.User;
import org.pircbotx.UserLevel;
import com.google.common.base.Optional;
import io.github.likcoras.asuka.exception.ConfigException;

public class AuthManager {
	
	private String ownerId;
	private Map<String, UserLevel> levels;
	
	public AuthManager(BotConfig config) throws ConfigException {
		levels = new ConcurrentHashMap<String, UserLevel>();
		ownerId = config.getString("ownerId");
		levels.put(ownerId, UserLevel.OWNER);
	}
	
	public void setLevel(User user, UserLevel level) {
		String id = BotUtil.getId(user);
		if (!id.equals(ownerId))
			levels.put(id, level);
	}
	
	public void removeLevel(User user) {
		String id = BotUtil.getId(user);
		if (!id.equals(ownerId))
			levels.remove(id);
	}
	
	public Optional<UserLevel> getLevel(User user) {
		return Optional.fromNullable(levels.get(BotUtil.getId(user)));
	}
	
	public boolean checkAccess(User user, UserLevel required) {
		Optional<UserLevel> userLevel = getLevel(user);
		if (!userLevel.isPresent())
			return false;
		return required.compareTo(userLevel.get()) <= 0;
	}
	
}

@@ -604,36 +604,33 @@
  level) %7B%0A%09%09
-String id = 
+setLevel(
 BotUtil.getI
@@ -632,33 +632,100 @@
 .getId(user)
-;
+, level);%0A%09%7D%0A%09%0A%09public void setLevel(String user, UserLevel level) %7B
 %0A%09%09if (!
 id.equals(ow
@@ -704,34 +704,36 @@
 level) %7B%0A%09%09if (!
-id
+user
 .equals(ownerId)
@@ -748,18 +748,20 @@
 els.put(
-id
+user
 , level)
@@ -811,20 +811,20 @@
 %7B%0A%09%09
-String id = 
+removeLevel(
 BotU
@@ -842,19 +842,67 @@
 ser)
+)
 ;%0A
+%09%7D%0A%09%0A%09public void removeLevel(String user) %7B%0A
 %09%09if (!
-id
+user
 .equ
@@ -932,18 +932,20 @@
 .remove(
-id
+user
 );%0A%09%7D%0A%09%0A

Allow editing user levels by providing string id