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

import com.stevesoltys.telegramirc.protocol.telegram.bot.TelegramBot;
import com.stevesoltys.telegramirc.protocol.telegram.message.decoder.FileDecoder;
import org.apache.log4j.Logger;
import org.telegram.telegrambots.TelegramApiException;
import org.telegram.telegrambots.api.methods.GetFile;
import org.telegram.telegrambots.api.objects.File;
import org.telegram.telegrambots.api.objects.Message;

import java.util.Collections;
import java.util.List;
import java.util.Optional;

/**
 * @author Steve Soltys
 */
public abstract class TelegramFileMessageDecoder {

    private static final String TELEGRAM_BOT_FILE_ENDPOINT = "https://api.telegram.org/file/bot";

    private final Logger logger = Logger.getLogger(this.getClass());

    List<String> decode(TelegramBot telegramBot, FileDecoder fileDecoder, String fileId) {
        Optional<File> fileOptional = getFile(telegramBot, fileId);

        if(fileOptional.isPresent()) {
            File file = fileOptional.get();
            String url = getFileUrl(telegramBot, file);

            if (file.getFileSize() < fileDecoder.maxFileSize()) {
                Optional<String> resultUrlOptional = fileDecoder.decode(url);

                if (resultUrlOptional.isPresent()) {
                    return Collections.singletonList(resultUrlOptional.get());
                }
            }
        }

        return Collections.emptyList();
    }

    private Optional<File> getFile(TelegramBot telegramBot, String fileId) {
        GetFile getFileMethod = new GetFile();
        getFileMethod.setFileId(fileId);

        try {
            return Optional.ofNullable(telegramBot.getFile(getFileMethod));

        } catch (TelegramApiException e) {
            logger.error("Error while downloading Telegram sticker image", e);
        }

        return Optional.empty();
    }

    private String getFileUrl(TelegramBot telegramBot, File file) {
        return TELEGRAM_BOT_FILE_ENDPOINT + telegramBot.getBotToken() + "/" + file.getFilePath();
    }

    abstract List<String> decode(TelegramBot telegramBot, Message message);
}

@@ -477,69 +477,9 @@
 til.
-Collections;%0Aimport java.util.List;%0Aimport java.util.Optional
+*
 ;%0A%0A/
@@ -726,24 +726,92 @@
 tClass());%0A%0A
+    private final Map%3CString, String%3E fileCache = new HashMap%3C%3E();%0A%0A
     List%3CStr
@@ -1037,24 +1037,182 @@
 onal.get();%0A
+%0A            if (fileCache.containsKey(file.getFileId())) %7B%0A                return Collections.singletonList(fileCache.get(file.getFileId()));%0A            %7D%0A%0A
             
@@ -1446,32 +1446,161 @@
 .isPresent()) %7B%0A
+                    String resultUrl = resultUrlOptional.get();%0A%0A                    fileCache.put(file.getFileId(), resultUrl);%0A
                 
@@ -1645,30 +1645,16 @@
 esultUrl
-Optional.get()
 );%0A     

Add file caching support