package cn.van.business.util;


import cn.van.business.enums.FromType;
import cn.van.business.model.wx.WxMessage;
import cn.van.business.repository.SettingRepository;
import cn.van.business.repository.WxMessageDataForChatRepository;
import cn.van.business.repository.WxUserRepository;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static cn.van.business.util.WXUtil.*;


/**
 * @author Leo
 * @version 1.0
 * @create 2023/12/19 0019 上午 11:03
 * @description：
 */
@Component
public class WxMessageConsumer {
    private static final Logger logger = LoggerFactory.getLogger(WxMessageConsumer.class);

    private final JDUtil jdUtils;

    @Autowired
    public WxMessageConsumer(@Lazy JDUtil jdUtils) {
        this.jdUtils = jdUtils;
    }

    @Async("threadPoolTaskExecutor")
    public void consume(WxMessage wxMessage) throws Exception {
        if (wxMessage.getEvent() == null) {
            return;
        }

        if (FromType.PRIVATE.getKey().equals(wxMessage.getEvent())) {
            handlePrivateMessage(wxMessage);
        } else if (FromType.GROUP.getKey().equals(wxMessage.getEvent())) {
            handleGroupMessage(wxMessage);
        }
    }

    /**
     * 处理私聊消息
     *
     * @param wxMessage
     */
    private void handlePrivateMessage(WxMessage wxMessage) throws Exception {
        logger.info("处理私聊消息： {}", JSON.toJSONString(wxMessage));

        WxMessage.DataSection data = wxMessage.getData();
        WxMessage.DataSection.InnerData innerData = data.getData();
        if (Util.isAnyEmpty(innerData.getMsg(), innerData.getFromWxid())) {
            logger.info("消息内容为空，不处理");
            return;
        } else {
            logger.info("消息内容：{}", innerData.getMsg());
        }
        String fromWxid = innerData.getFromWxid();
        WXUtil.SuperAdmin superAdmin = getSuperAdmins(fromWxid).get(0);
        if (Util.isEmpty(superAdmin)) {
            logger.info("不是超管消息，不处理");
            return;
        }
        String msg = innerData.getMsg();
        if (msg.startsWith("京")) {
            logger.info("消息以京开头，处理京东指令消息");
            jdUtils.sendOrderToWxByOrderJD(msg.replace("京", ""), fromWxid);
            return;
        }
        if (msg.startsWith("单")) {
            logger.info("消息以单开头，处理单指令消息");
            jdUtils.sendOrderToWxByOrderD(msg.replace("单", ""), fromWxid);
            return;
        }
        logger.info("未命中前置指令，开始命中 Default 流程");
        jdUtils.sendOrderToWxByOrderDefault(msg, fromWxid);
    }

    private void handleGroupMessage(WxMessage wxMessage) {
        logger.info("处理群聊消息： {}", JSON.toJSONString(wxMessage));

        WxMessage.DataSection data = wxMessage.getData();
        WxMessage.DataSection.InnerData innerData = data.getData();
        if (Util.isAnyEmpty(innerData.getMsg(), innerData.getFromWxid())) {
            logger.info("消息内容为空，不处理");
            return;
        } else {
            logger.info("消息内容：{}", innerData.getMsg());
        }

        String fromWxid = innerData.getFromWxid();
        String msg = innerData.getMsg();

        if (chatRoom_xb.containsKey(fromWxid)) {
            logger.info("线报群消息");
            jdUtils.xb(msg, fromWxid);
            return;
        }
        // 超级管理员内部群
        if (chatRoom_admin_inner.contains(fromWxid)) {
            if (msg.startsWith("单")) {
                logger.info("消息以单开头，处理单指令消息");
                jdUtils.sendOrderToWxByOrderD(msg.replace("单", ""), fromWxid);
                return;
            }
            logger.info("未命中前置指令，开始命中 Default 流程");
            jdUtils.sendOrderToWxByOrderDefault(msg, fromWxid);
        }
        // 可以对外的群聊
        if (!chatRoom_admin.contains(fromWxid)) {
            logger.info("不是白名单群聊，不处理");
            return;
        }
        if (msg.startsWith("评")) {
            if (("47484514467@chatroom".equals(fromWxid))) {
                logger.info("消息以评开头，处理评价指令消息");
                jdUtils.sendOrderToWxByOrderP(msg.replace("评", ""), fromWxid);
                return;
            }
        }


    }

}
