package org.jebot.handler.impl.channel;

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

import java.util.regex.Pattern;

import static org.jebot.constant.Constant.DISABLE_CHANNEL_FLAG;
import static org.jebot.constant.Constant.ENABLE_CHANNEL_FLAG;

@Slf4j
public class ChannelStausHandler extends AbstractHandler {


    //根据通道编号启用通道
    private static final Pattern ENABLE_CHANNEL = Pattern.compile("^" + ENABLE_CHANNEL_FLAG + "[a-zA-Z0-9]+(,[a-zA-Z0-9]+)*$");


    //根据通道编号禁用通道
    private static final Pattern DISABLE_CHANNEL = Pattern.compile("^" + DISABLE_CHANNEL_FLAG + "[a-zA-Z0-9]+(,[a-zA-Z0-9]+)*$");


    @Override
    public boolean msgHandler(BotMessage botMessage) {

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

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

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

        //群组内管理员发送打开通道命令
        if (ENABLE_CHANNEL.matcher(botMessage.getMessageText()).matches()) {
            StringBuilder sb = new StringBuilder();
            for (String channelId : getChannelIds(botMessage.getMessageText(), ENABLE_CHANNEL_FLAG)) {
                //开启通道
                if (handlerManager.getPassageRepository().updateStatusByIdI(Constant.OPEN_CHANNEL, Long.valueOf(channelId)) < 1) {
                    sb.append(channelId).append(":").append("开启失败").append("\r\n");
                } else {
                    sb.append(channelId).append(":").append("开启成功\r\n");
                }
            }
            SendMessage sendMessage = new SendMessage(botMessage.getMessage().chat().id(), sb.toString());
            sendMessage.replyParameters(new ReplyParameters(botMessage.getMessage().messageId()));
            SendResponse execute = botMessage.getTelegramBot().execute(sendMessage);
            if (!execute.isOk()) {
                log.error("开启通道结果发送失败: {}", execute);
            }
            return true;
        }

        //群组内管理员发送关闭通道命令
        if (DISABLE_CHANNEL.matcher(botMessage.getMessageText()).matches()) {
            StringBuilder sb = new StringBuilder();
            for (String channelId : getChannelIds(botMessage.getMessageText(), DISABLE_CHANNEL_FLAG)) {
                //关闭通道
                if (handlerManager.getPassageRepository().updateStatusByIdI(Constant.CLOSE_CHANNEL, Long.valueOf(channelId)) < 1) {
                    sb.append(channelId).append(":").append("关闭失败").append("\r\n");
                } else {
                    sb.append(channelId).append(":").append("关闭成功\r\n");
                }
            }
            SendMessage sendMessage = new SendMessage(botMessage.getMessage().chat().id(), sb.toString());
            sendMessage.replyParameters(new ReplyParameters(botMessage.getMessage().messageId()));
            SendResponse execute = botMessage.getTelegramBot().execute(sendMessage);
            if (!execute.isOk()) {
                log.error("关闭通道结果发送失败: {}", execute);
            }
            return true;
        }
        return false;
    }

    private static String[] getChannelIds(String text, String replaceStr) {
        String channelIdsStr = text.replace(replaceStr, "");
        if (channelIdsStr.contains(",")) {
            return channelIdsStr.split(",");
        }
        return new String[]{channelIdsStr};
    }

}
