package com.github.games647.lagmonitor.commands;

import com.github.games647.lagmonitor.LagMonitor;

import java.util.Map;
import java.util.Map.Entry;

import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.api.chat.BaseComponent;
import net.md_5.bungee.api.chat.HoverEvent;
import net.md_5.bungee.api.chat.HoverEvent.Action;
import net.md_5.bungee.api.chat.TextComponent;

import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.util.ChatPaginator;

public class HelpCommand implements CommandExecutor {

    private static final int HOVER_MAX_LENGTH = ChatPaginator.GUARANTEED_NO_WRAP_CHAT_PAGE_WIDTH / 2;

    private final LagMonitor plugin;

    public HelpCommand(LagMonitor plugin) {
        this.plugin = plugin;
    }

    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
        Map<String, Map<String, Object>> commands = plugin.getDescription().getCommands();

        sender.sendMessage(ChatColor.AQUA + plugin.getName() + "-Help");

        int maxWidth = ChatPaginator.GUARANTEED_NO_WRAP_CHAT_PAGE_WIDTH;
        if (!(sender instanceof Player)) {
            maxWidth = Integer.MAX_VALUE;
        }

        for (Entry<String, Map<String, Object>> entry : commands.entrySet()) {
            String commandKey = entry.getKey();
            Map<String, Object> value = entry.getValue();

            String description = ' ' + value.getOrDefault("description", "No description").toString();
            String usage = ((String) value.getOrDefault("usage", '/' + commandKey)).replace("<command>", commandKey);

            TextComponent usageComponent = new TextComponent(usage);
            usageComponent.setColor(ChatColor.DARK_AQUA);

            TextComponent descriptionComponent = new TextComponent(description);
            descriptionComponent.setColor(ChatColor.GOLD);
            int totalLen = usage.length() + description.length();
            if (totalLen > maxWidth) {
                int newDescLeng = maxWidth - usage.length() - 3 - 1;
                if (newDescLeng < 0) {
                    newDescLeng = 0;
                }

                String shortDesc = description.substring(0, newDescLeng) + "...";
                descriptionComponent.setText(shortDesc);

                TextComponent hoverComponent = new TextComponent();
                for (int i = 0; i < description.length(); i += HOVER_MAX_LENGTH) {
                    String line = description.substring(i, Math.min(i + HOVER_MAX_LENGTH, description.length()));
                    TextComponent textComponent = new TextComponent(line + "\n");
                    textComponent.setColor(ChatColor.GOLD);
                    hoverComponent.addExtra(textComponent);
                }

                descriptionComponent
                        .setHoverEvent(new HoverEvent(Action.SHOW_TEXT, new BaseComponent[]{hoverComponent}));
            } else {
                descriptionComponent.setText(description);
            }

            usageComponent.addExtra(descriptionComponent);
            if (sender instanceof Player) {
                ((Player) sender).spigot().sendMessage(usageComponent);
            } else {
                sender.sendMessage(usageComponent.toLegacyText());
            }
        }

        return true;
    }
}

@@ -373,16 +373,58 @@
 onent;%0A%0A
+import org.apache.commons.lang.WordUtils;%0A
 import o
@@ -450,16 +450,16 @@
 ommand;%0A
-
 import o
@@ -713,60 +713,10 @@
 H = 
-ChatPaginator.GUARANTEED_NO_WRAP_CHAT_PAGE_WIDTH / 2
+40
 ;%0A%0A 
@@ -1816,17 +1816,55 @@
 _AQUA);%0A
+            %0A            %0A            
 %0A
-
         
@@ -2492,54 +2492,54 @@
     
-for (int i = 0; i %3C description.length(); i +=
+String seperated = WordUtils.wrap(description,
  HOV
@@ -2551,19 +2551,31 @@
 X_LENGTH
-) %7B
+, %22%5Cn%22, false);
 %0A       
@@ -2579,28 +2579,29 @@
             
-    
+for (
 String line 
@@ -2604,89 +2604,34 @@
 ine 
-= description.substring(i, Math.min(i + HOVER_MAX_LENGTH, description.length()));
+: seperated.split(%22%5Cn%22)) %7B
 %0A   
@@ -3214,25 +3214,63 @@
     
-((Player) sender)
+Player player = (Player) sender;%0A                player
 .spi

Wrap to a new line only after the word