package com.zanxiang.game.module.manage.websocket;

import com.zanxiang.game.module.manage.enums.KfWebSocketMsgEnum;
import com.zanxiang.game.module.manage.pojo.dto.KfWebSocketMsgDTO;
import com.zanxiang.game.module.manage.service.IKfRoomService;
import com.zanxiang.module.util.JsonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;

import java.util.List;
import java.util.Objects;

/**
 * @author : lingfeng
 * @time : 2024-02-23
 * @description : 客服消息redis监听器
 */
@Component
@Slf4j
public class KfMsgRedisListener implements MessageListener {

    @Autowired
    private KfMsgWebSocketSessionRegistry kfMsgWebSocketSessionRegistry;

    @Autowired
    private IKfRoomService kfRoomService;

    @Override
    public void onMessage(Message message, byte[] pattern) {
        //从redis中拿到的消息
        String messageBody = new String(message.getBody());
        //转化消息对象
        KfWebSocketMsgDTO kfWebSocketMsgDTO = JsonUtil.toObj(messageBody, KfWebSocketMsgDTO.class);
        Long kfUserId = kfWebSocketMsgDTO.getKfUserId();
        //发送给指定客服
        if (kfUserId != null) {
            WebSocketSession session = kfMsgWebSocketSessionRegistry.getSession(kfUserId);
            if (session != null && session.isOpen()) {
                try {
                    session.sendMessage(new TextMessage(JsonUtil.toString(kfWebSocketMsgDTO)));
                } catch (Exception e) {
                    log.error("发送消息给指定客服异常, kfUserId : {}, kfWebSocketMsgDTO : {}, e : {}", kfUserId,
                            JsonUtil.toString(kfWebSocketMsgDTO), e.getMessage());
                }
            }
            return;
        }
        //发送给所有在线客服
        List<WebSocketSession> openSessions = kfMsgWebSocketSessionRegistry.getAllSessions();
        openSessions.forEach(session -> {
            if (session != null && session.isOpen()) {
                try {
                    //待接入的游戏消息, 设置个人游戏列表
                    if (Objects.equals(kfWebSocketMsgDTO.getWebSocketMsgType(), KfWebSocketMsgEnum.WEBSOCKET_MSG_WAIT_LIST)) {
                        Long finalKfUserId = Long.valueOf(session.getAttributes().get("kfUserId").toString());
                        kfWebSocketMsgDTO.setGameList(kfRoomService.getKfGameByGameId(kfWebSocketMsgDTO.getGameId(), finalKfUserId));
                    }
                    session.sendMessage(new TextMessage(JsonUtil.toString(kfWebSocketMsgDTO)));
                } catch (Exception e) {
                    log.error("发送消息给所有客服异常, kfUserId : {}, kfWebSocketMsgDTO : {}, e : {}",
                            session.getAttributes().get("kfUserId"), JsonUtil.toString(kfWebSocketMsgDTO), e.getMessage());
                }
            }
        });
    }
}
