package roycurtis.signshopexport.signshop;

import org.bukkit.Location;
import org.bukkit.block.Sign;
import org.bukkit.inventory.ItemStack;
import org.wargamer2010.signshop.Seller;
import org.wargamer2010.signshop.configuration.Storage;
import org.wargamer2010.signshop.util.economyUtil;
import org.wargamer2010.signshop.util.itemUtil;
import roycurtis.signshopexport.DataSource;
import roycurtis.signshopexport.json.Record;

public class SSDataSource implements DataSource
{
    private Seller[] signs;

    public int prepare()
    {
        int total = Storage.get().shopCount();

        signs = Storage.get().getSellers().toArray(new Seller[total]);

        return total;
    }

    /** Creates a record from a SignShop "seller", using SignShop's utility functions for data */
    public Record createRecordForIndex(int idx)
    {
        Seller   sign  = signs[idx];
        Record   rec   = new Record();
        Location loc   = sign.getSignLocation();
        Sign     state = (Sign) sign.getSign().getState();

        rec.locWorld = loc.getWorld().getName();
        rec.locX     = loc.getBlockX();
        rec.locY     = loc.getBlockY();
        rec.locZ     = loc.getBlockZ();

        rec.ownerName = sign.getOwner().getName();
        rec.ownerUuid = sign.getOwner().GetIdentifier().getStringIdentifier();

        rec.signType  = sign.getOperation();
        rec.signPrice = economyUtil.parsePrice(state.getLine(3));
        rec.signText  = new String[] { state.getLine(1), state.getLine(2) };

        rec.invItems = sign.getItems();

        // This is necessary as the event's items are sometimes CraftItemStack type. That type does
        // not populate its private fields, thus it is useless to Gson's serializer.
        ItemStack[] plainStack = new ItemStack[rec.invItems.length];
        for (int i = 0; i < rec.invItems.length; i++)
        {
            ItemStack oldItem = rec.invItems[i];

            plainStack[i] = new ItemStack(
                oldItem.getType(), oldItem.getAmount(), oldItem.getDurability());

            plainStack[i].setData(oldItem.getData());
            plainStack[i].setItemMeta(oldItem.getItemMeta());
        }

        rec.invItems = plainStack;

        switch(rec.signType)
        {
            case "buy":
            case "ibuy":
                rec.invInStock = itemUtil.stockOKForContainables(
                    sign.getContainables(), sign.getItems(), true);
                break;
            case "sell":
            case "isell":
                rec.invInStock = itemUtil.stockOKForContainables(
                    sign.getContainables(), sign.getItems(), false);
                break;
            default:
                rec.invInStock = true;
                break;
        }

        return rec;
    }

    public void free()
    {
        signs = null;
    }
}

@@ -849,20 +849,21 @@
 ller   s
-ign 
+eller
  = signs
@@ -892,16 +892,17 @@
   rec   
+ 
 = new Re
@@ -932,22 +932,25 @@
 n loc   
+ 
 = s
-ign
+eller
 .getSign
@@ -979,20 +979,21 @@
 gn     s
-tate
+ign  
  = (Sign
@@ -995,19 +995,21 @@
 (Sign) s
-ign
+eller
 .getSign
@@ -1215,27 +1215,29 @@
 wnerName = s
-ign
+eller
 .getOwner().
@@ -1268,27 +1268,29 @@
 wnerUuid = s
-ign
+eller
 .getOwner().
@@ -1350,27 +1350,29 @@
 ignType  = s
-ign
+eller
 .getOperatio
@@ -1424,20 +1424,19 @@
 ePrice(s
-tate
+ign
 .getLine
@@ -1481,20 +1481,19 @@
 ng%5B%5D %7B s
-tate
+ign
 .getLine
@@ -1498,20 +1498,19 @@
 ne(1), s
-tate
+ign
 .getLine
@@ -1541,19 +1541,21 @@
 tems = s
-ign
+eller
 .getItem
@@ -2342,106 +2342,34 @@
 k = 
-itemUtil.stockOKForContainables(%0A                    sign.getContainables(), sign.getItems()
+checkStock(seller
 , true
+, 0
 );%0A 
@@ -2478,106 +2478,46 @@
 k = 
-itemUtil.stockOKForContainables(%0A                    sign.getContainables(), sign.getItems(), fals
+checkStock(seller, false, rec.signPric
 e);%0A
@@ -2656,24 +2656,406 @@
 rec;%0A    %7D%0A%0A
+    private boolean checkStock(Seller seller, boolean take, double price)%0A    %7B%0A        // Check balance of shop owner if sell sign%0A        if ( !take && !seller.getOwner().hasMoney(price) )%0A            return false;%0A%0A        // For both signs, check stock space/availability%0A        return itemUtil.stockOKForContainables(seller.getContainables(), seller.getItems(), take);%0A    %7D%0A%0A
     public v

Add check if SS sell sign has sufficient funds