package com.stevesoltys.telegramirc.protocol.telegram;

import org.springframework.stereotype.Component;
import org.telegram.telegrambots.api.objects.Document;
import org.telegram.telegrambots.api.objects.Message;
import org.telegram.telegrambots.api.objects.Sticker;

import java.util.LinkedList;
import java.util.List;

/**
 * @author Steve Soltys
 */
@Component
public class TelegramMessageDecoder {

    public List<String> decode(Message telegramMessage) {
        List<String> messages = new LinkedList<>();

        if (telegramMessage.isReply()) {
            Message replyToMessage = telegramMessage.getReplyToMessage();

            if (replyToMessage.getFrom() != null && replyToMessage.hasText()) {
                int replyToMessageLength = replyToMessage.getText().length();

                String quotedText = replyToMessage.getText()
                        .substring(0, replyToMessageLength > 100 ? 100 : replyToMessageLength);

                messages.add("<" + replyToMessage.getFrom().getUserName() + "> " + quotedText);
            }
        }

        if (telegramMessage.hasDocument()) {
            Document document = telegramMessage.getDocument();

            messages.add("(" + document.getMimeType() + ")");
        }

        if (telegramMessage.getSticker() != null) {
            Sticker sticker = telegramMessage.getSticker();

            messages.add("(Sticker " + sticker.getEmoji() + ")");
        }

        if (telegramMessage.getPhoto() != null) {
            messages.add("(Photo)");
        }

        if (telegramMessage.hasText()) {
            messages.add(telegramMessage.getText());
        }

        return messages;
    }

}

@@ -262,16 +262,41 @@
 icker;%0A%0A
+import java.util.Arrays;%0A
 import j
@@ -1622,16 +1622,43 @@
 ages.add
+All(decodeMultiLineMessages
 (telegra
@@ -1676,16 +1676,17 @@
 tText())
+)
 ;%0A      
@@ -1722,10 +1722,135 @@
 %0A    %7D%0A%0A
+    private List%3CString%3E decodeMultiLineMessages(String text) %7B%0A        return Arrays.asList(text.split(%22%5Cr%5Cn?%7C%5Cn%22));%0A    %7D%0A%0A
 %7D%0A

Fix #5