package com.sonic.cow.service.impl;


import com.sonic.common.event.Event;
import com.sonic.common.event.EventProducer;
import com.sonic.common.event.RabbitmqEventProducer;
import com.sonic.cow.event.inner.payload.AiGenImagePayload;
import com.sonic.cow.event.inner.payload.EmotionScorePayload;
import com.sonic.cow.event.inner.payload.VoiceCallDeductionPayload;
import com.sonic.cow.event.inner.payload.VoiceCallWebhookPayload;
import com.sonic.cow.event.outer.payload.CalcHeartbeatLevelPayload;
import com.sonic.cow.event.outer.payload.UserBalanceInsufficientCheckoutPayload;
import com.sonic.cow.event.outer.payload.UserDeductionStatPayload;
import com.sonic.cow.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 static com.sonic.cow.event.inner.EventType.*;
import static com.sonic.cow.event.outer.EventType.*;

/**
 * @description: 发送消息到im的mq
 * @author: mzc
 * @create: 2020-02-06 17:56
 **/
@Service
@Slf4j
public class CommonSendMqServiceImpl implements CommonSendMqService {

    @Autowired
    private EventProducer eventProducer;

    @Autowired
    @Qualifier("aiTextGenImageMeta")
    private RabbitmqEventProducer.RabbitmqMessageMeta aiTextGenImageMeta;
    @Autowired
    @Qualifier("aiImageGenImageMeta")
    private RabbitmqEventProducer.RabbitmqMessageMeta aiImageGenImageMeta;

    @Autowired
    @Qualifier("calcHeartbeatLevelMeta")
    private RabbitmqEventProducer.RabbitmqMessageMeta calcHeartbeatLevelMeta;

    @Autowired
    @Qualifier("emotionScoreMeta")
    private RabbitmqEventProducer.RabbitmqMessageMeta emotionScoreMeta;
    @Autowired
    @Qualifier("userDeductionStatMeta")
    private RabbitmqEventProducer.RabbitmqMessageMeta userDeductionStatMeta;

    @Autowired
    @Qualifier("voiceCallDeductionDeadMeta")
    private RabbitmqEventProducer.RabbitmqMessageMeta voiceCallDeductionDeadMeta;
    @Autowired
    @Qualifier("voiceCallDeductionMeta")
    private RabbitmqEventProducer.RabbitmqMessageMeta voiceCallDeductionMeta;
    @Autowired
    @Qualifier("voiceCallWebhookMeta")
    private RabbitmqEventProducer.RabbitmqMessageMeta voiceCallWebhookMeta;

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


    @Override
    public void aiTextGenImageSendMq(String batchNo, String taskId, String prompt, String imageUrl) {
        eventProducer.send(Event.builder()
                .eventScene(AI_TEXT_GEN_IMAGE.getEventCode().getScene())
                .eventModule(AI_TEXT_GEN_IMAGE.getEventCode().getModule())
                .eventName(AI_TEXT_GEN_IMAGE.getEventCode().getName())
                .data(AiGenImagePayload.builder().batchNo(batchNo).taskId(taskId).prompt(prompt).imageUrl(imageUrl).build()).build(), aiTextGenImageMeta);
    }

    @Override
    public void aiImageGenImageSendMq(String batchNo, String taskId, String prompt, String imageUrl) {
        eventProducer.send(Event.builder()
                .eventScene(AI_IMAGE_GEN_IMAGE.getEventCode().getScene())
                .eventModule(AI_IMAGE_GEN_IMAGE.getEventCode().getModule())
                .eventName(AI_IMAGE_GEN_IMAGE.getEventCode().getName())
                .data(AiGenImagePayload.builder().batchNo(batchNo).taskId(taskId).prompt(prompt).imageUrl(imageUrl).build()).build(), aiImageGenImageMeta);
    }

    @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 emotionScoreSendMq(EmotionScorePayload payload) {
        eventProducer.send(Event.builder()
                .eventScene(EMOTION_SCORE.getEventCode().getScene())
                .eventModule(EMOTION_SCORE.getEventCode().getModule())
                .eventName(EMOTION_SCORE.getEventCode().getName())
                .data(payload).build(), emotionScoreMeta);
    }

    @Override
    public void userDeductionStatSendMq(UserDeductionStatPayload payload) {
        eventProducer.send(Event.builder()
                .eventScene(USER_DEDUCTION_STAT.getEventCode().getScene())
                .eventModule(USER_DEDUCTION_STAT.getEventCode().getModule())
                .eventName(USER_DEDUCTION_STAT.getEventCode().getName())
                .data(payload).build(), userDeductionStatMeta);
    }

    @Override
    public void voiceCallDeductionSendDeadMq(Long userId, Long aiId, Long voiceCallId) {
        VoiceCallDeductionPayload payload = VoiceCallDeductionPayload.builder()
                .userId(userId)
                .aiId(aiId)
                .voiceCallId(voiceCallId)
                .build();
        eventProducer.send(new Event(
                VOICE_CALL_DEDUCTION.getEventCode().getScene(),
                VOICE_CALL_DEDUCTION.getEventCode().getModule(),
                VOICE_CALL_DEDUCTION.getEventCode().getName(),
                message -> {
                    //1分钟 这里单位是毫秒
                    message.getMessageProperties().setExpiration("60000");
                    return message;
                },
                payload), voiceCallDeductionDeadMeta);
    }

    @Override
    public void voiceCallDeductionSendMq(Long userId, Long aiId, Long voiceCallId) {
        VoiceCallDeductionPayload payload = VoiceCallDeductionPayload.builder()
                .userId(userId)
                .aiId(aiId)
                .voiceCallId(voiceCallId)
                .build();
        eventProducer.send(Event.builder()
                .eventScene(VOICE_CALL_DEDUCTION.getEventCode().getScene())
                .eventModule(VOICE_CALL_DEDUCTION.getEventCode().getModule())
                .eventName(VOICE_CALL_DEDUCTION.getEventCode().getName())
                .data(payload).build(), voiceCallDeductionMeta);
    }

    @Override
    public void voiceCallWebhookSendMq(com.sonic.cow.client.rtc.Event event) {
        VoiceCallWebhookPayload payload = VoiceCallWebhookPayload.builder()
                .voiceCallEvent(event)
                .build();
        eventProducer.send(Event.builder()
                .eventScene(VOICE_CALL_WEBHOOK.getEventCode().getScene())
                .eventModule(VOICE_CALL_WEBHOOK.getEventCode().getModule())
                .eventName(VOICE_CALL_WEBHOOK.getEventCode().getName())
                .data(payload).build(), voiceCallWebhookMeta);
    }

    @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);
    }
}
