package fr.Skydust.JdrBot.cmds;

import java.awt.Color;
import java.lang.management.ManagementFactory;
import java.text.DecimalFormat;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;

import com.sedmelluq.discord.lavaplayer.tools.PlayerLibrary;
import fr.Skydust.JdrBot.JdrBot;
import fr.Skydust.JdrBot.bases.command.Command;
import fr.Skydust.JdrBot.bases.command.CommandSpec;
import fr.Skydust.JdrBot.utils.DurationUtils;
import fr.Skydust.JdrBot.utils.Utils;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.JDAInfo;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import net.dv8tion.jda.api.utils.messages.MessageCreateBuilder;
import oshi.SystemInfo;
import oshi.hardware.HardwareAbstractionLayer;

@CommandSpec(
		name = "status",
		description = "Donne le status du bot et du serveur"
)
public class Etat extends Command {
	private static final DecimalFormat df = new DecimalFormat("###.##");

	@Override
	public void call(MessageReceivedEvent e) {
		SystemInfo si = new SystemInfo();
		HardwareAbstractionLayer hal = si.getHardware();
		Runtime rt = Runtime.getRuntime();
		DecimalFormat df = new DecimalFormat("###.##");

		LocalDateTime currentDate = LocalDateTime.now();
		Duration difference = Duration.between(JdrBot.basedate, currentDate);

		e.getChannel().sendMessage(new MessageCreateBuilder().setEmbeds(new EmbedBuilder()
				.setAuthor("Etat de "+ e.getGuild().getSelfMember().getEffectiveName())
				.addBlankField(false)
				.addField("CPU","-----", false)
				.addField("Load (Current/Average)",df.format(hal.getProcessor().getSystemCpuLoad()*100) +"%/"+ df.format(hal.getProcessor().getSystemLoadAverage()*100) +"%",false)
				.addField("CPU Temperature",  String.format("%.1f°C", hal.getSensors().getCpuTemperature()), true)
				.addField("Fan Speed", Arrays.toString(hal.getSensors().getFanSpeeds()), true)
				.addField("Threads", ManagementFactory.getThreadMXBean().getThreadCount()+"", true)
				.addBlankField(false)
				.addField("Memory (Used/Total)", "-----",false)
				.addField("Principal",convertAndFormat(hal.getMemory().getTotal()-hal.getMemory().getAvailable()) +"Mb/"+ convertAndFormat(hal.getMemory().getTotal())+"Mb", true)
				.addField("JVM", convertAndFormat(rt.totalMemory()-rt.freeMemory())+"Mb/"+ convertAndFormat(rt.totalMemory()) +"Mb", true)
				.addField("Swap", convertAndFormat(hal.getMemory().getSwapUsed()) + "Mb/"+ convertAndFormat(hal.getMemory().getSwapTotal()) +"Mb", true)
				.addBlankField(false)
				.addField("Servers",e.getJDA().getGuilds().size()+"",true)
				.addField("Versions", "JdrBot: "+ JdrBot.Version +" - JDA: "+ JDAInfo.VERSION +" - Lavaplayer: "+ PlayerLibrary.VERSION, true)
				.addField("Uptime", DurationUtils.formatDuration(difference), true)
				.setFooter("*Fait le "+ currentDate.format(DateTimeFormatter.ofPattern("dd/MM/yyyy à HH:mm:ss")) +"*", e.getJDA().getSelfUser().getAvatarUrl())
				.setColor(Color.BLUE)
				.build()).build()).queue();
	}

	public static long octetToMegabytes(long input) {
		return input/1048576;
	}

	public static String convertAndFormat(long octets) {
		return df.format(octetToMegabytes(octets));
	}
}
