package com.ruoyi.cai.ws.handler;

import com.ruoyi.cai.ws.bean.FdCtxData;
import com.ruoyi.cai.ws.cache.RoomCtxCache;
import com.ruoyi.cai.ws.dto.WsR;
import com.ruoyi.cai.ws.util.RoomWebSocketUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public abstract class AbstractMessageHandle implements IMessageHandler {

    @Autowired
    private RoomCtxCache roomCtxCache;

    protected void sendToCurrent(FdCtxData fdCtxData, WsR r){
        RoomWebSocketUtil.sendSendMessage(fdCtxData.getSessionKey(),  r);
    }

    protected void sendToTar(FdCtxData fdCtxData, WsR r) {
        String sessionKey = roomCtxCache.getSessionKeyByRoomIdAndUserType(fdCtxData.getRoomId(), fdCtxData.getTarUserType());
        RoomWebSocketUtil.sendSendMessage(sessionKey,  r);
    }

    protected void sendToReceiver(Long roomId, WsR r){
        String receiverSessionKey = roomCtxCache.getSessionKeyReceiverByRoomId(roomId);
        RoomWebSocketUtil.sendSendMessage(receiverSessionKey,  r);
    }

    protected void sendToAll(Long roomId, WsR ... r ){
        List<String> sessionKeys = roomCtxCache.getSessionKeysByRoomId(roomId);
        for (WsR wsR : r) {
            RoomWebSocketUtil.sendSendMessage(sessionKeys, wsR);
        }

    }

}
