package com.ruoyi.cai.ws.handler.message;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.cai.domain.UserCall;
import com.ruoyi.cai.service.UserCallService;
import com.ruoyi.cai.ws.bean.FdCtxData;
import com.ruoyi.cai.ws.bean.Room;
import com.ruoyi.cai.ws.cache.RoomDataCache;
import com.ruoyi.cai.ws.constant.RoomStatusEnums;
import com.ruoyi.cai.ws.dto.WsRMsgGen;
import com.ruoyi.cai.ws.handler.IMessageHandler;
import com.ruoyi.cai.ws.handler.AbstractMessageHandle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * 被叫同意接听处理
 */
@Component("agree")
public class AgreeMessageHandle extends AbstractMessageHandle implements IMessageHandler {

    @Autowired
    private RoomDataCache roomDataCache;
    @Autowired
    private UserCallService userCallService;

    @Override
    public void processOn(Room room, FdCtxData fdCtxData, JSONObject map) {
        if(!fdCtxData.isReceiver()){
            return;
        }
        boolean agree = roomDataCache.setStatusAgree(room.getRoomId());
        if(!agree){
            return;
        }
        // 通知可进行接通
        userCallService.update(Wrappers.lambdaUpdate(UserCall.class)
                .eq(UserCall::getId,room.getRoomId())
                .set(UserCall::getStatus, RoomStatusEnums.STATUS_AGREE.getCode())
                .set(UserCall::getBeginTime, LocalDateTime.now()));
        String message = "提示：禁止任何涉黄、任何微信QQ引导到其它平台行为";
        sendToAll(room.getRoomId(), WsRMsgGen.startVideo(room.getRoomId(),0L),WsRMsgGen.sysNotice(message));
    }
}
