package com.blamejared.mcbot.commands.api;

import java.util.Collection;
import java.util.Collections;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.blamejared.mcbot.MCBot;
import com.blamejared.mcbot.util.NonNull;

import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.experimental.Accessors;
import sx.blah.discord.api.internal.json.objects.EmbedObject;
import sx.blah.discord.handle.obj.IGuild;
import sx.blah.discord.handle.obj.IUser;

@RequiredArgsConstructor
@Getter
public abstract class CommandBase implements ICommand {
    
    @Getter
    @Accessors(fluent = true)
    @RequiredArgsConstructor
    @EqualsAndHashCode
    public static class SimpleFlag implements Flag {
        private final String name;
        private final boolean hasValue;
        
        @Override
        public String longFormName() {
            return name;
        }
    }

    private final @NonNull String name;
    @Accessors(fluent = true)
    private final boolean admin;
    
    private final Collection<Flag> flags;
    
    protected CommandBase(@NonNull String name, boolean admin) {
        this(name, admin, Collections.emptyList());
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + getName().hashCode();
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null || getClass() != obj.getClass()) {
            return false;
        }
        
        CommandBase other = (CommandBase) obj;
        return getName().equals(other.getName());
    }
    
    private static final Pattern REGEX_MENTION = Pattern.compile("<@&?!?([0-9]+)>");
    
    public static String escapeMentions(IGuild guild, String message) {
        if (message == null) return null;
        
    	Matcher matcher = REGEX_MENTION.matcher(message);
    	while (matcher.find()) {
    	    long id = Long.parseLong(matcher.group(1));
    	    String name;
    	    if (matcher.group().contains("&")) {
    	        name = "the " + MCBot.instance.getRoleByID(id).getName();
    	    } else {
    	        IUser user = guild.getUserByID(id);
    	        name = matcher.group().contains("!") ? escapeMentions(guild, user.getNicknameForGuild(guild)) : user.getName();
    	    }

    		message = message.replace(matcher.group(), name);
        }
        return message.replace("@here", "everyone").replace("@everyone", "everyone");
    }
    
    public static EmbedObject escapeMentions(IGuild guild, EmbedObject embed) {
    	embed.title = escapeMentions(guild, embed.title);
    	embed.description = escapeMentions(guild, embed.description);
    	return embed;
    }
}

@@ -2527,24 +2527,30 @@
 e(%22@here%22, %22
+%5Cu200B
 everyone%22).r
@@ -2570,16 +2570,22 @@
 yone%22, %22
+%5Cu200B
 everyone

Fix potential exploit allowing @everyone to be echoed