package org.dynmap.bukkitbridge.permissions;

import java.util.HashSet;
import java.util.Set;

import org.bukkit.Bukkit;
import org.bukkit.Server;
import org.bukkit.plugin.Plugin;
import org.anjocaido.groupmanager.GroupManager;
import org.anjocaido.groupmanager.dataholder.worlds.WorldsHolder;
import org.anjocaido.groupmanager.permissions.AnjoPermissionsHandler;
import org.dynmap.bukkitbridge.DynmapCBBridgePlugin;
import org.dynmap.permissions.PermissionsHandler;

public class GroupManagerPermissions extends PermissionsHandler  {
    private static final String PREFIX = "dynmap.";
    GroupManager gm;
    WorldsHolder wh;

    public static GroupManagerPermissions create() {
        Server server = Bukkit.getServer();
        Plugin permissionsPlugin = server.getPluginManager().getPlugin("GroupManager");
        if (permissionsPlugin == null)
            return null;
        server.getPluginManager().enablePlugin(permissionsPlugin);
        if(permissionsPlugin.isEnabled() == false)
            return null;
        DynmapCBBridgePlugin.log.info("Using GroupManager " + permissionsPlugin.getDescription().getVersion() + " for access control");
        return new GroupManagerPermissions(permissionsPlugin);
    }

    public GroupManagerPermissions(Plugin permissionsPlugin) {
        gm = (GroupManager)permissionsPlugin;
        wh = gm.getWorldsHolder();
    }
    
    @Override
    public Set<String> hasOfflinePermissions(String player, Set<String> perms) {
        HashSet<String> hasperms = new HashSet<String>();
        AnjoPermissionsHandler apm = wh.getWorldPermissionsByPlayerName(player);
        if (apm != null) {
            for (String pp : perms) {
                if (apm.permission(player, PREFIX + pp)) {
                    hasperms.add(pp);
                }
            }
        }
        return hasperms;
    }
    @Override
    public boolean hasOfflinePermission(String player, String perm) {
        AnjoPermissionsHandler apm = wh.getWorldPermissionsByPlayerName(player);
        if(apm != null) {
            return apm.permission(player, PREFIX + perm);
        }
        return false;
    }

    @Override
    public boolean hasPermission(String player, String perm) {
        AnjoPermissionsHandler apm = wh.getWorldPermissionsByPlayerName(player);
        if(apm != null) {
            return apm.permission(player, PREFIX + perm);
        }
        return false;
    }

    @Override
    public boolean hasPermissionNode(String player, String perm) {
        AnjoPermissionsHandler apm = wh.getWorldPermissionsByPlayerName(player);
        if(apm != null) {
            return apm.permission(player, perm);
        }
        return false;
    }
}

@@ -604,29 +604,8 @@
  gm;
-%0A    WorldsHolder wh;
 %0A%0A  
@@ -1313,43 +1313,8 @@
 in;%0A
-        wh = gm.getWorldsHolder();%0A
     
@@ -1502,34 +1502,34 @@
 nsHandler apm = 
-wh
+gm
 .getWorldPermiss
@@ -1525,38 +1525,58 @@
 orld
-PermissionsByPlayerName(player
+sHolder().getDefaultWorld().getPermissionsHandler(
 );%0A 
@@ -1922,34 +1922,34 @@
 nsHandler apm = 
-wh
+gm
 .getWorldPermiss
@@ -1945,38 +1945,58 @@
 orld
-PermissionsByPlayerName(player
+sHolder().getDefaultWorld().getPermissionsHandler(
 );%0A 
@@ -2223,34 +2223,34 @@
 nsHandler apm = 
-wh
+gm
 .getWorldPermiss
@@ -2246,38 +2246,58 @@
 orld
-PermissionsByPlayerName(player
+sHolder().getDefaultWorld().getPermissionsHandler(
 );%0A 
@@ -2540,10 +2540,10 @@
 m = 
-wh
+gm
 .get
@@ -2551,38 +2551,58 @@
 orld
-PermissionsByPlayerName(player
+sHolder().getDefaultWorld().getPermissionsHandler(
 );%0A 

Update for GroupManager - handle offline users better