package com.usky.cdi.controller;

import com.usky.cdi.service.sip.config.SipServerProperties;
import com.usky.cdi.service.sip.model.VideoStreamSession;
import com.usky.cdi.service.sip.service.VideoStreamManager;
import com.usky.cdi.service.sip.util.NetworkUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.web.bind.annotation.*;

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

@RestController
@RequestMapping("/api/sip")
@RequiredArgsConstructor
@ConditionalOnProperty(prefix = "sip.server", name = "enabled", havingValue = "true")
public class SipVideoController {

    private final SipServerProperties properties;
    private final VideoStreamManager streamManager;

    @GetMapping("/status")
    public Map<String, Object> status() {
        Map<String, Object> result = new HashMap<>();
        result.put("enabled", properties.isEnabled());
        result.put("host", properties.getHost());
        result.put("port", properties.getPort());
        result.put("localIp", NetworkUtils.resolveLocalIp(properties.getLocalIp()));
        result.put("platformId", properties.getPlatformId());
        result.put("activeSessions", streamManager.getAllSessions().size());
        return result;
    }

    @GetMapping("/sessions")
    public List<Map<String, Object>> sessions() {
        return streamManager.getAllSessions().stream()
                .map(this::toSessionMap)
                .collect(Collectors.toList());
    }

    @GetMapping("/sessions/{callId}")
    public Map<String, Object> session(@PathVariable String callId) {
        VideoStreamSession session = streamManager.getSession(callId);
        if (session == null) {
            Map<String, Object> notFound = new HashMap<>();
            notFound.put("error", "会话不存在");
            return notFound;
        }
        return toSessionMap(session);
    }

    @DeleteMapping("/sessions/{callId}")
    public Map<String, Object> stopSession(@PathVariable String callId) {
        streamManager.stopSession(callId);
        Map<String, Object> result = new HashMap<>();
        result.put("callId", callId);
        result.put("status", "stopped");
        return result;
    }

    private Map<String, Object> toSessionMap(VideoStreamSession session) {
        Map<String, Object> map = new HashMap<>();
        map.put("sessionId", session.getSessionId());
        map.put("callId", session.getCallId());
        map.put("remoteAddress", session.getRemoteAddress());
        map.put("remoteRtpPort", session.getRemoteRtpPort());
        map.put("localRtpPort", session.getLocalRtpPort());
        map.put("codec", session.getCodec());
        map.put("payloadType", session.getPayloadType());
        map.put("ssrc", session.getSsrc());
        map.put("state", session.getState().name());
        map.put("startTime", session.getStartTime());
        map.put("receivedPackets", session.getReceivedPackets().get());
        map.put("receivedFrames", session.getReceivedFrames().get());
        map.put("receivedBytes", session.getReceivedBytes().get());
        return map;
    }
}
