package com.elmakers.mine.bukkit.magicquests;

import com.elmakers.mine.bukkit.api.magic.Mage;
import com.elmakers.mine.bukkit.api.magic.MageController;
import com.elmakers.mine.bukkit.api.magic.MagicAPI;
import com.elmakers.mine.bukkit.api.wand.Wand;
import com.elmakers.mine.bukkit.api.wand.WandUpgradePath;
import me.blackvein.quests.CustomRequirement;
import org.bukkit.ChatColor;
import org.bukkit.Server;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;

import java.util.Map;

public class PathRequirement extends CustomRequirement {
    private static MagicAPI api;

    protected static MagicAPI getAPI(Server server) {
        if (api == null) {
            Plugin magicPlugin = server.getPluginManager().getPlugin("Magic");
            if (magicPlugin != null && magicPlugin instanceof MagicAPI) {
                api = (MagicAPI)magicPlugin;
            }
        }

        return api;
    }

    public PathRequirement() {
        this.setName("Has Wand Path");
        this.setAuthor("NathanWolf");
        this.addStringPrompt("Wand", "The template to look at (normally 'default')", null);
        this.addStringPrompt("Path", "What path the player must be on.", null);
    }

    @Override
    public boolean testRequirement(Player player, Map<String, Object> stringObjectMap) {
        MagicAPI api = getAPI(player.getServer());
        String baseTemplate = (String)stringObjectMap.get("Wand");
        if (baseTemplate == null || baseTemplate.isEmpty()) {
            baseTemplate = "default";
        }
        String pathKey = (String)stringObjectMap.get("Path");
        if (api == null || pathKey == null) return false;

        MageController controller = api.getController();
        if (!controller.isMage(player)) return false;

        Mage mage = api.getMage(player);

        WandUpgradePath path = null;
        if (baseTemplate != null)
        {
            Wand boundWand = mage.getBoundWand(baseTemplate);
            if (boundWand != null) {
                path = boundWand.getPath();
            }
        }

        boolean isDebug = mage.getDebugLevel() > 0;
        if (path == null) {
            if (isDebug) mage.sendDebugMessage(ChatColor.RED + "Has no " + ChatColor.YELLOW + baseTemplate + ChatColor.DARK_RED +" wand", 7);
            return false;
        }

        boolean hasPath = path.hasPath(pathKey);
        if (isDebug) mage.sendDebugMessage(ChatColor.BLUE + "Checking wand path " + ChatColor.GOLD + path.getKey() +
                ChatColor.BLUE +" has path " + ChatColor.YELLOW + pathKey + "? " +
                (hasPath ? ChatColor.GREEN + "Yes" : ChatColor.RED + "No"), 7);
        return hasPath;
    }
}

@@ -51,48 +51,145 @@
 ort 
-com.elmakers.mine.bukkit.api.magic.Mage;
+java.util.Map;%0A%0Aimport org.bukkit.ChatColor;%0Aimport org.bukkit.Server;%0Aimport org.bukkit.entity.Player;%0Aimport org.bukkit.plugin.Plugin;%0A
 %0Aimp
@@ -231,26 +231,16 @@
 gic.Mage
-Controller
 ;%0Aimport
@@ -278,21 +278,22 @@
 agic.Mag
-icAPI
+eClass
 ;%0Aimport
@@ -322,25 +322,36 @@
 kit.api.
-wand.Wand
+magic.MageController
 ;%0Aimport
@@ -384,218 +384,175 @@
 api.
-wand.WandUpgradePath;%0Aimport me.blackvein.quests.CustomRequirement;%0Aimport org.bukkit.ChatColor;%0Aimport org.bukkit.Server;%0Aimport org.bukkit.entity.Player;%0Aimport org.bukkit.plugin.Plugin;%0A%0Aimport java.util.Map
+magic.MagicAPI;%0Aimport com.elmakers.mine.bukkit.api.magic.ProgressionPath;%0Aimport com.elmakers.mine.bukkit.api.wand.Wand;%0A%0Aimport me.blackvein.quests.CustomRequirement
 ;%0A%0Ap
@@ -1029,21 +1029,16 @@
 me(%22Has 
-Wand 
 Path%22);%0A
@@ -1117,11 +1117,23 @@
 %22, %22
-The
+A specific wand
  tem
@@ -1150,31 +1150,88 @@
 ook 
-at (normally 'default')
+for%22, null);%0A        this.addStringPrompt(%22Class%22, %22A specific class to look for
 %22, n
@@ -1481,185 +1481,8 @@
 ));%0A
-        String baseTemplate = (String)stringObjectMap.get(%22Wand%22);%0A        if (baseTemplate == null %7C%7C baseTemplate.isEmpty()) %7B%0A            baseTemplate = %22default%22;%0A        %7D%0A
     
@@ -1667,105 +1667,254 @@
     
-if (!controller.isMage(player)) return false;%0A%0A        Mage mage = api.getMage(player
+Mage mage = controller.getRegisteredMage(player);%0A        if (mage == null) return false;%0A%0A        ProgressionPath path = null;%0A%0A        // Look for a specific wand first%0A        String baseTemplate = (String)stringObjectMap.get(%22Wand%22
 );%0A
-%0A
         
 Wand
@@ -1913,35 +1913,63 @@
     
-WandUpgradePath path = null
+String baseClass = (String)stringObjectMap.get(%22Class%22)
 ;%0A  
@@ -2002,17 +2002,36 @@
 null
-)%0A       
+ && !baseTemplate.isEmpty())
  %7B%0A 
@@ -2187,32 +2187,325 @@
      %7D%0A        %7D
+ else if (baseClass != null && !baseClass.isEmpty()) %7B%0A            MageClass mageClass = mage.getClass(baseClass);%0A            if (mageClass != null) %7B%0A                path = mageClass.getPath();%0A            %7D%0A        %7D else %7B%0A            path = mage.getActiveProperties().getPath();%0A        %7D
 %0A%0A        boolea

Update path requirement to not require a wand