package io.github.thatsmusic99.headsplus.commands.maincommand;

import io.github.thatsmusic99.headsplus.HeadsPlus;
import io.github.thatsmusic99.headsplus.commands.CommandInfo;
import io.github.thatsmusic99.headsplus.commands.IHeadsPlusCommand;
import io.github.thatsmusic99.headsplus.config.HeadsPlusMessagesManager;
import io.github.thatsmusic99.headsplus.listeners.DeathEvents;
import io.github.thatsmusic99.headsplus.util.CachedValues;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.util.StringUtil;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

@CommandInfo(commandname = "tests", permission = "headsplus.maincommand.tests", subcommand = "Tests", usage = "/hp tests <Entity type> <Amount>", maincommand = true)
public class TestsCommand implements IHeadsPlusCommand {

    private final HeadsPlusMessagesManager hpc = HeadsPlus.getInstance().getMessagesConfig();

    @Override
    public String getCmdDescription(CommandSender sender) {
        return hpc.getString("descriptions.tests", sender);
    }

    @Override
    public boolean fire(String[] args, CommandSender sender) {
        if (args.length > 1) {
            if (DeathEvents.ableEntities.contains(args[1].toUpperCase())) {
                if (args.length > 2) {
                    if (CachedValues.MATCH_PAGE.matcher(args[2]).matches()) {
                        int amount = Integer.parseInt(args[2]);
                        String type = args[1].toLowerCase().replaceAll("_", "");
                        sender.sendMessage(hpc.getString("commands.tests.running-tests"));
                        double chance = HeadsPlus.getInstance().getHeadsConfig().getConfig().getDouble(type + ".chance");
                        Random rand = new Random();
                        new BukkitRunnable() {
                            @Override
                            public void run() {
                                int successes = 0;
                                for (int in = 0; in < amount; in++) {
                                    double result = rand.nextDouble() * 100;
                                    if (result <= chance) {
                                        successes++;
                                    }
                                }
                                sender.sendMessage(hpc.getString("commands.tests.results").replaceAll("\\{results}", successes + "/" + amount + " (" + (((double) successes / (double) amount) * 100) + "%)"));
                            }
                        }.runTaskAsynchronously(HeadsPlus.getInstance());
                        return true;
                    } else {
                        sender.sendMessage(hpc.getString("commands.errors.invalid-input-int"));
                    }
                } else {
                    sender.sendMessage(hpc.getString("commands.errors.invalid-args"));
                }
            } else {
                sender.sendMessage(hpc.getString("commands.errors.invalid-args"));
            }
        } else {
            sender.sendMessage(hpc.getString("commands.errors.invalid-args"));
        }


        return false;
    }

    @Override
    public List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command cmd, @NotNull String label, @NotNull String[] args) {
        List<String> results = new ArrayList<>();
        if (args.length == 2) {
            StringUtil.copyPartialMatches(args[1], IHeadsPlusCommand.getEntities(), results);
        }
        return results;
    }
}

@@ -1416,115 +1416,30 @@
    i
-f (CachedValues.MATCH_PAGE.matcher(args%5B2%5D).matches()) %7B%0A                        int amount = Integer.parse
+nt amount = HPUtils.is
 Int(
@@ -1448,20 +1448,16 @@
 gs%5B2%5D);%0A
-    
         
@@ -1541,34 +1541,27 @@
             
-    sender
+hpc
 .sendMessage
@@ -1557,38 +1557,24 @@
 sendMessage(
-hpc.getString(
 %22commands.te
@@ -1591,24 +1591,27 @@
 g-tests%22
-));%0A    
+, sender);%0A
         
@@ -1740,20 +1740,16 @@
         
-    
 Random r
@@ -1788,20 +1788,16 @@
         
-    
 new Bukk
@@ -1835,20 +1835,16 @@
         
- 
-   
 @Overrid
@@ -1845,20 +1845,16 @@
 verride%0A
-    
         
@@ -1913,28 +1913,24 @@
             
-    
 int successe
@@ -1936,20 +1936,16 @@
 es = 0;%0A
-    
         
@@ -2030,28 +2030,24 @@
             
-    
 double resul
@@ -2075,20 +2075,16 @@
  * 100;%0A
-    
         
@@ -2167,20 +2167,16 @@
         
-    
 successe
@@ -2208,34 +2208,26 @@
             
-   
- 
 %7D%0A
-    
             
@@ -2268,34 +2268,27 @@
             
-    sender
+hpc
 .sendMessage
@@ -2284,38 +2284,24 @@
 sendMessage(
-hpc.getString(
 %22commands.te
@@ -2316,24 +2316,19 @@
 lts%22
-).replaceAll(%22%5C%5C
+, sender, %22
 %7Bres
@@ -2423,23 +2423,18 @@
  + %22%25)%22)
-)
 ;%0A
-    
         
@@ -2471,20 +2471,16 @@
         
-  
-  
 %7D.runTas
@@ -2537,28 +2537,24 @@
             
-    
 return true;
@@ -2554,20 +2554,17 @@
 n true;%0A
-    
+%0A
         
@@ -2596,34 +2596,27 @@
             
-    sender
+hpc
 .sendMessage
@@ -2612,38 +2612,24 @@
 sendMessage(
-hpc.getString(
 %22commands.er
@@ -2645,153 +2645,21 @@
 lid-
-input-int%22));%0A                    %7D%0A                %7D else %7B%0A                    sender.sendMessage(hpc.getString(%22commands.errors.invalid-args%22)
+args%22, sender
 );%0A 
@@ -2704,38 +2704,35 @@
                 
-sender
+hpc
 .sendMessage(hpc
@@ -2724,38 +2724,24 @@
 sendMessage(
-hpc.getString(
 %22commands.er
@@ -2750,33 +2750,40 @@
 rs.invalid-args%22
-)
+, sender
 );%0A            %7D
@@ -2812,22 +2812,19 @@
         
-sender
+hpc
 .sendMes
@@ -2828,30 +2828,16 @@
 Message(
-hpc.getString(
 %22command
@@ -2858,17 +2858,24 @@
 id-args%22
-)
+, sender
 );%0A     

Test subcommand changes