package org.jebot.handler;


import com.pengrad.telegrambot.TelegramBot;
import com.pengrad.telegrambot.model.Update;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.jebot.handler.dto.BotMessage;
import org.jebot.handler.impl.AuthHandler;
import org.jebot.handler.impl.HelpHandler;
import org.jebot.handler.impl.IdHandler;
import org.jebot.handler.impl.SqlInjectionFilterHandler;
import org.jebot.handler.impl.admin.AdminHandler;
import org.jebot.handler.impl.calculator.CalculatorHandler;
import org.jebot.handler.impl.change.GroupChangeHandler;
import org.jebot.handler.impl.channel.*;
import org.jebot.handler.impl.complaint.ComplaintHandler;
import org.jebot.handler.impl.merchant.*;
import org.jebot.handler.impl.price.PriceQueryHandler;
import org.jebot.repository.jebot.*;
import org.jebot.repository.xxpay.MchAccountRepository;
import org.jebot.repository.xxpay.PayOrderRepository;
import org.jebot.repository.xxpay.PayPassageRepository;
import org.jebot.repository.xxpay.PayProductRepository;
import org.jebot.service.IAccountBookService;
import org.jebot.service.IMchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.awt.*;
import java.io.File;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Component
@Getter
public class HandlerManager {


    @Resource
    BotUserRepository userRepository;


    @Resource
    BotGroupRepository groupRepository;

    @Resource
    BotComplaintRepository complaintRepository;

    @Resource
    BotAccountBookRepository accountBookRepository;


    @Resource
    BotAccountBookHistoryRepository accountBookHistoryRepository;


    @Resource
    PayOrderRepository payOrderRepository;

    @Resource
    PayPassageRepository passageRepository;

    @Resource
    PayProductRepository productRepository;

    @Resource
    MchAccountRepository mchAccountRepository;

    @Resource
    IAccountBookService accountBookService;

    @Resource
    IMchService mchService;

    private final List<AbstractHandler> handlers = new ArrayList<>();

    public HandlerManager() {
        // 无需认证的处理器
        addHandler(new SqlInjectionFilterHandler());
        addHandler(new HelpHandler());
        addHandler(new IdHandler());
        addHandler(new CalculatorHandler());
        addHandler(new PriceQueryHandler());
        //认证处理器
        addHandler(new AuthHandler());

        //投诉处理
        addHandler(new ComplaintHandler());

        //群组变更处理
        addHandler(new GroupChangeHandler());

        //添加或删除管理
        addHandler(new AdminHandler());

        //通道相关
        addHandler(new ChannelInfoHandler());
        addHandler(new ChannelStausHandler());
        addHandler(new ChannelSuccessRateHandler());
        addHandler(new ChannelAccountBookHandler());
        addHandler(new ChannelAccountBookHistoryHandler());
        addHandler(new ChannelBindGroupOrUnbindGroupHandler());
        //商户相关
        addHandler(new MerchantInfoHandler());
        addHandler(new MerchantStatusHandler());
        addHandler(new MerchantWarnHandler());
        addHandler(new MerchantSuccessRateHandler());
        addHandler(new MerchantDropOrderHandler());
        addHandler(new MerchantAccountBookHandler());
        addHandler(new MerchantAccountBookHistoryHandler());
        addHandler(new MerchantOneClickSettlementHandler());
        addHandler(new MerchantBindGroupOrUnbindGroupHandler());
    }


    // 添加处理器
    public void addHandler(AbstractHandler handler) {
        handler.handlerManager = this;
        handlers.add(handler);
    }

    /**
     * 处理消息
     */
    public void handle(TelegramBot telegramBot, List<Update> updates) {
        updates.forEach(update -> {
            if (update.message() == null) return;
            BotMessage botMessage = new BotMessage(telegramBot, update.message(), update);
            for (AbstractHandler handler : this.handlers) {
                try {
                    if (handler.msgHandler(botMessage)) {
                        break;
                    }
                } catch (Exception e) {
                    log.error("处理消息失败: {}", e.getMessage());
                    e.printStackTrace();
                }
            }
        });
    }

    public Font loadFontFromResources() {
        try (InputStream fontStream = getClass().getClassLoader().getResourceAsStream("msyh.ttf")) {
            if (fontStream == null) {
                log.error("字体文件未找到，请确保 msyh.ttf 存在于 resources 目录下");
                return null;
            }
            Font chineseFont = Font.createFont(Font.TRUETYPE_FONT, fontStream);
            chineseFont = chineseFont.deriveFont(Font.PLAIN, 16);
            log.info("成功加载字体: {}", chineseFont.getFontName());
            return chineseFont;
        } catch (Exception e) {
            log.error("加载字体失败: {}", e.getMessage());
            e.printStackTrace();
            return null;
        }
    }

}
