package com.ruoyi.framework.websocket;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.service.impl.SysUserOnlineTimeServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
@Configuration
public class WebSocketComponentConfig {

    private static final Logger LOGGER = LoggerFactory.getLogger(WebSocketComponentConfig.class);

    private final LocationService locationService;
    private final ObjectMapper objectMapper;


    private final SysUserOnlineTimeServiceImpl userOnlineTimeWebSocketServer;
    private final TokenService tokenService;
    private final ObjectMapper onlineTimeObjectMapper;


    // 使用构造器注入
    public WebSocketComponentConfig(LocationService locationService,SysUserOnlineTimeServiceImpl  userOnlineTimeWebSocketServer, ObjectMapper objectMapper, ObjectMapper onlineTimeObjectMapper
    , TokenService tokenService) {
        this.locationService = locationService;
        this.userOnlineTimeWebSocketServer = userOnlineTimeWebSocketServer;

        this.objectMapper = objectMapper;
        this.onlineTimeObjectMapper = onlineTimeObjectMapper;
        this.tokenService = tokenService;
    }

    @PostConstruct
    public void init() {
        try {
            LocationWebSocketServer.setLocationService(locationService);
            LocationWebSocketServer.setObjectMapper(objectMapper);

            UserOnlineTimeWebSocketServer.setObjectMapper(onlineTimeObjectMapper);
            UserOnlineTimeWebSocketServer.setTokenService(tokenService);
            UserOnlineTimeWebSocketServer.setUserOnlineTimeService(userOnlineTimeWebSocketServer);

            LOGGER.info("WebSocket组件初始化成功");
        } catch (Exception e) {
            LOGGER.error("WebSocket组件初始化失败", e);
            throw new RuntimeException("WebSocket组件初始化失败", e);
        }
    }
}
