package com.ruoyi.xq.manager;

import com.github.houbb.sensitive.word.bs.SensitiveWordBs;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.xq.domain.User;
import com.ruoyi.xq.domain.UserChatRecord;
import com.ruoyi.xq.domain.UserVip;
import com.ruoyi.xq.dto.app.im.ImMessageDTO;
import com.ruoyi.xq.dto.app.im.ImResp;
import com.ruoyi.xq.enums.ErrorEnum;
import com.ruoyi.xq.service.UserChatFilterService;
import com.ruoyi.xq.service.UserChatRecordService;
import com.ruoyi.xq.service.UserService;
import com.ruoyi.xq.service.UserVipService;
import com.ruoyi.yunxin.enums.YxImTypeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class ImManager {

    @Autowired
    private UserChatRecordService userChatRecordService;
    @Autowired
    private SensitiveWordBs sensitiveWordBs;
    @Autowired
    private UserChatFilterService userChatFilterService;
    @Autowired
    private UserService userService;
    @Autowired
    private UserVipService userVipService;

    public ImResp sendMessage(ImMessageDTO message) {
        Long fromUserId = message.getFromUserId();
        YxImTypeEnum typeEnum = YxImTypeEnum.getByCode(message.getType());
        if(typeEnum == null || fromUserId == null){
            throw new ServiceException("参数异常");
        }
        // 检测用户是否被封号
        User fromUser = userService.getById(fromUserId);
        if(fromUser == null){
            throw new ServiceException("发送人不存在!");
        }
        if(fromUser.getStatus() == 1){
            throw new ServiceException("发送人账号不可用!");
        }
        Long toUserId = message.getToUserId();
        User toUser = userService.getById(toUserId);
        if(toUser == null){
            throw new ServiceException("接收人不存在!");
        }
        if(toUser.getStatus() == 1){
            throw new ServiceException("接收人账号不可用!");
        }
        // 自定义消息跳过所有流程
        if(typeEnum == YxImTypeEnum.CUSTOM){
            ImResp resp = new ImResp();
            resp.setContent(message.getContent());
            return resp;
        }
        boolean inner = fromUser.getType().equals(1) || toUser.getType().equals(1);
        // 有内部用户参与的 跳过所有校验
        if(inner){
            ImResp resp = new ImResp();
            resp.setContent(message.getContent());
            return resp;
        }
        boolean fileType = typeEnum.isFileType();
        if(fileType){
            // 发送文件
        }
        if(fromUserId.equals(toUserId)){
            throw new ServiceException("不能给自己发送哦!");
        }
        // 判断VIP 只有VIP才可以发送消息
        if(!fromUser.getOpenVip()){
            throw new ServiceException(ErrorEnum.VIP_AUTH);
        }
        // 检查拉黑
        int filter = 0;
        String content = message.getContent();
        // 正则判断违规数据替换
        if(typeEnum == YxImTypeEnum.TXT){
            boolean contains = sensitiveWordBs.contains(message.getContent());
            if(contains){
                filter = 1;
                content = sensitiveWordBs.replace(message.getContent());
            }
        }
        UserChatRecord record = userChatRecordService.saveRecord(fromUser, toUser, message);
        if(filter == 1){
            userChatFilterService.saveFilter(fromUser, toUser, message.getContent(),content);
        }
        ImResp resp = new ImResp();
        resp.setRecordId(record.getId());
        resp.setFilter(filter);
        resp.setContent(content);
        return resp;
    }


}
