package failchat.twitch;

import com.fasterxml.jackson.annotation.JsonIgnore;
import failchat.core.Message;
import failchat.core.Source;
import org.apache.commons.lang.StringUtils;
import org.pircbotx.hooks.events.MessageEvent;

import java.util.Date;

/**
 * Сообщение из irc-чата твитча. Может быть как сообщением от пользователя, так и мета-сообщением от jtv
 */
public class TwitchMessage extends Message {
    private static int[] globalEmoteSet = {0};

    private MetaProperties properties;

    TwitchMessage() {} //for jackson (exception related with JsonInclude.Include.NON_DEFAULT)

    TwitchMessage(MessageEvent event) {
        this.text = event.getMessage();
        this.timestamp = new Date();
        this.source = Source.TWITCH;

        String displayedName = event.getV3Tags().get("display-name");
        if (!displayedName.equals("")) {
            this.author = displayedName;
        }

        //еслипо льзователь не менял ник, то в v3tags пусто, ник capitalized
        else {
            this.author = StringUtils.capitalize(event.getUserHostmask().getNick());
        }

        // TODO: emote sets and other properties
    }

    TwitchMessage(String author, String text) {
        this.author = author;
        this.text = text;
        this.timestamp = new Date();
        this.source = Source.TWITCH;
    }

    @JsonIgnore
    public int[] getEmoteSets() {
        return properties != null && properties.getEmoteSets() != null ? properties.getEmoteSets() : globalEmoteSet;
    }

    public void setProperties(MetaProperties properties) {
        this.properties = properties;
    }
}

@@ -840,105 +840,53 @@
 e%22);
-%0D%0A        if (!displayedName.equals(%22%22)) %7B%0D%0A            this.author = displayedName;%0D%0A        %7D%0D%0A
+ //could return null (e.g. from twitchnotify)
 %0D%0A  
@@ -901,11 +901,11 @@
 %D0%B5%D1%81%D0%BB%D0%B8
+ 
 %D0%BF%D0%BE
- 
 %D0%BB%D1%8C%D0%B7%D0%BE
@@ -973,12 +973,62 @@
     
-else
+if (displayedName == null %7C%7C displayedName.equals(%22%22))
  %7B%0D%0A
@@ -1119,24 +1119,93 @@
 %0D%0A        %7D%0D
+%0A        else %7B%0D%0A            this.author = displayedName;%0D%0A        %7D%0D
 %0A%0D%0A        /

Fix for empty IRCv3 tags