package cn.reghao.tnb.message.app.ws.device;

import cn.reghao.jutil.jdk.serializer.JsonConverter;
import cn.reghao.tnb.message.api.dto.MediaProgress;
import cn.reghao.tnb.message.app.ws.device.msg.DeviceEvent;
import cn.reghao.tnb.message.app.ws.device.msg.DeviceEventType;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.*;

import java.io.IOException;

/**
 * @author reghao
 * @date 2025-10-12 14:52:42
 */
@Slf4j
@Component
public class DeviceHandler implements WebSocketHandler {
    private final DeviceService deviceService;

    public DeviceHandler(DeviceService deviceService) {
        this.deviceService = deviceService;
    }

    @Override
    public void afterConnectionEstablished(WebSocketSession webSocketSession) throws IOException {
        boolean ret = deviceService.addSession(webSocketSession);
        if (ret) {
            log.info("WebSocket 建立连接");
        } else {
            log.info("没有认证信息, 关闭 WebSocket 连接");
            webSocketSession.close(CloseStatus.NO_STATUS_CODE);
        }
    }

    @Override
    public void handleMessage(WebSocketSession webSocketSession, WebSocketMessage<?> webSocketMessage)
            throws IOException {
        if (webSocketMessage instanceof TextMessage) {
            dispatchTextMessage(webSocketSession, (TextMessage) webSocketMessage);
        } else if (webSocketMessage instanceof BinaryMessage) {
            log.info("接收到 WebSocket 二进制消息");
        } else if (webSocketMessage instanceof PingMessage) {
            log.info("接收到 WebSocket PingMessage");
        } else if (webSocketMessage instanceof PongMessage) {
            log.info("接收到 WebSocket PongMessage");
        } else {
            log.error("接收到未知类型的 WebSocket 消息");
        }
    }

    @Override
    public void handleTransportError(WebSocketSession webSocketSession, Throwable throwable) {
        log.error("WebSocket 数据传输错误: {}", throwable.getMessage());
        deviceService.removeSession(webSocketSession);
    }

    @Override
    public void afterConnectionClosed(WebSocketSession webSocketSession, CloseStatus closeStatus) {
        log.info("WebSocket 断开连接: {} -> {}", closeStatus.getCode(), closeStatus.getReason());
        deviceService.removeSession(webSocketSession);
    }

    @Override
    public boolean supportsPartialMessages() {
        return false;
    }

    private void dispatchTextMessage(WebSocketSession webSocketSession, TextMessage textMessage) {
        String sessionId = webSocketSession.getId();
        String payload = textMessage.getPayload();
        try {
            DeviceEvent deviceEvent = JsonConverter.jsonToObject(payload, DeviceEvent.class);
            String type = deviceEvent.getType();
            DeviceEventType deviceEventType = DeviceEventType.valueOf(type);
            switch (deviceEventType) {
                case heartbeat:
                    deviceService.sendHeartbeatPong(webSocketSession);
                    break;
                case start_cam:
                    String jsonStr = deviceEvent.getData().toString();
                    MediaProgress mediaProgress = JsonConverter.jsonToObject(jsonStr, MediaProgress.class);
                    break;
                case shutdown_cam:
                    break;
                default:
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
