package com.ttstd.signaling.manager;

import com.ttstd.signaling.model.DeviceInfo;
import com.ttstd.signaling.model.DeviceType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.WebSocketSession;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

@Component
public class SessionManager {

    private static final Logger logger = LoggerFactory.getLogger(SessionManager.class);

    // deviceId -> WebSocketSession
    private final Map<String, WebSocketSession> sessions = new ConcurrentHashMap<>();
    // deviceId -> deviceType
    private final Map<String, DeviceType> deviceTypes = new ConcurrentHashMap<>();
    // session.getId() -> deviceId (用于断开连接时清理)
    private final Map<String, String> sessionToDevice = new ConcurrentHashMap<>();
    // deviceId -> 注册（上线）时间戳
    private final Map<String, Long> connectTime = new ConcurrentHashMap<>();

    public void registerDevice(String deviceId, DeviceType deviceType, WebSocketSession session) {
        sessions.put(deviceId, session);
        deviceTypes.put(deviceId, deviceType);
        sessionToDevice.put(session.getId(), deviceId);
        connectTime.put(deviceId, System.currentTimeMillis());
        logger.info("Device registered: {} ({})", deviceId, deviceType);
    }

    public void unregisterSession(WebSocketSession session) {
        String deviceId = sessionToDevice.remove(session.getId());
        if (deviceId != null) {
            sessions.remove(deviceId);
            deviceTypes.remove(deviceId);
            connectTime.remove(deviceId);
            logger.info("Device unregistered: {}", deviceId);
        }
    }

    public WebSocketSession getSession(String deviceId) {
        return sessions.get(deviceId);
    }

    public boolean isDeviceOnline(String deviceId) {
        WebSocketSession session = sessions.get(deviceId);
        return session != null && session.isOpen();
    }

    public DeviceType getDeviceType(String deviceId) {
        return deviceTypes.get(deviceId);
    }

    public List<String> getDevicesByType(DeviceType type) {
        List<String> result = new ArrayList<>();
        for (Map.Entry<String, DeviceType> entry : deviceTypes.entrySet()) {
            if (entry.getValue() == type && isDeviceOnline(entry.getKey())) {
                result.add(entry.getKey());
            }
        }
        return result;
    }

    public List<String> getAllOnlineDevices() {
        List<String> result = new ArrayList<>();
        for (Map.Entry<String, WebSocketSession> entry : sessions.entrySet()) {
            if (entry.getValue().isOpen()) {
                result.add(entry.getKey());
            }
        }
        return result;
    }

    public int getOnlineCount() {
        int count = 0;
        for (WebSocketSession session : sessions.values()) {
            if (session.isOpen()) {
                count++;
            }
        }
        return count;
    }

    public Long getConnectedAt(String deviceId) {
        return connectTime.get(deviceId);
    }

    /** 返回全部已注册设备的明细（含当前是否在线）。 */
    public List<DeviceInfo> getAllDeviceDetails() {
        Set<String> allIds = new HashSet<>();
        allIds.addAll(sessions.keySet());
        allIds.addAll(deviceTypes.keySet());
        List<DeviceInfo> result = new ArrayList<>();
        for (String id : allIds) {
            DeviceType type = deviceTypes.get(id);
            boolean online = isDeviceOnline(id);
            result.add(new DeviceInfo(id, type == null ? null : type.name(), online, connectTime.get(id)));
        }
        return result;
    }
}
