package fr.Skydust.JdrBot.cmds;

import java.util.Arrays;

import fr.Skydust.JdrBot.bases.command.Command;
import fr.Skydust.JdrBot.bases.command.CommandSpec;
import fr.Skydust.JdrBot.bases.command.HideCommand;
import net.dv8tion.jda.api.entities.emoji.RichCustomEmoji;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;

@HideCommand
@CommandSpec(
        name = "emote",
        description = "Donne les informations techniques d'une emote"
)
public class Emote extends Command {
    @Override
    public void call(MessageReceivedEvent e) {
        String[] args = Arrays.copyOfRange(e.getMessage().getContentRaw().split(" "), 1, e.getMessage().getContentRaw().split(" ").length);

        String str = args[0];
        if(str.matches("<:.*:\\d+>"))
        {
            String id = str.replaceAll("<:.*:(\\d+)>", "$1");
            RichCustomEmoji emote = e.getJDA().getEmojiById(id);
            if(emote==null)
            {
                e.getChannel().sendMessage("Unknown emote:\n"
                        +"ID: **"+id+"**\n"
                        +"Guild: Unknown\n"
                        +"URL: https://discordcdn.com/emojis/"+id+".png").queue();
                return;
            }
            e.getChannel().sendMessage("Emote **"+emote.getName()+"**:\n"+"ID: **"+emote.getId()+"**\n"+"Guild: "+(emote.getGuild()==null ? "Unknown" : "**"+emote.getGuild().getName()+"**")+"\n"+"URL: "+emote.getImageUrl());
            return;
        }
        if(str.codePoints().count()>10)
        {
            e.getChannel().sendMessage("Invalid emote, or input is too long").queue();;
            return;
        }
        StringBuilder builder = new StringBuilder("Emoji/Character info:");
        str.codePoints().forEachOrdered(code -> {
            char[] chars = Character.toChars(code);
            String hex = Integer.toHexString(code).toUpperCase();
            while(hex.length()<4)
                hex = "0"+hex;
            builder.append("\n`\\u").append(hex).append("`   ");
            if(chars.length>1)
            {
                String hex0 = Integer.toHexString(chars[0]).toUpperCase();
                String hex1 = Integer.toHexString(chars[1]).toUpperCase();
                while(hex0.length()<4)
                    hex0 = "0"+hex0;
                while(hex1.length()<4)
                    hex1 = "0"+hex1;
                builder.append("[`\\u").append(hex0).append("\\u").append(hex1).append("`]   ");
            }
            builder.append(String.valueOf(chars)).append("   _").append(Character.getName(code)).append("_");
        });
        e.getChannel().sendMessage(builder.toString()).queue();;
    }
}
