package org.jebot.handler.impl.merchant;

import com.pengrad.telegrambot.request.SendMessage;
import org.jebot.handler.AbstractHandler;
import org.jebot.handler.dto.BotMessage;
import org.jebot.models.jebot.BotGroup;
import org.jebot.repository.jebot.BotGroupRepository;

import java.util.regex.Pattern;

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

public class MerchantWarnHandler extends AbstractHandler {
    private static final Pattern UPDATE_MCH_AGENT_WARN_THRESHOLD = Pattern.compile("^" + UPDATE_MCH_AGENT_WARN_THRESHOLD_FLAG + "-?\\d+(\\.\\d+)?$");

    private static final Pattern UPDATE_MCH_PAYMENT_WARN_THRESHOLD = Pattern.compile("^" + UPDATE_MCH_PAYMENT_WARN_THRESHOLD_FLAG + "-?\\d+(\\.\\d+)?$");

    @Override
    public boolean msgHandler(BotMessage botMessage) {

        if (botMessage.isGroupMch()) {
            return false;
        }

        if (!botMessage.isUserAuthenticated()) {
            return false;
        }

        if (botMessage.messageTextIsEmpty()) {
            return false;
        }
        String text = botMessage.getMessageText();

        //设置商户代收预警值
        if (UPDATE_MCH_PAYMENT_WARN_THRESHOLD.matcher(text).find()) {
            if (botMessage.getBotGroup() == null) {
                botMessage.getTelegramBot().execute(new SendMessage(botMessage.getMessage().chat().id(), "当前群组未绑定"));
                return true;
            }
            String warnThresholdStr = text.replace(UPDATE_MCH_PAYMENT_WARN_THRESHOLD_FLAG, "");
            double warnThreshold = Double.parseDouble(warnThresholdStr);
            if (0 > warnThreshold || warnThreshold > 50) {
                botMessage.getTelegramBot().execute(new SendMessage(botMessage.getMessage().chat().id(), "预警值范围在0-50之间,0表示不预警"));
                return true;
            }
            BotGroup botGroup = botMessage.getBotGroup();
            BotGroupRepository groupRepository = handlerManager.getGroupRepository();
            groupRepository.updatePaymentWarningThresholdByIdAndPaymentWarningThreshold(warnThreshold, botGroup.getId());
            botMessage.getTelegramBot().execute(new SendMessage(botMessage.getMessage().chat().id(), "设置成功"));
            return true;
        }

        //设置商户代收预警值
        if (UPDATE_MCH_AGENT_WARN_THRESHOLD.matcher(text).find()) {
            if (botMessage.getBotGroup() == null) {
                botMessage.getTelegramBot().execute(new SendMessage(botMessage.getMessage().chat().id(), "当前群组未绑定"));
                return true;
            }
            String warnThresholdStr = text.replace(UPDATE_MCH_AGENT_WARN_THRESHOLD_FLAG, "");
            double warnThreshold = Double.parseDouble(warnThresholdStr);
            if (0 > warnThreshold || warnThreshold > 50) {
                botMessage.getTelegramBot().execute(new SendMessage(botMessage.getMessage().chat().id(), "预警值范围在0-50之间,0表示不预警"));
                return true;
            }
            BotGroup botGroup = botMessage.getBotGroup();
            BotGroupRepository groupRepository = handlerManager.getGroupRepository();
            groupRepository.updateAgentWarningThresholdByIdAndAgentWarningThreshold(warnThreshold, botGroup.getId());
            botMessage.getTelegramBot().execute(new SendMessage(botMessage.getMessage().chat().id(), "设置成功"));
            return true;
        }
        return false;
    }
}
