package com.sonic.frog.service.impl;


import com.sonic.common.event.Event;
import com.sonic.common.event.EventProducer;
import com.sonic.common.event.RabbitmqEventProducer;
import com.sonic.frog.enums.MessageTypeEnum;
import com.sonic.frog.event.inner.payload.*;
import com.sonic.frog.event.outer.payload.AiChatPayload;
import com.sonic.frog.service.CommonSendMqService;
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 java.math.BigDecimal;

import static com.sonic.frog.enums.Constants.threshold;
import static com.sonic.frog.event.inner.EventType.*;
import static com.sonic.frog.event.outer.EventType.AI_CHAT;

/**
 * 发送消息到im的mq
 */
@Service
@Slf4j
public class CommonSendMqServiceImpl implements CommonSendMqService {

    @Autowired
    private EventProducer eventProducer;

    @Autowired
    @Qualifier("aiImInfoMeta")
    private RabbitmqEventProducer.RabbitmqMessageMeta aiImInfoMeta;
    @Autowired
    @Qualifier("calcHeartbeatLevelMeta")
    private RabbitmqEventProducer.RabbitmqMessageMeta calcHeartbeatLevelMeta;
    @Autowired
    @Qualifier("aiChatMeta")
    private RabbitmqEventProducer.RabbitmqMessageMeta aiChatMeta;
    @Autowired
    @Qualifier("subtractHeartbeatValMeta")
    private RabbitmqEventProducer.RabbitmqMessageMeta subtractHeartbeatValMeta;
    @Autowired
    @Qualifier("calcHeartbeatScoreMeta")
    private RabbitmqEventProducer.RabbitmqMessageMeta calcHeartbeatScoreMeta;
    @Autowired
    @Qualifier("calcHeartbeatRankMeta")
    private RabbitmqEventProducer.RabbitmqMessageMeta calcHeartbeatRankMeta;
    @Autowired
    @Qualifier("aiUserStatMeta")
    private RabbitmqEventProducer.RabbitmqMessageMeta aiUserStatMeta;
    @Autowired
    @Qualifier("aiChangeMeta")
    private RabbitmqEventProducer.RabbitmqMessageMeta aiChangeMeta;


    @Override
    public void aiImInfoSendMq(AiImInfoPayload payload) {
        eventProducer.send(Event.builder()
                .eventScene(AI_IM_INFO.getEventCode().getScene())
                .eventModule(AI_IM_INFO.getEventCode().getModule())
                .eventName(AI_IM_INFO.getEventCode().getName())
                .data(payload).build(), aiImInfoMeta);
    }

    @Override
    public void calHeartbeatLevelSendMq(CalcHeartbeatLevelPayload payload) {
        eventProducer.send(Event.builder()
                .eventScene(CALC_HEARTBEAT_LEVEL.getEventCode().getScene())
                .eventModule(CALC_HEARTBEAT_LEVEL.getEventCode().getModule())
                .eventName(CALC_HEARTBEAT_LEVEL.getEventCode().getName())
                .data(payload).build(), calcHeartbeatLevelMeta);
    }

    @Override
    public void sendAiChatMq(Long fromUserId, Long toUserId, String content, MessageTypeEnum messageType, String attach, AiChatPayload.SourceType sourceType) {
        //发送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(messageType.name())
                        .content(content)
                        .attach(attach)
                        .sourceType(sourceType)
                        .build()).build(), aiChatMeta);
    }

    @Override
    public void calcHeartbeatScoreMq(Long userId, Long aiId) {
        //发送MQ消息
        eventProducer.send(Event.builder()
                .eventScene(CALC_HEARTBEAT_SCORE.getEventCode().getScene())
                .eventModule(CALC_HEARTBEAT_SCORE.getEventCode().getModule())
                .eventName(CALC_HEARTBEAT_SCORE.getEventCode().getName())
                .data(CalcHeartbeatScorePayload.builder()
                        .userId(userId)
                        .aiId(aiId)
                        .build()).build(), calcHeartbeatScoreMeta);
    }

    @Override
    public void calcHeartbeatRankMq(Long aiId, Long userId, BigDecimal oldHeartbeatVal, BigDecimal newHeartbeatVal) {
        //判断，如果前后的分值都小于15分的话，那么不发送mq消息
        if(oldHeartbeatVal.compareTo(threshold) < 0 && newHeartbeatVal.compareTo(threshold) < 0) {
            return;
        }
        //发送MQ消息
        eventProducer.send(Event.builder()
                .eventScene(CALC_HEARTBEAT_RANK.getEventCode().getScene())
                .eventModule(CALC_HEARTBEAT_RANK.getEventCode().getModule())
                .eventName(CALC_HEARTBEAT_RANK.getEventCode().getName())
                .data(CalcAiUserHeartbeatRankPayload.builder()
                        .userId(userId)
                        .aiId(aiId)
                        .oldHeartbeatVal(oldHeartbeatVal)
                        .newHeartbeatVal(newHeartbeatVal)
                        .build()).build(), calcHeartbeatRankMeta);
    }

    public static void main(String[] args) {
        BigDecimal threshold = new BigDecimal("12");
        BigDecimal oldHeartbeatVal = new BigDecimal(12);
        BigDecimal newHeartbeatVal = new BigDecimal(0);
        System.out.println(oldHeartbeatVal.compareTo(threshold));
        System.out.println(oldHeartbeatVal.compareTo(threshold) == 0);
        System.out.println(newHeartbeatVal.compareTo(threshold) == 0);

    }

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

    @Override
    public void aiUserStatMq(Long aiId, AiUserStatPayload.Type type, Long coinNum) {
        //发送MQ消息
        eventProducer.send(Event.builder()
                .eventScene(AI_USER_STAT.getEventCode().getScene())
                .eventModule(AI_USER_STAT.getEventCode().getModule())
                .eventName(AI_USER_STAT.getEventCode().getName())
                .data(AiUserStatPayload.builder()
                        .aiId(aiId)
                        .type(type)
                        .coinNum(coinNum)
                        .build()).build(), aiUserStatMeta);
    }
    @Override
    public void aiChangeMq(Long aiId, Boolean isDialoguePrologueChange, Boolean isCreate) {
        //发送MQ消息
        eventProducer.send(Event.builder()
                .eventScene(AI_CHANGE.getEventCode().getScene())
                .eventModule(AI_CHANGE.getEventCode().getModule())
                .eventName(AI_CHANGE.getEventCode().getName())
                .data(AiChangePayload.builder()
                        .aiId(aiId)
                        .isDialoguePrologueChange(isDialoguePrologueChange)
                        .isCreate(isCreate)
                        .build()).build(), aiChangeMeta);
    }
}
