package com.ruoyi.cai.notice;

import com.alibaba.fastjson.JSON;
import com.ruoyi.cai.domain.User;
import com.ruoyi.cai.notice.data.*;
import com.ruoyi.cai.notice.data.child.*;
import com.ruoyi.cai.notice.enums.MessageBaseTypeEnum;
import com.ruoyi.cai.util.CaiDateUtil;
import com.ruoyi.yunxin.YunExecutor;
import com.ruoyi.yunxin.Yunxin;
import com.ruoyi.yunxin.resp.SendMsgResp;
import com.ruoyi.yunxin.resp.YxDataR;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.time.LocalDateTime;

@Component
@Slf4j
public class YunxinHttpService {

    private final static Long SYS_NOTICE_ID = 2L;
    @Autowired
    private Yunxin yunxin;

    /**
     *  注册赠送消息
     */
    public void registerRewardCoin(Long toUid,Long rewardCoin,Long totalCoin){
        YunExecutor.YUN_EXECUTOR.execute(() -> {
            MessageExtNew messageExtNew = new MessageExtNew(MessageBaseTypeEnum.TXT);
            MessageExtData data = new MessageExtData();
            data.setAction(1);
            data.setTitle("系统奖励");
            data.setCurrentDate();
            data.setTxt1("陌生人交友需谨慎，添加陌生人微信,QQ失去平台监管有极大被骗风险，凡是诱导加微信，QQ引导到其他平台或以色情为由索取礼物的行为，请大家及时举报，一经查明直接永久封号，良好的交友氛围需大家共同维护，希望大家提高自我警惕性杜绝被骗");
            data.addFields("注册奖励",rewardCoin+"云贝");
            data.addFields("注册奖励",totalCoin+"云贝");
            messageExtNew.setData(data);
            YxDataR<SendMsgResp> r = yunxin.sendTo(toUid, SYS_NOTICE_ID, messageExtNew);
            if(r == null || !r.isSuccess()){
                log.error("云信发送失败【sendCallAsync】r={}", JSON.toJSONString(r));
            }
        });
    }

    /**
     * 充值云贝成功消息
     */
    public void rechargeCoinSendMessage(Long toUid,Long rewardCoin,Long totalCoin){
        YunExecutor.YUN_EXECUTOR.execute(() -> {
            MessageExtNew messageExtNew = new MessageExtNew(MessageBaseTypeEnum.TXT);
            MessageExtData data = new MessageExtData();
            data.setAction(1);
            data.setTitle("购买成功");
            data.setCurrentDate();
            data.addFields("购买云贝",rewardCoin+"云贝");
            data.addFields("总云贝",totalCoin+"云贝");
            messageExtNew.setData(data);
            YxDataR<SendMsgResp> r = yunxin.sendTo(toUid, SYS_NOTICE_ID, messageExtNew);
            if(r == null || !r.isSuccess()){
                log.error("云信发送失败【sendCallAsync】r={}", JSON.toJSONString(r));
            }
        });
    }


    /**
     * 收到礼物发送消息
     */
    public void getGiftSendMessage(Long toUid,String giftName,Long giftPrice,Long incomeCoin,Long totalCoin){
        YunExecutor.YUN_EXECUTOR.execute(() -> {
            MessageExtNew messageExtNew = new MessageExtNew(MessageBaseTypeEnum.TXT);
            MessageExtData data = new MessageExtData();
            data.setAction(1);
            data.setTitle("收到礼物");
            data.setCurrentDate();
            data.addFields("礼物名称",giftName);
            data.addFields("礼物单价",giftPrice+"云贝");
            data.addFields("收入云贝",incomeCoin+"云贝");
            data.addFields("总云贝",totalCoin+"云贝");
            messageExtNew.setData(data);
            YxDataR<SendMsgResp> r = yunxin.sendTo(toUid, SYS_NOTICE_ID, messageExtNew);
            if(r == null || !r.isSuccess()){
                log.error("云信发送失败【sendCallAsync】r={}", JSON.toJSONString(r));
            }
        });
    }

    /**
     * 邀请奖励
     */
    public void inviteRewardSendMessage(Long toUid,String type,String sourceNickname,String sourceUsercode,Long incomeCoin){
        YunExecutor.YUN_EXECUTOR.execute(() -> {
            String str = "";
            if("recharge".equals(type)){
                str = "用户充值奖励";
            }else if("reward".equals(type)){
                str = "奖励分成";
            }
            MessageExtNew messageExtNew = new MessageExtNew(MessageBaseTypeEnum.TXT);
            MessageExtData data = new MessageExtData();
            data.setAction(1);
            data.setTitle("邀请奖励");
            data.setCurrentDate();
            data.addFields("用户昵称",sourceNickname);
            data.addFields("用户蜜瓜号",sourceUsercode);
            data.addFields(str,incomeCoin+"云贝");
            messageExtNew.setData(data);
            YxDataR<SendMsgResp> r = yunxin.sendTo(toUid, SYS_NOTICE_ID, messageExtNew);
            if(r == null || !r.isSuccess()){
                log.error("云信发送失败【sendCallAsync】r={}", JSON.toJSONString(r));
            }
        });
    }

    /**
     * 用户充值，邀请人返现
     */
    public void inviteCashbackSendMessage(Long toUid,String sourceNickname,String sourceUsercode,Long payCoin,Long incomeCoin){
        YunExecutor.YUN_EXECUTOR.execute(() -> {
            MessageExtNew messageExtNew = new MessageExtNew(MessageBaseTypeEnum.TXT);
            MessageExtData data = new MessageExtData();
            data.setAction(1);
            data.setTitle("充值分成");
            data.setTxt1("充值分成：每一笔账户充值将给其邀请账户额外赠送{}%的云贝");
            data.setCurrentDate();
            data.addFields("充值用户",sourceNickname);
            data.addFields("蜜瓜号",sourceUsercode);
            data.addFields("充值金额",payCoin+"云贝");
            data.addFields("充值分成",incomeCoin+"云贝");
            messageExtNew.setData(data);
            YxDataR<SendMsgResp> r = yunxin.sendTo(toUid, SYS_NOTICE_ID, messageExtNew);
            if(r == null || !r.isSuccess()){
                log.error("云信发送失败【sendCallAsync】r={}", JSON.toJSONString(r));
            }
        });
    }

    /**
     * 提现审核转账发送消息【提现审核成功】
     */
    public void cashSuccessSendMessage(Long toUid, LocalDateTime cashTime, BigDecimal cashMoney, BigDecimal realCashMonty,
                                       String cardAccount,LocalDateTime verifyTime){
        YunExecutor.YUN_EXECUTOR.execute(() -> {
            MessageExtNew messageExtNew = new MessageExtNew(MessageBaseTypeEnum.TXT);
            MessageExtData data = new MessageExtData();
            data.setAction(1);
            data.setTitle("提现成功");
            data.setCurrentDate();
            data.addFields("提现时间", CaiDateUtil.localDateTimeToString(cashTime));
            data.addFields("提现金额",cashMoney.toString()+"元");
            data.addFields("提现方式","支付宝");
            data.addFields("到账金额",realCashMonty.toString()+"元");
            data.addFields("到账账户","支付宝("+cardAccount+")");
            data.addFields("审核时间",CaiDateUtil.localDateTimeToString(verifyTime));
            messageExtNew.setData(data);
            YxDataR<SendMsgResp> r = yunxin.sendTo(toUid, SYS_NOTICE_ID, messageExtNew);
            if(r == null || !r.isSuccess()){
                log.error("云信发送失败【sendCallAsync】r={}", JSON.toJSONString(r));
            }
        });
    }


    /**
     * 提现审核转账发送消息【提现审核失败】
     */
    public void cashFailSendMessage(Long toUid, LocalDateTime cashTime, BigDecimal cashMoney, String remark){
        YunExecutor.YUN_EXECUTOR.execute(() -> {
            MessageExtNew messageExtNew = new MessageExtNew(MessageBaseTypeEnum.TXT);
            MessageExtData data = new MessageExtData();
            data.setAction(1);
            data.setTitle("提现失败");
            data.setCurrentDate();
            data.addFields("提现时间", CaiDateUtil.localDateTimeToString(cashTime));
            data.addFields("提现金额",cashMoney.toString()+"元");
            data.addFields("提现方式","支付宝");
            data.setTxt2(remark);
            messageExtNew.setData(data);
            YxDataR<SendMsgResp> r = yunxin.sendTo(toUid, SYS_NOTICE_ID, messageExtNew);
            if(r == null || !r.isSuccess()){
                log.error("云信发送失败【sendCallAsync】r={}", JSON.toJSONString(r));
            }
        });
    }

    /**
     * 举报发送消息
     */
    public void reportSendMessage(Long toUid){
        YunExecutor.YUN_EXECUTOR.execute(() -> {
            MessageExtNew messageExtNew = new MessageExtNew(MessageBaseTypeEnum.TXT);
            MessageExtData data = new MessageExtData();
            data.setAction(1);
            data.setTitle("已收到你的举报信息");
            data.setTxt1("感谢您提交的举报信息，拒绝不良风气，良好的氛围需要大家共同维护。");
            data.setCurrentDate();
            messageExtNew.setData(data);
            YxDataR<SendMsgResp> r = yunxin.sendTo(toUid, SYS_NOTICE_ID, messageExtNew);
            if(r == null || !r.isSuccess()){
                log.error("云信发送失败【sendCallAsync】r={}", JSON.toJSONString(r));
            }
        });
    }

    /**
     * 关注发送消息
     * @param toUid
     */
    public void followedSendMessage(Long toUid,
                                    Long followUserId,
                                    String followNickname,
                                    String followAvatar,
                                    Integer followAge,
                                    String followCity,
                                    LocalDateTime followTime,
                                    Integer gender){
        YunExecutor.YUN_EXECUTOR.execute(() -> {
            MessageSendFollowNew messageExtNew = new MessageSendFollowNew(MessageBaseTypeEnum.SEND_FOLLOW);
            MessageSendFollowData data = new MessageSendFollowData();
            data.setUserid(followUserId);
            data.setNickname(followNickname);
            data.setAvatar(followAvatar);
            data.setAge(followAge);
            data.setCity(followCity);
            data.setTime(CaiDateUtil.localDateTimeToString(followTime));
            data.setSex(gender);
            messageExtNew.setData(data);
            YxDataR<SendMsgResp> r = yunxin.sendTo(toUid, SYS_NOTICE_ID, messageExtNew);
            if(r == null || !r.isSuccess()){
                log.error("云信发送失败【sendCallAsync】r={}", JSON.toJSONString(r));
            }
        });
    }

    /**
     * 通过女神认证发送消息
     */
    public void passAnchorSendMessage(Long toUid){
        YunExecutor.YUN_EXECUTOR.execute(() -> {
            MessageExtNew messageExtNew = new MessageExtNew(MessageBaseTypeEnum.TXT);
            MessageExtData data = new MessageExtData();
            data.setAction(1);
            data.setTitle("恭喜你通过自拍认证，成为平台女神");
            data.setCurrentDate();
            data.addFields("设置服务价格","请到“我的-->女神设置”处设置");
            data.addFields("找男生聊聊","首页右上角点击“男生”按钮查找在线男生");
            data.addFields("赚钱小秘密一","多给在线男生打招呼");
            data.addFields("赚钱小秘密二","照片越好看，男生越喜欢");
            data.addFields("赚钱小秘密三","勤发动态，让他感受到你的美");
            messageExtNew.setData(data);
            YxDataR<SendMsgResp> r = yunxin.sendTo(toUid, SYS_NOTICE_ID, messageExtNew);
            if(r == null || !r.isSuccess()){
                log.error("云信发送失败【sendCallAsync】r={}", JSON.toJSONString(r));
            }
        });
    }


    /**
     * 更改女神邀请比例
     */
    public void changeInviteConfigSendMessage(Long toUid){
        YunExecutor.YUN_EXECUTOR.execute(() -> {
            MessageExtNew messageExtNew = new MessageExtNew(MessageBaseTypeEnum.TXT);
            MessageExtData data = new MessageExtData();
            data.setAction(1);
            data.setTitle("恭喜成为邀请女神");
            data.setCurrentDate();
            messageExtNew.setData(data);
            YxDataR<SendMsgResp> r = yunxin.sendTo(toUid, SYS_NOTICE_ID, messageExtNew);
            if(r == null || !r.isSuccess()){
                log.error("云信发送失败【sendCallAsync】r={}", JSON.toJSONString(r));
            }
        });
    }

    /**
     * 发送礼物自定义消息
     */
    public void sendGiftMessage(Long toUid){
        YunExecutor.YUN_EXECUTOR.execute(() -> {
            MessageSendGiftMessageNew messageExtNew = new MessageSendGiftMessageNew(MessageBaseTypeEnum.SEND_GIFT_MESSAGE);
            MessageSendGiftMessageData data = new MessageSendGiftMessageData();
            messageExtNew.setData(data);
            YxDataR<SendMsgResp> r = yunxin.sendTo(toUid, SYS_NOTICE_ID, messageExtNew);
            if(r == null || !r.isSuccess()){
                log.error("云信发送失败【sendCallAsync】r={}", JSON.toJSONString(r));
            }
        });
    }


    /**
     * 收到守护符通知
     */
    public void getGuardMessage(Long toUid,Integer num,Long price,BigDecimal incomeCoin,BigDecimal totalCoin){
        YunExecutor.YUN_EXECUTOR.execute(() -> {
            MessageExtNew messageExtNew = new MessageExtNew(MessageBaseTypeEnum.TXT);
            MessageExtData data = new MessageExtData();
            data.setAction(1);
            data.setTitle("收到守护符");
            data.setCurrentDate();
            data.addFields("守护类型",num+"个守护符");
            data.addFields("守护单价",price+"云贝");
            data.addFields("收入云贝",incomeCoin+"云贝");
            data.addFields("总云贝",totalCoin+"云贝");
            messageExtNew.setData(data);
            YxDataR<SendMsgResp> r = yunxin.sendTo(toUid, SYS_NOTICE_ID, messageExtNew);
            if(r == null || !r.isSuccess()){
                log.error("云信发送失败【sendCallAsync】r={}", JSON.toJSONString(r));
            }
        });
    }


    /**
     * 发送上线了批量自定义消息
     */
    public void sendOnlineAttentionNotice(Long toUid){
        YunExecutor.YUN_EXECUTOR.execute(() -> {
            MessageSendOnlineAttentionNew messageExtNew = new MessageSendOnlineAttentionNew(MessageBaseTypeEnum.TXT);
            MessageSendOnlineAttentionData data = new MessageSendOnlineAttentionData();
            messageExtNew.setData(data);
            YxDataR<SendMsgResp> r = yunxin.sendTo(toUid, SYS_NOTICE_ID, messageExtNew);
            if(r == null || !r.isSuccess()){
                log.error("云信发送失败【sendCallAsync】r={}", JSON.toJSONString(r));
            }
        });
    }

    /**
     * 取消女神通知
     */
    public void cancelAnchorMessage(Long toUid){
        YunExecutor.YUN_EXECUTOR.execute(() -> {
            MessageExtNew messageExtNew = new MessageExtNew(MessageBaseTypeEnum.TXT);
            MessageExtData data = new MessageExtData();
            data.setAction(1);
            data.setTitle("取消女神通知");
            data.setCurrentDate();
            data.setTxt1("因多次违规或被投诉,已被取消女神资格！");
            messageExtNew.setData(data);
            YxDataR<SendMsgResp> r = yunxin.sendTo(toUid, SYS_NOTICE_ID, messageExtNew);
            if(r == null || !r.isSuccess()){
                log.error("云信发送失败【sendCallAsync】r={}", JSON.toJSONString(r));
            }
        });
    }


    /**
     * 动态审核通知
     */
    public void dynamicAuditMessage(Long toUid){
        YunExecutor.YUN_EXECUTOR.execute(() -> {
            MessageExtNew messageExtNew = new MessageExtNew(MessageBaseTypeEnum.TXT);
            MessageExtData data = new MessageExtData();
            data.setAction(1);
            data.setTitle("动态审核通知");
            data.setCurrentDate();
            data.setTxt1("您的动态因不符合规范,已被删除，多次违规会被取消大咖资格或封号处理");
            messageExtNew.setData(data);
            YxDataR<SendMsgResp> r = yunxin.sendTo(toUid, SYS_NOTICE_ID, messageExtNew);
            if(r == null || !r.isSuccess()){
                log.error("云信发送失败【sendCallAsync】r={}", JSON.toJSONString(r));
            }
        });
    }


    /**
     * 相册审核通知
     */
    public void albumAuditMessage(Long toUid){
        YunExecutor.YUN_EXECUTOR.execute(() -> {
            MessageExtNew messageExtNew = new MessageExtNew(MessageBaseTypeEnum.TXT);
            MessageExtData data = new MessageExtData();
            data.setAction(1);
            data.setTitle("相册审核通知");
            data.setCurrentDate();
            data.setTxt1("您上传的照片不符合要求,已被删除，多次违规会被取消大咖资格或封号处理，请按照指引上传符合要求的照片");
            data.setLink_type(1);
            data.setLink_url("/index/about/album_rule.html");
            messageExtNew.setData(data);
            YxDataR<SendMsgResp> r = yunxin.sendTo(toUid, SYS_NOTICE_ID, messageExtNew);
            if(r == null || !r.isSuccess()){
                log.error("云信发送失败【sendCallAsync】r={}", JSON.toJSONString(r));
            }
        });
    }


    /**
     * 昵称重置审核通知
     */
    public void nickAuditMessage(Long toUid){
        YunExecutor.YUN_EXECUTOR.execute(() -> {
            MessageExtNew messageExtNew = new MessageExtNew(MessageBaseTypeEnum.TXT);
            MessageExtData data = new MessageExtData();
            data.setAction(1);
            data.setTitle("重置昵称通知");
            data.setCurrentDate();
            data.setTxt1("您的昵称不合格，已被重置，多次违规会被封号！");
            messageExtNew.setData(data);
            YxDataR<SendMsgResp> r = yunxin.sendTo(toUid, SYS_NOTICE_ID, messageExtNew);
            if(r == null || !r.isSuccess()){
                log.error("云信发送失败【sendCallAsync】r={}", JSON.toJSONString(r));
            }
        });
    }


    /**
     * 发送充值飘窗提醒
     */
    public void sendRechargeWindowsAmount(Long toUid){
        YunExecutor.YUN_EXECUTOR.execute(() -> {
            MessageSendRechargeWindowsAmountNew messageExtNew = new MessageSendRechargeWindowsAmountNew(MessageBaseTypeEnum.SEND_GIFTWINDOWS_AMOUNT);
            MessageSendRechargeWindowsAmountData data = new MessageSendRechargeWindowsAmountData();
            messageExtNew.setData(data);
            YxDataR<SendMsgResp> r = yunxin.sendTo(toUid, SYS_NOTICE_ID, messageExtNew);
            if(r == null || !r.isSuccess()){
                log.error("云信发送失败【sendCallAsync】r={}", JSON.toJSONString(r));
            }
        });
    }


    /**
     * 发送礼物飘窗提醒
     */
    public void sendGiftWindowsAmount(Long toUid){
        YunExecutor.YUN_EXECUTOR.execute(() -> {
            MessageSendGiftWindowsAmountNew messageExtNew = new MessageSendGiftWindowsAmountNew(MessageBaseTypeEnum.SEND_GIFTWINDOWS_AMOUNT);
            MessageSendGiftWindowsAmountData data = new MessageSendGiftWindowsAmountData();
            messageExtNew.setData(data);
            YxDataR<SendMsgResp> r = yunxin.sendTo(toUid, SYS_NOTICE_ID, messageExtNew);
            if(r == null || !r.isSuccess()){
                log.error("云信发送失败【sendCallAsync】r={}", JSON.toJSONString(r));
            }
        });
    }


    /**
     * 动态通知
     */
    public void sendFollowDynamicBath(Long toUid, User user,String image,String content){
        YunExecutor.YUN_EXECUTOR.execute(() -> {
            MessageSendFollowDynamicNew messageExtNew = new MessageSendFollowDynamicNew(MessageBaseTypeEnum.SEND_FOLLOW_DYNAMIC);
            MessageSendFollowDynamicData data = new MessageSendFollowDynamicData();
            data.setUserid(user.getId());
            data.setNickname(user.getNickname());
            data.setAvatar(user.getAvatar());
            data.setAge(user.getAge().intValue());
            data.setCity(user.getCity());
            data.setTime(CaiDateUtil.getCurrentTimeStr());
            data.setImage(image);
            data.setTitle(content);
            messageExtNew.setData(data);
            YxDataR<SendMsgResp> r = yunxin.sendTo(toUid, SYS_NOTICE_ID, messageExtNew);
            if(r == null || !r.isSuccess()){
                log.error("云信发送失败【sendCallAsync】r={}", JSON.toJSONString(r));
            }
        });
    }


}
