package xyz.benw.plugins.fouriermc.command;

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.MemorySection;
import org.bukkit.configuration.file.FileConfiguration;
import xyz.benw.plugins.fouriermc.FourierMC;
import xyz.benw.plugins.fouriermc.player.PlayerData;
import xyz.benw.plugins.fouriermc.violation.AggregatedViolation;
import xyz.benw.plugins.fouriermc.violation.ViolationType;

import java.util.*;

/**
 * Basic config related commands
 *
 * @author bcbwilla
 */
public class FourierCommands implements CommandExecutor {

    private FourierMC plugin;
    private FileConfiguration config;

    public FourierCommands(FourierMC plugin) {
        this.plugin = plugin;
        this.config = plugin.getConfig();
    }

    @Override
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
        if (cmd.getName().equalsIgnoreCase("fmc")) {

            if (!sender.hasPermission("fmc")) {
                sender.sendMessage(ChatColor.RED + "You don't have permission.");
                return true;
            }

            if(args.length == 1 && args[0].equalsIgnoreCase("list")) {
                Map<String, Object> configParams = config.getValues(true);

                sender.sendMessage(ChatColor.DARK_AQUA + "FourierMC Config Settings:");
                for (Map.Entry<String, Object> entry : configParams.entrySet()) {
                    String key = entry.getKey();
                    Object value = entry.getValue();

                    if(!(value instanceof MemorySection)) {
                        sender.sendMessage(" " + key + ": " + value.toString());
                    }
                }
                return true;

            } else if(args.length == 2 && args[0].equalsIgnoreCase("get")) {
                sender.sendMessage(args[1] + ": " + config.get(args[1]));
                return true;

            } else if(args.length == 1 && args[0].equalsIgnoreCase("reloadconfig")) {
                plugin.reloadConfig();
                sender.sendMessage("Reloaded config for FourierMC.");
                return true;


            } else if(args.length == 2 && args[0].equalsIgnoreCase("info")) {

                String nameString = args[1];
                UUID targetID = Bukkit.getPlayer(nameString).getUniqueId(); // Is there a better way to do this?

                PlayerData playerData = plugin.getPlayerData(targetID);

                String msg = ChatColor.DARK_PURPLE + "Violation Report for " + nameString;
                msg += ChatColor.RESET + "\n";
                for(ViolationType vt : ViolationType.values()) {

                    double velocity = 0;
                    int timesFailed = 0;

                    List<AggregatedViolation> violationList = playerData.getAggregatedViolations(vt);

                    if(violationList != null) {
                        for(AggregatedViolation av : playerData.getAggregatedViolations(vt)) {
                            timesFailed += av.getTimesFailed();
                            velocity += av.getFailedVelocity();
                        }

                        msg += vt.name() + ": \n";
                        msg += "  Times Failed: " + Integer.toString(timesFailed) + "\n";
                        msg += "  Velocity: " + Double.toString(velocity) + "\n\n";
                    } else {
                        msg += ChatColor.DARK_GREEN + " No violations found.";
                    }
                }

                sender.sendMessage(msg);
                return true;

            }

        }
        return false;
    }

}

@@ -2583,21 +2583,27 @@
     
-String msg = 
+sender.sendMessage(
 Chat
@@ -2658,17 +2658,19 @@
 meString
-;
+);%0A
 %0A       
@@ -2682,36 +2682,22 @@
     
+String 
 msg 
-+
 = 
-ChatColor.RESET + %22%5Cn
+%22
 %22;%0A 
@@ -3052,41 +3052,17 @@
 v : 
-playerData.getAggregatedV
+v
 iolation
 s(vt
@@ -3057,21 +3057,20 @@
 iolation
-s(vt)
+List
 ) %7B%0A    
@@ -3441,24 +3441,25 @@
 ) + %22%5Cn%5Cn%22;%0A
+%0A
             
@@ -3471,24 +3471,18 @@
    %7D
- else %7B
 %0A
+ 
         
     
@@ -3469,32 +3469,42 @@
      %7D%0A         
+       %7D%0A%0A
                 
@@ -3507,18 +3507,31 @@
     msg 
-+
 =
+ (msg == %22%22) ?
  ChatCol
@@ -3573,50 +3573,15 @@
 nd.%22
-;%0A                    %7D%0A                %7D%0A
+ : msg;
 %0A   
@@ -3618,16 +3618,17 @@
 e(msg);%0A
+%0A
         

fix double print in info command