package org.jebot.handler.impl.channel;

import cn.hutool.core.date.DateUtil;
import com.pengrad.telegrambot.request.SendPhoto;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.util.Strings;
import org.jebot.handler.AbstractHandler;
import org.jebot.handler.dto.BotMessage;
import org.jebot.models.jebot.BotAccountBookHistory;
import org.jebot.util.ImageUtil;
import org.jebot.util.SnowflakeId;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.Date;
import java.util.List;

import static org.jebot.constant.Constant.*;

@Slf4j
public class ChannelAccountBookHistoryHandler extends AbstractHandler {
    @Override
    public boolean msgHandler(BotMessage message) {

        //只处理通道群组消息
        if (!message.isGroupChannel()) {
            return false;
        }

        if (message.messageTextIsEmpty()) {
            return false;
        }

        String messageText = message.getMessageText();
        if (!AGENT_ACCOUNT_HISTORY_FLAG.equals(messageText) && !PAYMENT_ACCOUNT_HISTORY_FLAG.equals(messageText)) {
            return false;
        }

        if (AGENT_ACCOUNT_HISTORY_FLAG.equals(messageText)) {
            List<BotAccountBookHistory> bookHistories = handlerManager.getAccountBookHistoryRepository().findByBelongIdAndBelongTypeAndType(message.getBotGroup().getGroupId(), DATA_TYPE_CHANNEL, AGENT, DateUtil.offsetHour(new Date(), -24));
            String format = String.format("./%d.png", SnowflakeId.getId());
            File imageFile = null;
            try {
                ImageUtil.generateTableImage(handlerManager.loadFontFromResources(), bookHistories, format);
                imageFile = new File(format);
                SendPhoto sendPhoto = new SendPhoto(message.getMessage().chat().id(), imageFile);
                message.getTelegramBot().execute(sendPhoto);
            } catch (Exception e) {
                log.error("生成图片失败,错误信息:{}", e.getMessage());
                e.printStackTrace();
            } finally {
                if (imageFile != null && imageFile.exists()) {
                    imageFile.delete();
                }
            }
            return true;
        }

        if (PAYMENT_ACCOUNT_HISTORY_FLAG.equals(messageText)) {
            List<BotAccountBookHistory> bookHistories = handlerManager.getAccountBookHistoryRepository().findByBelongIdAndBelongTypeAndType(message.getBotGroup().getGroupId(), DATA_TYPE_CHANNEL, PAYMENT, DateUtil.offsetHour(new Date(), -24));
            String format = String.format("./%d.png", SnowflakeId.getId());
            File imageFile = null;
            try {
                ImageUtil.generateTableImage(handlerManager.loadFontFromResources(), bookHistories, format);
                imageFile = new File(format);
                SendPhoto sendPhoto = new SendPhoto(message.getMessage().chat().id(), imageFile);
                message.getTelegramBot().execute(sendPhoto);
            } catch (Exception e) {
                log.error("生成图片失败,错误信息:{}", e.getMessage());
                e.printStackTrace();
            } finally {
                if (imageFile != null && imageFile.exists()) {
                    imageFile.delete();
                }
            }
            return true;
        }
        return false;
    }
}
