package com.robothand.highqualitybot.permission;

import com.robothand.highqualitybot.Bot;
import com.robothand.highqualitybot.Utils;
import com.robothand.highqualitybot.command.Command;
import net.dv8tion.jda.core.entities.Member;
import net.dv8tion.jda.core.entities.Role;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Hashtable;

/**
 * PermissionGroup.java
 *
 * Represents a set of permissions for a group of Users
 */
public class PermissionGroup {
    Logger log = LoggerFactory.getLogger(this.getClass());
    private ArrayList<Command> allowed;
    private ArrayList<Command> disallowed;
    private boolean allowedHasPrecedence;
    private int priority;
    private Role role;

    public PermissionGroup(String filename) throws FileNotFoundException {
        // default values
        allowed = new ArrayList<>();
        disallowed = new ArrayList<>();
        allowedHasPrecedence = true;
        priority = 0;

        log.debug("Reading PermissionGroup config from \'{}\'...", filename);
        Hashtable<String,String> table = Utils.readCFG(filename);

        for (String key : table.keySet()) {
            String value = table.get(key);

            switch (key) {
                case "allowed":
                    allowed = PermissionManager.instance().parseCommandList(value);
                    break;

                case "disallowed":
                    disallowed = PermissionManager.instance().parseCommandList(value);

                case "allowedHasPrecedence":
                    allowedHasPrecedence = Boolean.parseBoolean(value);
                    break;

                case "priority":
                    priority = Integer.parseInt(value);
                    break;

                case "role":
                    role = Bot.getAPI().getRoleById(value);

                    if (role == null) {
                        log.error("in \'{}\': invalid Role ID", filename);
                    }

                    break;

                default:
                    log.info("in \'{}\': unknown property \'{}\', ignoring...", filename, key);
                    break;
            }
        }
    }

    public boolean appliesTo(Member member, Command command) {
        return member.getRoles().contains(role) && (allowed.contains(command) || disallowed.contains(command));
    }

    public boolean hasPermission(Command command) {
        boolean permission = false;

        if (allowed.contains(command)) {
            permission = true;
        }

        if (disallowed.contains(command) && !allowedHasPrecedence) {
            permission = false;
        }

        return permission;
    }

    public int getPriority() {
        return priority;
    }
}

@@ -47,49 +47,8 @@
 n;%0A%0A
-import com.robothand.highqualitybot.Bot;%0A
 impo
@@ -86,16 +86,16 @@
 .Utils;%0A
+
 import c
@@ -724,20 +724,22 @@
 private 
-Role
+String
  role;%0A%0A
@@ -1791,16 +1791,20 @@
 se %22role
+Name
 %22:%0A     
@@ -1829,180 +1829,15 @@
 e = 
-Bot.getAPI().getRoleById(
 value
-)
 ;%0A
-%0A                    if (role == null) %7B%0A                        log.error(%22in %5C'%7B%7D%5C': invalid Role ID%22, filename);%0A                    %7D%0A%0A
     
@@ -2114,110 +2114,485 @@
     
-return member.getRoles().contains(role) && (allowed.contains(command) %7C%7C disallowed.contains(command))
+// check if this role applies to given command first%0A        if (!(allowed.contains(command) %7C%7C disallowed.contains(command))) %7B%0A            return false;%0A        %7D%0A%0A        boolean flag = false;%0A%0A        // check by role name%0A        if (role != null) %7B%0A            for (Role r : member.getGuild().getRolesByName(role, true)) %7B%0A                if (member.getRoles().contains(r)) %7B%0A                    flag = true;%0A                %7D%0A            %7D%0A        %7D%0A%0A        return flag
 ;%0A  

change PermissionGroup configuration to use role names instead of ids