package com.stevesoltys.telegramirc.protocol.telegram.message;

import com.stevesoltys.telegramirc.protocol.telegram.bot.TelegramBot;
import com.stevesoltys.telegramirc.protocol.telegram.message.decoder.photo.TelegramPhotoMessageDecoder;
import org.springframework.beans.factory.annotation.Autowired;
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.Arrays;
import java.util.LinkedList;
import java.util.List;

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

    private final TelegramPhotoMessageDecoder photoMessageDecoder;

    @Autowired
    public TelegramMessageDecoder(TelegramPhotoMessageDecoder photoMessageDecoder) {
        this.photoMessageDecoder = photoMessageDecoder;
    }

    public List<String> decode(TelegramBot telegramBot, 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.addAll(photoMessageDecoder.decodePhotoMessage(telegramBot, telegramMessage));
        }

        if(telegramMessage.getCaption() != null) {
            messages.addAll(decodeMultiLineMessages(telegramMessage.getCaption()));
        }

        if (telegramMessage.hasText()) {
            messages.addAll(decodeMultiLineMessages(telegramMessage.getText()));
        }

        return messages;
    }

    private List<String> decodeMultiLineMessages(String text) {
        return Arrays.asList(text.split("\r\n?|\n"));
    }

}

@@ -667,16 +667,76 @@
 oder %7B%0A%0A
+    private static final int MAX_IRC_MESSAGE_LENGTH = 480;%0A%0A
     priv
@@ -1037,24 +1037,198 @@
 mMessage) %7B%0A
+        return decode(telegramBot, telegramMessage, false);%0A    %7D%0A%0A    private List%3CString%3E decode(TelegramBot telegramBot, Message telegramMessage, boolean quotedMessage) %7B%0A
         List
@@ -1305,16 +1305,34 @@
 sReply()
+ && !quotedMessage
 ) %7B%0A    
@@ -1454,80 +1454,56 @@
 null
- && replyToMessage.hasText()) %7B%0A                int replyToMessageLength
+) %7B%0A                String quotedMessageUsername
  = r
@@ -1523,21 +1523,26 @@
 .get
-Text().length
+From().getUserName
 ();%0A
@@ -1562,150 +1562,241 @@
     
-String quotedText = replyToMessage.getText()%0A                        .substring(0, replyToMessageL
+decode(telegramBot, replyToMessage, true).forEach(text -%3E %7B%0A                    String quotedText = text.substring(0,%0A                            text.l
 ength
+()
  %3E 
-100 ? 100 : replyToMessageL
+MAX_IRC_MESSAGE_LENGTH ? MAX_IRC_MESSAGE_LENGTH : text.l
 ength
+()
 );%0A%0A
+    
     
@@ -1830,46 +1830,29 @@
 %22 + 
-replyToMessage.getFrom().get
+quotedMessage
 User
-N
+n
 ame
-()
  + %22
@@ -1870,16 +1870,36 @@
 dText);%0A
+                %7D);%0A
         
@@ -2457,16 +2457,17 @@
       if
+ 
 (telegra

Use recursion for decoding Telegram reply messages