package com.huashe.park.infrastructure.socket.server;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.alibaba.fastjson2.JSONPath;
import org.springframework.web.socket.WebSocketSession;

import com.alibaba.fastjson2.JSONObject;
import com.huashe.park.common.websocket.SocketEndPoint;
import com.huashe.park.common.websocket.SocketHandle;

@SocketEndPoint(url = "/pkb/uwb/trail")
public class UwbVideoTraceSocketServer extends SocketHandle {
    private static final String SESSION_KEY_TEMPLATE = "%s-%s";

    private static final String UWB_KEY = "deviceId";

    private static final String CLIENT = "client";

    @Override
    public String custSessionKey(WebSocketSession session) {
        Map<String, Object> attributes = session.getAttributes();
        return String.format(SESSION_KEY_TEMPLATE, attributes.get(CLIENT), attributes.get(UWB_KEY));
    }

    @Override
    public void sendMessage(String sessionKey, JSONObject message) {
        JSONObject msg = (JSONObject)JSONPath.eval(message, "$.msg");
        super.sendMessage(msg.getString("key"), message);
    }

    @Override
    public List<WebSocketSession> filterSession(String sessionKey, Map<String, WebSocketSession> webSocketSessionMap) {
        return webSocketSessionMap.entrySet().stream().filter(entry -> entry.getKey().contains(sessionKey))
            .map(Map.Entry::getValue).collect(Collectors.toList());
    }

}
