package com.sonic.frog.service.impl;


import com.alibaba.fastjson.JSON;
import com.google.common.collect.Maps;
import com.sonic.frog.domain.entity.AiUser;
import com.sonic.frog.enums.Constants;
import com.sonic.frog.service.AiUserSearchService;
import com.sonic.frog.service.CommonMessageService;
import com.sonic.pigeon.lib.client.MessageClient;
import com.sonic.pigeon.lib.enums.StationMessageTypeEnum;
import com.sonic.pigeon.lib.input.SendMessageInput;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Map;

/**
 * 公共发送系统通知
 */
@Service
public class CommonMessageServiceImpl implements CommonMessageService {

    @Autowired
    private MessageClient messageClient;
    @Autowired
    private AiUserSearchService aiUserSearchService;

    @Override
    public void aiGiftSendMessage(Long userId, Long aiId, String giftName, Integer giftNum, Long totalAmount) {
        AiUser aiUser = aiUserSearchService.getAiUserByAiId(aiId);
        String title = StationMessageTypeEnum.AI_GIFT.getTitle();
        //用户实际所得金额
        BigDecimal userAmount = new BigDecimal(totalAmount).multiply(Constants.USER_RATE).divide(new BigDecimal(100), 0, RoundingMode.CEILING);
        //用户实际所得金额大于0才发送
        if (userAmount.compareTo(BigDecimal.ZERO) > 0) {
            String content = String.format(StationMessageTypeEnum.AI_GIFT.getContent(), aiUser.getNickname(), giftName, giftNum, userAmount);
            SendMessageInput sendMessageInput = new SendMessageInput(userId, aiUser.getUserId(), StationMessageTypeEnum.AI_GIFT.getIndex(), title, content);
            messageClient.sendMessage(sendMessageInput);
        }
    }

    @Override
    public void aiHeartbeatLevelDowngradeSendMessage(Long userId, Long aiId, String heartbeatLevelName) {
        AiUser aiUser = aiUserSearchService.getAiUserByAiId(aiId);
        String title = StationMessageTypeEnum.HEARTBEAT_LEVEL_DOWN.getTitle();
        String content = String.format(StationMessageTypeEnum.HEARTBEAT_LEVEL_DOWN.getContent(), aiUser.getNickname(), heartbeatLevelName);
        //额外扩展字段
        Map<String, Object> extra = Maps.newHashMap();
        extra.put("aiId", aiId);
        SendMessageInput sendMessageInput = new SendMessageInput(-1L, userId, StationMessageTypeEnum.HEARTBEAT_LEVEL_DOWN.getIndex(), title, content);
        sendMessageInput.setExtras(JSON.toJSONString(extra));
        messageClient.sendMessage(sendMessageInput);
    }

    @Override
    public void unlockAlbumImgSendMessage(Long userId, Long aiId, Long albumId, Long unlockAmount) {
        AiUser aiUser = aiUserSearchService.getAiUserByAiId(aiId);
        String title = StationMessageTypeEnum.AI_IMG_UNLOCK.getTitle();
        //用户实际所得金额，除以100
        BigDecimal userAmount = new BigDecimal(unlockAmount).multiply(Constants.USER_RATE).divide(new BigDecimal(100), 0, RoundingMode.CEILING);
        //用户实际所得金额大于0才发送
        if (userAmount.compareTo(BigDecimal.ZERO) > 0) {
            String content = String.format(StationMessageTypeEnum.AI_IMG_UNLOCK.getContent(), aiUser.getNickname(), userAmount);
            SendMessageInput sendMessageInput = new SendMessageInput(userId, aiUser.getUserId(), StationMessageTypeEnum.AI_IMG_UNLOCK.getIndex(), title, content);
            messageClient.sendMessage(sendMessageInput);
        }
    }
}
