package com.ruoyi.yunxin.service;

import com.alibaba.fastjson.JSON;
import com.ruoyi.yunxin.YunExecutor;
import com.ruoyi.yunxin.Yunxin;
import com.ruoyi.yunxin.data.ImDataRes;
import com.ruoyi.yunxin.data.ImMsgGen;
import com.ruoyi.yunxin.data.WsSendAttachMsgData;
import com.ruoyi.yunxin.enums.CallNoticeEnum;
import com.ruoyi.yunxin.resp.SendMsgResp;
import com.ruoyi.yunxin.resp.YxCommonR;
import com.ruoyi.yunxin.resp.YxDataR;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class YunxinWsService {

    @Autowired
    private Yunxin yunxin;
    public void sendCallAsync(Long roomId,Long callId,Long receiverId){
        YunExecutor.YUN_EXECUTOR.execute(() -> {
            WsSendAttachMsgData data = new WsSendAttachMsgData();
            WsSendAttachMsgData.SendAttachMsgDataMsg msgDataMsg = new WsSendAttachMsgData.SendAttachMsgDataMsg();
            msgDataMsg.setRoomid(roomId);
            data.setData(msgDataMsg);
            YxDataR<YxCommonR> r = yunxin.sendAttachMsg(callId, receiverId, data);
            if(r == null || !r.isSuccess()){
                log.error("云信发送失败【sendCallAsync】r={}", JSON.toJSONString(r));
            }
        });
    }

    /**
     * 发送消息
     * @param toUid 接收着ID
     * @param fromUid 发送着ID
     * @param status 状态 // 通话状态，1 取消 2 拒绝 3 超时 4 已通话
     * @param calltime 通话时长
     */
    public void sendToCallNotifyAsync(Long toUid, Long fromUid, CallNoticeEnum status, Long calltime){
        YunExecutor.YUN_EXECUTOR.execute(() -> {
            ImDataRes imDataRes = ImMsgGen.callNotice(status, fromUid, toUid, calltime);
            YxDataR<SendMsgResp> r = yunxin.sendTo(toUid, fromUid, imDataRes);
            if(r == null || !r.isSuccess()){
                log.error("云信发送失败【sendCallAsync】r={}", JSON.toJSONString(r));
            }
        });
    }

}
