package com.elmakers.mine.bukkit.action.builtin;

import com.elmakers.mine.bukkit.action.BaseShopAction;
import com.elmakers.mine.bukkit.api.action.CastContext;
import com.elmakers.mine.bukkit.api.magic.MageController;
import com.elmakers.mine.bukkit.api.spell.Spell;
import com.elmakers.mine.bukkit.api.spell.SpellResult;
import org.apache.commons.lang.StringUtils;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.inventory.ItemStack;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ItemShopAction extends BaseShopAction
{
    private Map<String, Double> items = new HashMap<String, Double>();

    @Override
    public void initialize(Spell spell, ConfigurationSection parameters)
    {
        super.initialize(spell, parameters);
        items.clear();
        if (parameters.contains("items"))
        {
            ConfigurationSection itemSection = parameters.getConfigurationSection("items");
            Collection<String> itemKeys = itemSection.getKeys(false);
            for (String itemKey : itemKeys) {
                items.put(itemKey, itemSection.getDouble(itemKey));
            }
        }
    }

    @Override
    public SpellResult perform(CastContext context) {
        SpellResult contextResult = checkContext(context);
        if (!contextResult.isSuccess()) {
            return contextResult;
        }
        List<ShopItem> shopItems = new ArrayList<ShopItem>();
        MageController controller = context.getController();
        for (Map.Entry<String, Double> itemValue : items.entrySet()) {
            String itemKey = itemValue.getKey();
            double worth = items.get(itemKey);
            // This is kinda ugly.. :|
            // This is here to undo the scaling of whatever type of currency is selected
            // So, for an SP shop- SP is converted to virtual economy units
            // and then converted back into SP by BaseShopAction.
            if (costScale > 0) {
                worth /= costScale;
            }

            String[] pieces = StringUtils.split(itemKey, '@');
            int amount = 1;
            if (pieces.length > 1) {
                itemKey = pieces[0];
                try {
                    amount = Integer.parseInt(pieces[1]);
                } catch (Exception ex) {
                    context.getLogger().warning("Invalid item amount in shop: " + pieces[1] + " for item " + itemKey + " shop " + context.getSpell().getKey());
                }
            }
            ItemStack item = controller.createItem(itemKey);
            item.setAmount(amount);
            if (item == null) continue;
            shopItems.add(new ShopItem(item, worth));
        }
        return showItems(context, shopItems);
	}
}

@@ -316,16 +316,76 @@
 Result;%0A
+import com.elmakers.mine.bukkit.utility.ConfigurationUtils;%0A
 import o
@@ -712,62 +712,328 @@
 ate 
-Map%3C
+class ShopItemConfiguration%0A    %7B%0A        ShopItemConfiguration(
 String
+ key
 , 
-D
+d
 ouble
-%3E items = new HashMap%3CString, Double
+ cost)%0A        %7B%0A            this.cost = cost;%0A            this.itemKey = key;%0A        %7D%0A%0A        public String itemKey;%0A        public double cost;%0A    %7D%0A    private List%3CShopItemConfiguration%3E items = new ArrayList%3CShopItemConfiguration
 %3E();
@@ -1239,32 +1239,98 @@
 ms%22))%0A        %7B%0A
+            if (parameters.isConfigurationSection(%22items%22)) %7B%0A    
             Conf
@@ -1417,16 +1417,20 @@
         
+   
+ 
 Collecti
@@ -1487,24 +1487,28 @@
             
+    
 for (String 
@@ -1549,17 +1549,47 @@
     
+    
 items.
-put
+add(new ShopItemConfiguration
 (ite
@@ -1625,16 +1625,17 @@
 temKey))
+)
 ;%0A      
@@ -1644,482 +1644,1289 @@
     
-%7D%0A        %7D%0A    %7D%0A%0A    @Override%0A    public SpellResult perform(CastContext context) %7B%0A        SpellResult contextResult = checkContext(context);%0A        if (!contextResult.isSuccess()) %7B%0A            return contextResult;%0A        %7D%0A        List%3CShopItem%3E shopItems = new ArrayList%3CShopItem%3E();%0A        MageController controller = context.getController();%0A        for (Map.Entry%3CString, Double%3E itemValue : items.entrySet()) %7B%0A            String itemKey = itemValue.getKey();
+    %7D%0A            %7D else %7B%0A                Collection%3CConfigurationSection%3E itemList = ConfigurationUtils.getNodeList(parameters, %22items%22);%0A                for (ConfigurationSection itemConfig : itemList)%0A                %7B%0A                    if (itemConfig != null) %7B%0A                        items.add(new ShopItemConfiguration(itemConfig.getString(%22item%22), itemConfig.getDouble(%22cost%22)));%0A                    %7D else %7B%0A                        items.add(null);%0A                    %7D%0A                %7D%0A            %7D%0A        %7D%0A    %7D%0A%0A    @Override%0A    public SpellResult perform(CastContext context) %7B%0A        SpellResult contextResult = checkContext(context);%0A        if (!contextResult.isSuccess()) %7B%0A            return contextResult;%0A        %7D%0A        List%3CShopItem%3E shopItems = new ArrayList%3CShopItem%3E();%0A        MageController controller = context.getController();%0A        for (ShopItemConfiguration itemConfig : items) %7B%0A            if (itemConfig == null)%0A            %7B%0A                shopItems.add(null);%0A                continue;%0A            %7D%0A            String itemKey = itemConfig.itemKey;%0A            if (itemKey == null %7C%7C itemKey.isEmpty() %7C%7C itemKey.equalsIgnoreCase(%22none%22))%0A            %7B%0A                shopItems.add(null);%0A                continue;%0A            %7D
 %0A   
@@ -2957,23 +2957,21 @@
 item
-s.get(itemKey);
+Config.cost;%0A
 %0A   

Allow for lists of objects in a shop config, and respect the order of the item list