package com.ruoyi.cai.manager;

import com.ruoyi.cai.domain.Account;
import com.ruoyi.cai.domain.ConsumeLog;
import com.ruoyi.cai.domain.Gift;
import com.ruoyi.cai.domain.User;
import com.ruoyi.cai.dto.app.query.GiveGiftReq;
import com.ruoyi.cai.dto.app.query.GiveGuardReq;
import com.ruoyi.cai.dto.commom.consumer.GiftConsumerResp;
import com.ruoyi.cai.dto.commom.consumer.GuardConsumerResp;
import com.ruoyi.cai.dto.commom.consumer.RechargeConsumerResp;
import com.ruoyi.cai.dto.video.VideoSettleResp;
import com.ruoyi.cai.enums.SystemConfigEnum;
import com.ruoyi.cai.mq.AmqpProducer;
import com.ruoyi.cai.mq.handle.dto.WindowGiftDTO;
import com.ruoyi.cai.mq.handle.dto.WindowRechargeDTO;
import com.ruoyi.cai.notice.YunxinHttpService;
import com.ruoyi.cai.pay.PayTypeEnum;
import com.ruoyi.cai.rank.RankManager;
import com.ruoyi.cai.service.*;
import com.ruoyi.cai.ws.bean.Room;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Map;

@Component
@Slf4j
public class ConsumerManager {
    @Autowired
    private GuardTotalService guardTotalService;
    @Autowired
    private UserGiftService userGiftService;
    @Autowired
    private AmqpProducer amqpProducer;
    @Autowired
    private RechargeOrderService rechargeOrderService;
    @Autowired
    private AccountService accountService;
    @Autowired
    private RankManager rankManager;
    @Autowired
    private YunxinHttpService yunxinHttpService;
    @Autowired
    private SystemConfigManager systemConfigManager;
    @Autowired
    private UserService userService;

    public GuardConsumerResp sendGuard(GiveGuardReq query){
        GuardConsumerResp resp = guardTotalService.giveGuard(query);
        if(resp.isSuccess()){
            try {
                amqpProducer.sendCalculateSales(resp.getConsumeLogId()+"");
            }catch (Exception e){
                log.error("RabbitMq 发送失败, 守护分销流程流转失败！",e);
            }
            try {
                Account account = accountService.getByUserId(resp.getToUid());
                if(account != null){
                    Long totalCoin = account.getCoin() + account.getIncomeCoin();
                    // 主播的系统收益通知
                    yunxinHttpService.getGuardMessage(resp.getToUid(), query.getGuardNum(), query.getGuardPrice(), resp.getAnchorIncomeCoin(), totalCoin);
                }
            }catch (Exception e){
                log.error("礼物赠送成功通知失败！",e);
            }
        }
        return resp;
    }

    public GiftConsumerResp sendGift(GiveGiftReq query){
        GiftConsumerResp resp = userGiftService.giveGift(query);
        if(resp.isSuccess()){
            try {
                amqpProducer.sendCalculateSales(resp.getConsumeLogId()+"");
            }catch (Exception e){
                log.error("RabbitMq 发送失败, 礼物分销流程流转失败！",e);
            }
            try {
                Account account = accountService.getByUserId(resp.getToUid());
                if(account != null){
                    Long totalCoin = account.getCoin() + account.getIncomeCoin();
                    // 主播的系统收益通知
                    yunxinHttpService.getGiftSendMessage(resp.getToUid(), resp.getGift(), resp.getAnchorIncomeCoin(), totalCoin);
                }
                // 发送方给接收方的消息
                yunxinHttpService.sendGiftMessage(resp.getFromUid(),resp.getToUid(),resp.getGift(),query.getGiftCount().intValue());
            }catch (Exception e){
                log.error("礼物赠送成功通知失败！",e);
            }
            try {
                Long minPrice = systemConfigManager.getSystemConfigOfLong(SystemConfigEnum.WINDOW_GIFT_THRESHOLD);
                Gift gift = resp.getGift();
                if(gift.getPrice() > minPrice){
                    User fromUser = userService.getById(resp.getFromUid());
                    Integer rankHide = fromUser.getRankHide();
                    if(rankHide == 0){
                        WindowGiftDTO windowGift = new WindowGiftDTO();
                        windowGift.setFromId(resp.getFromUid());
                        windowGift.setToId(resp.getToUid());
                        windowGift.setGift(resp.getGift());
                        windowGift.setGiftNum(query.getGiftCount());
                        amqpProducer.sendWindowMq(windowGift);
                    }
                }
            }catch (Exception e){
                log.error("发送充值飘窗！",e);
            }
        }
        return resp;
    }

    public VideoSettleResp videoSettle(Room room){
        VideoSettleResp resp = accountService.videoSettle(room);
        ConsumeLog consumeLog = resp.getConsumeLog();
        try {
            amqpProducer.sendCalculateSales(consumeLog.getId()+"");
        }catch (Exception e){
            log.error("RabbitMq 发送失败, 视频分销流程流转失败！",e);
        }
        rankManager.sendLoveRankMq(consumeLog.getSourceUserId(),consumeLog.getAnchorAmount(),consumeLog.getTraceId());
        return resp;
    }

    public RechargeConsumerResp rechargeOrderSuccess(String tradeNo, Map<String,String> params, String appId, PayTypeEnum payTypeEnum){
        RechargeConsumerResp resp = rechargeOrderService.orderSuccess(tradeNo,params,appId,payTypeEnum);
        if(resp.isSuccess()){
            try {
                amqpProducer.sendCalculateSales(resp.getConsumeLogId()+"");
            }catch (Exception e){
                log.error("RabbitMq 发送失败, 充值分销流程流转失败！",e);
            }
            try {
                Account account = accountService.getByUserId(resp.getUserId());
                if(account != null){
                    Long totalCoin = account.getCoin() + account.getIncomeCoin();
                    yunxinHttpService.rechargeCoinSendMessage(resp.getUserId(), resp.getRechargeCoin(),totalCoin);
                }
            }catch (Exception e){
                log.error("充值成功通知失败！",e);
            }
            try {
                Long minPrice = systemConfigManager.getSystemConfigOfLong(SystemConfigEnum.WINDOW_RECHARGE_THRESHOLD);
                if(resp.getRechargeCoin() > minPrice){
                    Integer rankHide = resp.getUser().getRankHide();
                    if(rankHide == 0){
                        WindowRechargeDTO windowRecharge = new WindowRechargeDTO();
                        windowRecharge.setId(resp.getUserId());
                        windowRecharge.setNickname(resp.getUser().getNickname());
                        windowRecharge.setAvatar(resp.getUser().getAvatar());
                        windowRecharge.setAmount(resp.getRechargeCoin());
                        windowRecharge.setTime(LocalDateTime.now());
                        amqpProducer.sendWindowMq(windowRecharge);
                    }
                }
            }catch (Exception e){
                log.error("发送充值飘窗！",e);
            }

        }
        return resp;
    }
}
