package org.jebot.handler.impl.merchant;

import cn.hutool.json.JSONObject;
import com.pengrad.telegrambot.request.SendMessage;
import lombok.extern.slf4j.Slf4j;
import org.jebot.constant.Constant;
import org.jebot.handler.AbstractHandler;
import org.jebot.handler.dto.BotMessage;

import static org.jebot.constant.Constant.DISABLE_MCH;
import static org.jebot.constant.Constant.ENABLE_MCH;

@Slf4j
public class MerchantStatusHandler extends AbstractHandler {
    @Override
    public boolean msgHandler(BotMessage botMessage) {
        if (!botMessage.isUserAuthenticated()) {
            return false;
        }

        if (botMessage.messageTextIsEmpty()) {
            return false;
        }
        //禁用商户
        if (DISABLE_MCH.equals(botMessage.getMessageText())) {
            if (botMessage.getBotGroup() == null) {
                botMessage.getTelegramBot().execute(new SendMessage(botMessage.getMessage().chat().id(), "当前群组未绑定"));
                return true;
            }

            if (handlerManager.getMchService().updateMchStatus(botMessage.getBotGroup().getDataId(), Constant.CLOSE_MERCHANT) < 1) {
                botMessage.getTelegramBot().execute(new SendMessage(botMessage.getMessage().chat().id(), "关闭商户失败，请重试"));
                return true;
            } else {
                botMessage.getTelegramBot().execute(new SendMessage(botMessage.getMessage().chat().id(), "关闭商户成功"));
            }
            return true;
        }

        //启用商户
        if (ENABLE_MCH.equals(botMessage.getMessageText())) {
            if (botMessage.getBotGroup() == null) {
                botMessage.getTelegramBot().execute(new SendMessage(botMessage.getMessage().chat().id(), "当前群组未绑定"));
                return true;
            }
            if (handlerManager.getMchService().updateMchStatus(botMessage.getBotGroup().getDataId(), Constant.OPEN_MERCHANT) < 1) {
                botMessage.getTelegramBot().execute(new SendMessage(botMessage.getMessage().chat().id(), "开启商户失败，请重试"));
                return true;
            } else {
                botMessage.getTelegramBot().execute(new SendMessage(botMessage.getMessage().chat().id(), "开启商户成功"));
            }
            return true;
        }
        return false;
    }
}
