package com.bwfcwalshy.flarebot.commands.general;

import com.bwfcwalshy.flarebot.FlareBot;
import com.bwfcwalshy.flarebot.MessageUtils;
import com.bwfcwalshy.flarebot.commands.Command;
import com.bwfcwalshy.flarebot.commands.CommandType;
import com.sun.management.OperatingSystemMXBean;
import sx.blah.discord.Discord4J;
import sx.blah.discord.api.IDiscordClient;
import sx.blah.discord.handle.obj.IChannel;
import sx.blah.discord.handle.obj.IMessage;
import sx.blah.discord.handle.obj.IUser;

import java.lang.management.ManagementFactory;

public class InfoCommand implements Command {

    private FlareBot flareBot;
    private IDiscordClient client;
    private Runtime runtime;

    public InfoCommand(FlareBot flareBot) {
        this.flareBot = flareBot;
        this.client = flareBot.getClient();
        this.runtime = Runtime.getRuntime();
    }

    private static final String DIVIDER = "+----------------------------------+";

    @Override
    public void onCommand(IUser sender, IChannel channel, IMessage message, String[] args) {
        String msg = "```FlareBot v" + FlareBot.getInstance().getVersion() + " Info"
                + "\n" + DIVIDER
                + "\nServers: " + client.getGuilds().size()
                + "\nVoice Channels: " + client.getConnectedVoiceChannels().size()
                + "\nText Channels: " + client.getChannels().size()
                + "\nUptime: " + flareBot.getUptime()
                + "\nMemory Usage: " + getMb(runtime.totalMemory() - runtime.freeMemory())
                + "\nFree Memory: " + getMb(runtime.freeMemory())
                + "\nDiscord4J Version: " + Discord4J.VERSION
                + "\nCPU Usage: " + ((int) (ManagementFactory.getPlatformMXBean(OperatingSystemMXBean.class).getSystemCpuLoad() * 10000)) / 100f
                + "%\n" + DIVIDER
                + "\nSupport Server: http://discord.me/flarebot"
                + "\nMade with love by bwfcwalshy#1284 and Arsen#3291\n"
                + "```\n"
                + "https://github.com/ArsenArsen/FlareBot";

        MessageUtils.sendMessage(channel, msg);
    }

    private String getMb(long bytes) {
        return (bytes / 1024 / 1024) + "MB";
    }

    @Override
    public String getCommand() {
        return "info";
    }

    @Override
    public String getDescription() {
        return "Displays info about the bot.";
    }

    @Override
    public CommandType getType() {
        return CommandType.GENERAL;
    }
}

@@ -281,16 +281,54 @@
 MXBean;%0A
+import org.apache.commons.io.IOUtils;%0A
 import s
@@ -542,41 +542,179 @@
 ava.
-lang.management.ManagementFactory
+io.File;%0Aimport java.io.IOException;%0Aimport java.lang.management.ManagementFactory;%0Aimport java.nio.charset.Charset;%0A%0Aimport static com.bwfcwalshy.flarebot.FlareBot.LOGGER
 ;%0A%0Ap
@@ -1215,24 +1215,816 @@
 g%5B%5D args) %7B%0A
+        String git = null;%0A        if (new File(%22FlareBot%22 + File.separator).exists()) %7B%0A            try %7B%0A                ProcessBuilder p = new ProcessBuilder(%22git%22, %22log%22, %22--pretty=format:'%25h'%22, %22-n%22, %221%22);%0A                p.directory(new File(%22FlareBot%22 + File.separator));%0A                Process pr = p.start();%0A                pr.waitFor();%0A                if(pr.exitValue() == 0)%7B%0A                    String res = IOUtils.toString(pr.getInputStream(), Charset.defaultCharset());%0A                    if(res != null && res.length() %3E 7)%7B%0A                        git = res.substring(0, 7);%0A                    %7D%0A                %7D%0A            %7D catch (InterruptedException %7C IOException e1) %7B%0A                LOGGER.error(%22Could not compare git revisions!%22, e1);%0A            %7D%0A        %7D%0A
         Stri
@@ -2784,32 +2784,108 @@
 %22%25%5Cn%22 + DIVIDER%0A
+                + (git != null? %22Current git revision: %22 + git + '%5Cn' : %22%22)%0A
                 

Add git revision to info command