package com.sonic.pigeon.service.impl;

import com.sonic.common.event.Event;
import com.sonic.common.event.EventProducer;
import com.sonic.common.event.RabbitmqEventProducer;
import com.sonic.pigeon.domain.bo.ImUserBo;
import com.sonic.pigeon.domain.input.ReceiveImMsgInput;
import com.sonic.pigeon.event.outer.payload.AiChatPayload;
import com.sonic.pigeon.event.outer.payload.UserBalanceInsufficientCheckoutPayload;
import com.sonic.pigeon.lib.enums.ImUserTypeEnum;
import com.sonic.pigeon.lib.enums.MessageTypeEnum;
import com.sonic.pigeon.service.CommonSendMqService;
import com.sonic.pigeon.utils.ImGenerator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import static com.sonic.pigeon.event.outer.EventType.*;

/**
 * 发送消息到mq
 *
 * @Author zzhan
 * @Date 2022/3/2
 * @Version 1.0
 */
@Slf4j
@Service
public class CommonSendMqServiceImpl implements CommonSendMqService {

    @Autowired
    private EventProducer eventProducer;

    @Autowired
    private ImGenerator imGenerator;

    @Autowired
    @Qualifier("aiChatMeta")
    private RabbitmqEventProducer.RabbitmqMessageMeta aiChatMeta;
    @Autowired
    @Qualifier("aiChatToFrogMeta")
    private RabbitmqEventProducer.RabbitmqMessageMeta aiChatToFrogMeta;
    @Autowired
    @Qualifier("userBalanceInsufficientCheckoutMeta")
    private RabbitmqEventProducer.RabbitmqMessageMeta userBalanceInsufficientCheckoutMeta;


    @Override
    public void sendAiChatMq(ReceiveImMsgInput input) {
        //处理逻辑，判断发送人。接收人
        ImUserBo fromUserBo = imGenerator.getImUserBo(input.getFromAccount());
        ImUserBo toUserBo = imGenerator.getImUserBo(input.getTo());
        //只有普通用户给AI机器人发送消息 才进行处理
        if(ImUserTypeEnum.u != fromUserBo.getImUserType() && ImUserTypeEnum.r != toUserBo.getImUserType()) {
            //快速返回，不进行处理
            return;
        }
        //发送MQ消息
        eventProducer.send(Event.builder()
                .eventScene(AI_CHAT.getEventCode().getScene())
                .eventModule(AI_CHAT.getEventCode().getModule())
                .eventName(AI_CHAT.getEventCode().getName())
                .data(AiChatPayload.builder()
                        .fromUserId(fromUserBo.getUserId())
                        .toUserId(toUserBo.getUserId())
                        .messageType(input.getMsgType())
                        .content(input.getBody())
                        .attach(input.getAttach())
                        .build()).build(), aiChatMeta);
    }

    @Override
    public void sendAiChatToFrogMq(ReceiveImMsgInput input) {
        //处理逻辑，判断发送人。接收人
        ImUserBo fromUserBo = imGenerator.getImUserBo(input.getFromAccount());
        ImUserBo toUserBo = imGenerator.getImUserBo(input.getTo());
        //只有普通用户给AI机器人发送消息 才进行处理
        if(ImUserTypeEnum.u != fromUserBo.getImUserType() && ImUserTypeEnum.r != toUserBo.getImUserType()) {
            //快速返回，不进行处理
            return;
        }
        eventProducer.send(Event.builder()
                .eventScene(AI_CHAT_TO_FROG.getEventCode().getScene())
                .eventModule(AI_CHAT_TO_FROG.getEventCode().getModule())
                .eventName(AI_CHAT_TO_FROG.getEventCode().getName())
                .data(AiChatPayload.builder()
                        .fromUserId(fromUserBo.getUserId())
                        .toUserId(toUserBo.getUserId())
                        .messageType(input.getMsgType())
                        .content(input.getBody())
                        .attach(input.getAttach())
                        .build()).build(), aiChatToFrogMeta);
    }

    @Override
    public void sendAiChatMqV1(Long fromUserId, Long toUserId, String content) {
        //发送MQ消息
        eventProducer.send(Event.builder()
                .eventScene(AI_CHAT.getEventCode().getScene())
                .eventModule(AI_CHAT.getEventCode().getModule())
                .eventName(AI_CHAT.getEventCode().getName())
                .data(AiChatPayload.builder()
                        .fromUserId(fromUserId)
                        .toUserId(toUserId)
                        .messageType(MessageTypeEnum.TEXT.name())
                        .content(content)
                        .build()).build(), aiChatMeta);
    }

    @Override
    public void sendAiChatMqV2(Long fromUserId, Long toUserId, String content) {
        //发送MQ消息
        eventProducer.send(Event.builder()
                .eventScene(AI_CHAT.getEventCode().getScene())
                .eventModule(AI_CHAT.getEventCode().getModule())
                .eventName(AI_CHAT.getEventCode().getName())
                .data(AiChatPayload.builder()
                        .fromUserId(fromUserId)
                        .toUserId(toUserId)
                        .messageType(MessageTypeEnum.CUSTOM.name())
                        .content(content)
                        .attach("{\"type\":\"image\",\"url\":\"https://hhb.crushlevel.ai/dev/main/role/439058245812225/17557485208684304.png\",\"width\":800,\"height\":960}")
                        .build()).build(), aiChatMeta);
    }

    @Override
    public void userBalanceInsufficientCheckoutMq(Long userId) {
        //发送MQ消息
        eventProducer.send(Event.builder()
                .eventScene(USER_BALANCE_INSUFFICIENT_CHECKOUT.getEventCode().getScene())
                .eventModule(USER_BALANCE_INSUFFICIENT_CHECKOUT.getEventCode().getModule())
                .eventName(USER_BALANCE_INSUFFICIENT_CHECKOUT.getEventCode().getName())
                .data(UserBalanceInsufficientCheckoutPayload.builder()
                        .userId(userId).build()).build(), userBalanceInsufficientCheckoutMeta);
    }
}
