package com.ruoyi.cai.manager;

import com.ruoyi.cai.domain.Account;
import com.ruoyi.cai.domain.User;
import com.ruoyi.cai.domain.UserChatRecord;
import com.ruoyi.cai.dto.app.dto.ImMessageDTO;
import com.ruoyi.cai.dto.app.vo.ImResp;
import com.ruoyi.cai.enums.GenderEnum;
import com.ruoyi.cai.enums.ImTypeEnum;
import com.ruoyi.cai.enums.SystemConfigEnum;
import com.ruoyi.cai.enums.UserMemberTypeEnum;
import com.ruoyi.cai.service.*;
import com.ruoyi.common.exception.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Set;

@Component
public class ImService {

    @Autowired
    private UserService userService;
    @Autowired
    private SystemConfigManager systemConfigManager;
    @Autowired
    private AccountService accountService;
    @Autowired
    private UserFollowService userFollowService;
    @Autowired
    private UserMemberService userMemberService;
    @Autowired
    private UserChatRecordService userChatRecordService;

    public ImResp sendMessage(Long fromUserId, ImMessageDTO message) {
        ImTypeEnum typeEnum = ImTypeEnum.getByType(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 == ImTypeEnum.CUSTOM){
            Account account = accountService.getByUserId(fromUserId);
            ImResp resp = new ImResp();
            resp.setCoin(account.getCoin()+ account.getIncomeCoin());
            resp.setContent(message.getContent());
            return resp;
        }
        Set<Long> systemCustomerService = systemConfigManager.getSystemConfigOfLongSet(SystemConfigEnum.SYSTEM_CUSTOMER_SERVICE);
        // 有内部用户参与的 跳过所有校验
        if(systemCustomerService.contains(fromUserId) || systemCustomerService.contains(toUserId)){
            Account account = accountService.getByUserId(fromUserId);
            ImResp resp = new ImResp();
            resp.setCoin(account.getCoin()+ account.getIncomeCoin());
            resp.setContent(message.getContent());
            return resp;
        }
        boolean fileType = typeEnum.isFileType();
        if(fileType){
            if(!systemCustomerService.contains(fromUserId) && !systemCustomerService.contains(toUserId)){
                boolean star = userFollowService.checkStar(toUserId, fromUserId);
                if(!star){
                    throw new ServiceException("对方关注你才能发送图片|语音|视频");
                }
            }
        }
        if(fromUserId.equals(toUserId)){
            throw new ServiceException("不能给自己发送哦!");
        }
        // 更新活跃时间
        // 检查拉黑
        boolean isBlack = userBlacklistService.existsBlack(fromUserId, toUserId);
        if(isBlack){
            throw new ServiceException("您已经将对方拉黑!");
        }
        isBlack = userBlacklistService.existsBlack(toUserId, fromUserId);
        if(isBlack){
            throw new ServiceException("对方已经将您拉黑!");
        }
        if (fromUser.getGender().equals(GenderEnum.WOMEN.getCode()) && toUser.getGender().equals(GenderEnum.WOMEN.getCode())) {
            throw new ServiceException("女生之间暂不支持私信!");
        }
        if (fromUser.getGender().equals(GenderEnum.MAN.getCode()) && toUser.getGender().equals(GenderEnum.MAN.getCode())) {
            throw new ServiceException("男生之间暂不支持私信!");
        }
        if (fromUser.getGender().equals(GenderEnum.MAN.getCode()) && toUser.getIsAnchor().equals(0)) {
            throw new ServiceException("只能给女神私信!");
        }
        if (fromUser.getGender().equals(GenderEnum.WOMEN.getCode()) && toUser.getIsAnchor().equals(0)) {
            throw new ServiceException("成为女神才能私信!");
        }
        if (fromUser.getIsAnchor().equals(1) && toUser.getIsAnchor().equals(1)) {
            throw new ServiceException("女神之间暂不支持私信!");
        }
        if (fromUser.getIsAnchor().equals(0) && toUser.getIsAnchor().equals(0)) {
            throw new ServiceException("目前只能和女神私信!");
        }
        // 正则判断违规数据替换
        if(typeEnum == ImTypeEnum.MESSAGE && !systemCustomerService.contains(fromUserId) && !systemCustomerService.contains(toUserId)){

        }
        if(fromUser.getIsAnchor().equals(1)){ // 女神发消息不要钱
            Account account = accountService.getByUserId(fromUserId);
            ImResp resp = new ImResp();
            resp.setCoin(account.getCoin()+ account.getIncomeCoin());
            resp.setContent(message.getContent());
            return resp;
        }
        // 判断VIP获取价格
        Long imPrice = getByImPrice(fromUserId);
        Long traceId = null;
        if(imPrice > 0){
            // 扣费
            traceId = accountService.imDesc(fromUser, toUser, imPrice);
        }
        // 存储聊天记录
        Account account = accountService.getByUserId(fromUserId);
        UserChatRecord record = userChatRecordService.saveRecord(fromUser, toUser, traceId, message);
        ImResp resp = new ImResp();
        resp.setCut(imPrice > 0);
        resp.setCutCoin(imPrice);
        resp.setCoin(account.getCoin()+ account.getIncomeCoin());
        resp.setRecordId(record.getId());
        resp.setContent(message.getContent());
        return resp;
    }

    @Autowired
    private UserBlacklistService userBlacklistService;

    private Long getByImPrice(Long userId){
        UserMemberTypeEnum userMemberType = userMemberService.checkUserMember(userId);
        if(userMemberType == null){
            return 10L;
        }
        if(userMemberType == UserMemberTypeEnum.NORMAL_VIP){
            return 5L;
        }
        if(userMemberType  == UserMemberTypeEnum.SUPER_VIP){
            return 0L;
        }
        return 10L;
    }
}
