package xyz.benw.plugins.fouriermc.violation;

import org.bukkit.Bukkit;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import xyz.benw.plugins.fouriermc.FourierMC;
import xyz.benw.plugins.fouriermc.event.violation.AggregatedViolationEvent;
import xyz.benw.plugins.fouriermc.event.violation.ViolationEvent;

import java.util.*;

/**
 * Listens for violation and aggregated violation events
 *
 * @author bcbwilla
 */
public class ViolationListener implements Listener {

    private FourierMC plugin;

    /**
     * Class constructor
     * @param plugin instance of FouirerMC plugin
     */
    public ViolationListener(FourierMC plugin) {
        this.plugin = plugin;
    }

    /**
     * Records when a player violates one of the clicking tests
     * @param event violation event
     */
    @EventHandler
    public void onViolation(ViolationEvent event) {
        UUID playerID = event.getPlayer().getUniqueId();
        Violation violation = event.getViolation();
        ViolationType violationType = violation.getViolationType();

        Map violationMap;
        if(plugin.violationLogger.containsKey(playerID)) {
            violationMap = plugin.violationLogger.get(playerID);
        } else {
            violationMap = new HashMap<ViolationType, ArrayList<Violation>>();
            plugin.violationLogger.put(playerID, violationMap);
        }

        List violationList;
        if(violationMap.containsKey(violationType)) {
            violationList = (ArrayList) violationMap.get(violationType);
        } else {
            violationList = new ArrayList<Violation>();
            violationMap.put(violationType, violationList);
        }

        violationList.add(violation);

        String msg = event.getPlayer().getDisplayName() + " logged a ";
        msg += violationType.toString() + " value of " + violation.getValue();
        Bukkit.getServer().broadcastMessage(msg);

    }

    /**
     * Records when a player's violation record produces suspicious violations
     * @param event aggregated violation event
     */
    @EventHandler
    public void onAggregatedViolation(AggregatedViolationEvent event) {

        boolean log = plugin.getConfig().getBoolean("tests.logAggregated");
        Bukkit.getServer().broadcastMessage("Aggregated violation");
        if(log) {
            String playerName = event.getPlayer().getDisplayName();

            AggregatedViolation av = event.getAggregatedViolation();
            String violationType = av.getViolationType().toString();
            String timesFailed = Integer.toString(av.getTimesFailed());
            String failedDuration = Double.toString(av.getfailedDuration() / 60.0);

            String msg = "Player " + playerName + "failed " + violationType + " " + timesFailed;
            msg += " times in the past " + failedDuration + " minutes.";

            Bukkit.getServer().broadcastMessage(msg);
        }

    }
}

@@ -44,34 +44,8 @@
 n;%0A%0A
-import org.bukkit.Bukkit;%0A
 impo
@@ -316,14 +316,78 @@
 ava.
-util.*
+text.DecimalFormat;%0Aimport java.util.*;%0Aimport java.util.logging.Level
 ;%0A%0A/
@@ -1754,24 +1754,61 @@
 iolation);%0A%0A
+%0A        if(plugin.getDebug()) %7B%0A    
         Stri
@@ -1867,16 +1867,20 @@
 ed a %22;%0A
+    
         
@@ -1962,49 +1962,62 @@
     
-Bukkit.getServer().broadcastMessage(msg);
+    plugin.getLogger().log(Level.INFO, msg);%0A        %7D
 %0A%0A  
@@ -2334,76 +2334,8 @@
 %22);%0A
-        Bukkit.getServer().broadcastMessage(%22Aggregated violation%22);
 %0A   
@@ -2627,16 +2627,75 @@
 iled());
+%0A%0A            DecimalFormat df = new DecimalFormat(%220.00%22);
 %0A       
@@ -2727,23 +2727,17 @@
 n = 
-Double.toString
+df.format
 (av.
@@ -2762,13 +2762,17 @@
 ) / 
-6
+(100
 0.0)
+)
 ;%0A%0A 
@@ -2821,16 +2821,17 @@
 Name + %22
+ 
 failed %22
@@ -2934,14 +2934,14 @@
 + %22 
-minute
+second
 s.%22;
@@ -2958,44 +2958,43 @@
     
-Bukkit.getServer().broadcastMessage(
+plugin.getLogger().log(Level.INFO, 
 msg)

Clean up violation event logging