package in.twizmwaz.cardinal.module.modules.blockdrops;

import in.twizmwaz.cardinal.match.Match;
import in.twizmwaz.cardinal.module.ModuleBuilder;
import in.twizmwaz.cardinal.module.ModuleCollection;
import in.twizmwaz.cardinal.module.modules.filter.FilterModule;
import in.twizmwaz.cardinal.module.modules.filter.FilterModuleBuilder;
import in.twizmwaz.cardinal.module.modules.regions.RegionModule;
import in.twizmwaz.cardinal.module.modules.regions.RegionModuleBuilder;
import in.twizmwaz.cardinal.util.Numbers;
import in.twizmwaz.cardinal.util.Parser;
import org.apache.commons.lang.math.NumberUtils;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
import org.jdom2.Element;

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

public class BlockdropsBuilder implements ModuleBuilder {

    @Override
    public ModuleCollection<Blockdrops> load(Match match) {
        ModuleCollection<Blockdrops> results = new ModuleCollection<>();
        for (Element element : match.getDocument().getRootElement().getChildren("blockdrops")) {
            for (Element rule : element.getChildren("rule")) {
                RegionModule region = null;
                if (rule.getChild("region") != null) {
                    region = RegionModuleBuilder.getRegion(rule.getChild("region"));
                }
                FilterModule filter = null;
                if (rule.getChild("filter") != null) {
                    filter = FilterModuleBuilder.getFilter(rule.getChild("filter").getChildren().get(0));
                }
                Set<ItemStack> drops = new HashSet<>();
                for (Element items : rule.getChildren("drops")) {
                    for (Element item : items.getChildren("item")) {
                        drops.add(Parser.getItem(item));
                    }
                }
                Material replaceType = Material.AIR;
                int replaceDamage = -1;
                for (Element replaceElement : rule.getChildren("replacement")) {
                    String material = replaceElement.getText();
                    String materialType = material.split(":")[0].trim();
                    replaceType = (NumberUtils.isNumber(materialType) ? Material.getMaterial(Integer.parseInt(materialType)) : Material.matchMaterial(materialType));
                    replaceDamage = material.contains(":") ? Numbers.parseInt(material.split(":")[1].trim()) : -1;
                }
                int experience = 0;
                for (Element experienceElement : rule.getChildren("experience")) {
                    experience = Numbers.parseInt(experienceElement.getText());
                }
                boolean wrongTool = false;
                for (Element wrongToolElement : rule.getChildren("wrongtool")) {
                    wrongTool = Numbers.parseBoolean(wrongToolElement.getText());
                }
                results.add(new Blockdrops(region, filter, drops, replaceType, replaceDamage, experience, wrongTool));
            }

        }
        return results;
    }
}
@@ -709,22 +709,73 @@
 va.util.
-HashSe
+ArrayList;%0Aimport java.util.HashSet;%0Aimport java.util.Lis
 t;%0Aimpor
@@ -1010,31 +1010,171 @@
     
-for (Element element : 
+List%3CElement%3E blockdrops = new ArrayList%3C%3E();%0A        blockdrops.addAll(match.getDocument().getRootElement().getChildren(%22blockdrops%22));%0A        blockdrops.addAll(
 matc
@@ -1224,24 +1224,25 @@
 n(%22block
+-
 drops%22))
  %7B%0A     
@@ -1225,32 +1225,76 @@
 (%22block-drops%22))
+;%0A        for (Element element : blockdrops)
  %7B%0A            f

Allow blockdrops to be defined as blockdrops or block-drops