package mnm.mods.tabbychat;

import java.util.Map;

import mnm.mods.tabbychat.api.Channel;
import mnm.mods.tabbychat.api.listener.ChannelListener;
import mnm.mods.tabbychat.api.listener.events.MessageAddedToChannelEvent;

import com.google.common.collect.Maps;

public class ChatAddonAntiSpam implements ChannelListener {

    private Map<Channel, Counter> messageMap = Maps.newHashMap();

    @Override
    public void onMessageAdded(MessageAddedToChannelEvent event) {
        if (TabbyChat.getInstance().generalSettings.antiSpam.getValue() && event.id == 0) {
            Channel channel = event.channel;
            Counter counter = this.messageMap.get(channel);
            if (counter == null) {
                counter = new Counter("");
                messageMap.put(channel, counter);
            }
            String chat = event.chat.getUnformattedText();
            if (chat.equals(counter.lastMessage)) {
                counter.spamCounter++;
                event.chat.appendText(" [" + counter.spamCounter + "x]");
                channel.removeMessageAt(0);
            } else {
                counter.lastMessage = chat;
                counter.spamCounter = 1;
            }
        }
    }

    private class Counter {
        private String lastMessage;
        private int spamCounter = 1;

        private Counter(String lastMessage) {
            this.lastMessage = lastMessage;
        }
    }

}

@@ -43,16 +43,61 @@
 til.Map;
+%0Aimport org.apache.commons.lang3.StringUtils;
 %0A%0Aimport
@@ -509,24 +509,25 @@
 vent) %7B%0A
+%0A
         
 if (Tabb
@@ -522,71 +522,350 @@
     
-if (TabbyChat.getInstance().generalSettings.antiSpam.getValue()
+boolean prefEnableAntiSpam = TabbyChat.getInstance().generalSettings.antiSpam.getValue();%0A        boolean prefPartialMatching = TabbyChat.getInstance().generalSettings.antiSpamPartial.getValue();%0A        float   prefPartialMatchAmount = TabbyChat.getInstance().generalSettings.antiSpamPartialAmount.getValue();%0A%0A        if (prefEnableAntiSpam
  && 
@@ -1203,16 +1203,41 @@
     if (
+(!prefPartialMatching && 
 chat.equ
@@ -1261,16 +1261,115 @@
 essage))
+ %7C%7C (prefPartialMatching &&  getDifference(chat, counter.lastMessage) %3C= (prefPartialMatchAmount)))
  %7B%0A     
@@ -1866,10 +1866,174 @@
 %0A    %7D%0A%0A
+    private float getDifference(String s1, String s2)%7B%0A        return StringUtils.getLevenshteinDistance(s1.toLowerCase(), s2.toLowerCase())*100/s1.length();%0A    %7D%0A
 %7D%0A

Update ChatAddonAntiSpam.java