package com.Acrobot.ChestShop.Listeners.PostTransaction;

import com.Acrobot.ChestShop.ChestShop;
import com.Acrobot.ChestShop.Configuration.Properties;
import com.Acrobot.ChestShop.DB.Queue;
import com.Acrobot.ChestShop.DB.Transaction;
import com.Acrobot.ChestShop.Events.TransactionEvent;
import org.bukkit.Location;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.inventory.ItemStack;

import static com.Acrobot.Breeze.Utils.MaterialUtil.getSignName;
import static com.Acrobot.ChestShop.Events.TransactionEvent.TransactionType.BUY;

/**
 * @author Acrobot
 */
public class TransactionLogger implements Listener {
    private static final String BUY_MESSAGE = "%1$s bought %2$s for %3$.2f from %4$s at %5$s";
    private static final String SELL_MESSAGE = "%1$s sold %2$s for %3$.2f to %4$s at %5$s";

    @EventHandler
    public static void onTransaction(TransactionEvent event) {
        String template = (event.getTransactionType() == BUY ? BUY_MESSAGE : SELL_MESSAGE);

        StringBuilder items = new StringBuilder(50);

        for (ItemStack item : event.getStock()) {
            items.append(item.getAmount()).append(' ').append(getSignName(item));
        }

        String message = String.format(template,
                event.getClient().getName(),
                items.toString(),
                event.getPrice(),
                event.getOwner().getName(),
                locationToString(event.getSign().getLocation()));

        ChestShop.getBukkitLogger().info(message);
    }

    @EventHandler
    public static void onTransactionLogToDB(TransactionEvent event) {
        if (!Properties.LOG_TO_DATABASE && !Properties.GENERATE_STATISTICS_PAGE) {
            return;
        }

        double pricePerStack = event.getPrice() / event.getStock().length;

        for (ItemStack item : event.getStock()) {
            Transaction transaction = new Transaction();

            transaction.setAmount(item.getAmount());

            transaction.setItemID(item.getTypeId());
            transaction.setItemDurability(item.getDurability());

            transaction.setPrice((float) pricePerStack);

            transaction.setShopOwner(event.getOwner().getName());
            transaction.setShopUser(event.getClient().getName());

            transaction.setSec(System.currentTimeMillis() / 1000);
            transaction.setBuy(event.getTransactionType() == BUY);

            Queue.addToQueue(transaction);
        }
    }

    private static String locationToString(Location loc) {
        return '[' + loc.getWorld().getName() + "] " + loc.getBlockX() + ", " + loc.getBlockY() + ", " + loc.getBlockZ();
    }
}

@@ -346,24 +346,63 @@
 entHandler;%0A
+import org.bukkit.event.EventPriority;%0A
 import org.b
@@ -887,32 +887,66 @@
    @EventHandler
+(priority = EventPriority.MONITOR)
 %0A    public stat
@@ -967,16 +967,22 @@
 saction(
+final 
 Transact
@@ -991,32 +991,195 @@
 nEvent event) %7B%0A
+        ChestShop.getBukkitServer().getScheduler().runTaskAsynchronously(ChestShop.getPlugin(), new Runnable() %7B%0A            @Override public void run() %7B%0A        
         String t
@@ -1251,24 +1251,32 @@
 _MESSAGE);%0A%0A
+        
         Stri
@@ -1313,24 +1313,32 @@
 ilder(50);%0A%0A
+        
         for 
@@ -1383,24 +1383,32 @@
             
+        
 items.append
@@ -1473,27 +1473,38 @@
         
-%7D%0A%0A
+        %7D%0A%0A   
         
 String m
@@ -1491,24 +1491,29 @@
 %0A           
+     
 String messa
@@ -1549,32 +1549,40 @@
                 
+        
 event.getClient(
@@ -1606,24 +1606,32 @@
             
+       
+ 
 items.toStri
@@ -1632,24 +1632,32 @@
 toString(),%0A
+        
             
@@ -1686,32 +1686,40 @@
                 
+ 
+       
 event.getOwner()
@@ -1746,16 +1746,24 @@
         
+   
+     
 location
@@ -1805,32 +1805,40 @@
 n()));%0A%0A        
+     
+   
 ChestShop.getBuk
@@ -1864,16 +1864,42 @@
 ssage);%0A
+            %7D%0A        %7D);%0A
     %7D%0A%0A 

Send the transaction message in an async thread