package com.kospo.talk.config.listener;

import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.event.EventListener;
import org.springframework.messaging.simp.broker.SubscriptionRegistry;
import org.springframework.security.core.session.SessionRegistry;
import org.springframework.web.socket.messaging.SessionDisconnectEvent;

@Configuration
@RequiredArgsConstructor
public class CustomMessageListener {
    private final SubscriptionRegistry subscriptionRegistry;
    private final SessionRegistry sessionRegistry;

//    private final Map<String, WebSocketSession> sessions = new ConcurrentHashMap<>();

//    @EventListener
//    public void onSessionConnected(SessionConnectedEvent event) {
//        String sabun = event.getUser().getName();
//        sessions.put(sabun = event.)
//    }

    @EventListener
    public void handleDisconnect(SessionDisconnectEvent event) {

        System.out.println("handleDisconnect : " + event.getSessionId());
        try {
            subscriptionRegistry.unregisterAllSubscriptions(event.getSessionId());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        sessionRegistry.removeSessionInformation(event.getSessionId());
    }
}
