package com.beolnix.marvin.im.telegram.telegram;

import com.beolnix.marvin.im.IMSessionUtils;
import com.beolnix.marvin.im.api.model.IMIncomingMessage;
import com.beolnix.marvin.im.api.model.IMIncomingMessageBuilder;
import com.beolnix.marvin.im.telegram.TelegramIMSession;
import com.beolnix.marvin.im.telegram.model.TelegramBotSettings;
import com.beolnix.marvin.plugins.api.PluginsManager;
import org.apache.log4j.Logger;
import org.springframework.util.StringUtils;
import org.telegram.telegrambots.api.objects.Message;
import org.telegram.telegrambots.api.objects.Update;
import org.telegram.telegrambots.bots.TelegramLongPollingBot;

import java.util.Calendar;

public class TelegramBot extends TelegramLongPollingBot {

    private Logger logger = Logger.getLogger(getClass().getName());

    private final TelegramBotSettings botSettings;
    private final PluginsManager pluginManager;
    private final IMSessionUtils imSessionUtils;

    public TelegramBot(TelegramBotSettings botSettings, PluginsManager pluginManager, IMSessionUtils imSessionUtils) {
        this.botSettings = botSettings;
        this.pluginManager = pluginManager;
        this.imSessionUtils = imSessionUtils;
    }

    @Override
    public String getBotToken() {
        return botSettings.getToken();
    }

    @Override
    public void onUpdateReceived(Update update) {
        Message message = update.getMessage();
        logger.debug("update received: " + update.toString());
        pluginManager.process(convert(message));
    }

    private IMIncomingMessage convert(Message message) {
        IMIncomingMessageBuilder imBuilder = new IMIncomingMessageBuilder()
                .withBotName(botSettings.getName())
                .withAutor(parseAutor(message))
                .withRawMessageBody(message.getText())
                .withCommand(message.isCommand())
                .withConference(true)
                .withProtocol(TelegramIMSession.PROTOCOL)
                .withCommandSymbol(TelegramIMSession.COMMAND_SYMBOL)
                .withTimestamp(Calendar.getInstance())
                .withConferenceName(message.getChatId().toString());

        imSessionUtils.parseCommand(message.getText(), TelegramIMSession.COMMAND_SYMBOL)
                .ifPresent(imBuilder::withCommandName);

        return imBuilder.build();
    }

    private String parseAutor(Message message) {
        String userName = message.getFrom().getUserName();
        if (!StringUtils.isEmpty(userName)) {
            return userName;
        }

        return message.getFrom().getFirstName() + " " + message.getFrom().getLastName();
    }

    @Override
    public String getBotUsername() {
        return botSettings.getName();
    }
}

@@ -939,16 +939,57 @@
 Utils;%0A%0A
+    private final String botNameSuffix;%0A%0A
     publ
@@ -1095,24 +1095,24 @@
 ionUtils) %7B%0A
-
         this
@@ -1229,16 +1229,74 @@
 nUtils;%0A
+        this.botNameSuffix = %22@%22 + botSettings.getName();%0A
     %7D%0A%0A 
@@ -1667,32 +1667,398 @@
 sage message) %7B%0A
+%0A        // it is needed to let plugins created for non-telegram protocols correctly process the command%0A        String msgText = message.getText();%0A        if (msgText.endsWith(botNameSuffix)) %7B%0A            msgText = msgText.replace(botNameSuffix, %22%22);%0A            logger.info(%22Removed bot name suffix %22 + botNameSuffix + %22 from the original message.%22);%0A        %7D%0A%0A
         IMIncomi
@@ -2254,32 +2254,22 @@
 geBody(m
-essage.get
+sg
 Text
-()
 )%0A      
@@ -2639,32 +2639,22 @@
 ommand(m
-essage.get
+sg
 Text
-()
 , Telegr

fix bug with bot name suffix in telegram commands