package org.jebot.handler.impl.admin;

import com.pengrad.telegrambot.request.SendMessage;
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.BotUser;

import java.util.regex.Pattern;

import static org.jebot.constant.Constant.ADD_ADMIN;
import static org.jebot.constant.Constant.DEL_ADMIN;

@Slf4j
public class AdminHandler extends AbstractHandler {


    private final Pattern CREATE_ADMIN = Pattern.compile("^" + ADD_ADMIN + "[a-zA-Z0-9_]*$");
    private final Pattern DELETE_ADMIN = Pattern.compile("^" + DEL_ADMIN + "[a-zA-Z0-9_]*$");

    @Override
    public boolean msgHandler(BotMessage botMessage) {
        if (!botMessage.isUserAuthenticated()) {
            return false;
        }

        String text = botMessage.getMessage().text();
        if (Strings.isNotEmpty(text) && CREATE_ADMIN.matcher(text).find()) {
            String userName = text.replace(ADD_ADMIN, "");
            // 检查用户名是否存在
            BotUser existingUser = this.handlerManager.getUserRepository().findByUserName(userName);
            if (existingUser != null) {
                // 将群组 ID 发回
                SendMessage sendMessage = new SendMessage(botMessage.getMessage().chat().id(), "用户已是管理");
                botMessage.getTelegramBot().execute(sendMessage);
                return true;
            }
            BotUser botUser = new BotUser();
            botUser.setUserName(userName);
            BotUser save = this.handlerManager.getUserRepository().save(botUser);
            if (save != null) {
                // 将群组 ID 发回
                SendMessage sendMessage = new SendMessage(botMessage.getMessage().chat().id(), "添加成功");
                botMessage.getTelegramBot().execute(sendMessage);
                return true;
            } else {
                // 将群组 ID 发回
                SendMessage sendMessage = new SendMessage(botMessage.getMessage().chat().id(), "添加失败");
                botMessage.getTelegramBot().execute(sendMessage);
                return true;
            }
        }
        if (Strings.isNotEmpty(text) && DELETE_ADMIN.matcher(text).find()) {
            String userName = text.replace(DEL_ADMIN, "");
            // 检查用户名是否存在
            BotUser existingUser = this.handlerManager.getUserRepository().findByUserName(userName);
            if (existingUser == null) {
                // 将群组 ID 发回
                SendMessage sendMessage = new SendMessage(botMessage.getMessage().chat().id(), "用户不存在");
                botMessage.getTelegramBot().execute(sendMessage);
                return true;
            }
            this.handlerManager.getUserRepository().delete(existingUser);
            // 将群组 ID 发回
            SendMessage sendMessage = new SendMessage(botMessage.getMessage().chat().id(), "删除成功");
            botMessage.getTelegramBot().execute(sendMessage);
            return true;
        }
        return false;
    }
}
