package com.elmakers.mine.bukkit.magicquests;

import com.elmakers.mine.bukkit.api.event.CastEvent;
import com.elmakers.mine.bukkit.api.spell.Spell;
import com.elmakers.mine.bukkit.api.spell.SpellResult;
import me.blackvein.quests.CustomObjective;
import me.blackvein.quests.Quest;
import me.blackvein.quests.Quester;
import me.blackvein.quests.Quests;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;

import java.util.Map;

public class SpellCastObjective extends CustomObjective {

    public SpellCastObjective() {
        this.setName("Magic Spell Cast");
        this.setAuthor("NathanWolf");
        this.setEnableCount(true);
        this.setShowCount(true);
        this.addData("Spell");
        this.setCountPrompt("Enter the number of times the player must cast the spell:");
        this.addDescription("Spell", "The name of the Magic spell the player must have cast.");
        this.setDisplay("Cast %Spell%: %count%");
    }

    @EventHandler
    public void onSpellCast(CastEvent event) {
        Player player = event.getMage().getPlayer();
        if (player == null) return;

        if (event.getSpellResult() != SpellResult.CAST) return;

        // This fails in a pretty bad way if the player is not on a quest currently
        // There isn't a super clean way to check for this state so we'll just catch
        // and ignore expections :\
        Quester quester = Quests.getInstance().getQuester(player.getUniqueId());
        if (quester == null) {
            return;
        }

        for (Quest quest : quester.currentQuests.keySet()) {
            Map<String, Object> map = null;
            try {
                map = getDatamap(player, this, quest);
            } catch (Exception ex) {
                map = null;
            }
            if (map == null) continue;

            String spellName = (String)map.get("Spell");
            if (spellName == null) return;

            Spell spell = event.getSpell();
            if (!spellName.equalsIgnoreCase(spell.getName()) && !spellName.equalsIgnoreCase(spell.getKey())) return;

            incrementObjective(player, this, 1, quest);
        }
    }
}

@@ -93,16 +93,64 @@
 tEvent;%0A
+import com.elmakers.mine.bukkit.api.magic.Mage;%0A
 import c
@@ -394,16 +394,45 @@
 Quests;%0A
+import org.bukkit.ChatColor;%0A
 import o
@@ -966,13 +966,8 @@
 ust 
-have 
 cast
@@ -1089,24 +1089,301 @@
 nt event) %7B%0A
+        Mage mage = event.getMage();%0A%0A        mage.sendDebugMessage(ChatColor.GRAY + %22Checking spell cast objective for cast of %22 + ChatColor.GOLD + event.getSpell().getName() +%0A                ChatColor.BLUE + %22 with result of %22 + ChatColor.YELLOW + event.getSpellResult());%0A%0A
         Play
@@ -1885,396 +1885,1018 @@
     
-for (Quest quest : quester.currentQuests.keySet()) %7B%0A            Map%3CString, Object%3E map = null;%0A            try %7B%0A                map = getDatamap(player, this, quest);%0A            %7D catch (Exception ex) %7B%0A                map = null;%0A            %7D%0A            if (map == null) continue;%0A%0A            String spellName = (String)map.get(%22Spell%22);%0A            if (spellName == null) return;
+mage.sendDebugMessage(ChatColor.BLUE + %22Checking spell cast objective for cast of %22 + ChatColor.GOLD + event.getSpell().getName() +%0A                ChatColor.BLUE + %22 with %22 + ChatColor.YELLOW + quester.currentQuests.size() +%0A                ChatColor.BLUE + %22 active quests%22, 5);%0A%0A        for (Quest quest : quester.currentQuests.keySet()) %7B%0A            Map%3CString, Object%3E map = null;%0A            try %7B%0A                map = getDatamap(player, this, quest);%0A            %7D catch (Exception ex) %7B%0A                map = null;%0A            %7D%0A            if (map == null) %7B%0A                mage.sendDebugMessage(%22No active data for this objective for quest: %22 + ChatColor.GOLD + quest.getName(), 5);%0A                continue;%0A            %7D%0A%0A            String spellName = (String)map.get(%22Spell%22);%0A            if (spellName == null) %7B%0A                mage.sendDebugMessage(ChatColor.RED + %22No active data for this objective for quest: %22 + ChatColor.GOLD + quest.getName(), 5);%0A                return;%0A            %7D
 %0A%0A  
@@ -3046,24 +3046,152 @@
 Key())) 
+b
 re
-turn;%0A
+ak;%0A%0A            mage.sendDebugMessage(ChatColor.GREEN + %22Incrementing objective for quest: %22 + ChatColor.GOLD + quest.getName(), 5);
 %0A       

Add a lot of debug info to the cast objective ... it's just not firing sometimes :(