package com.usky.cdi.controller;

import com.alibaba.fastjson.JSONObject;
import com.usky.cdi.service.config.Gb28181VideoProperties;
import com.usky.cdi.service.impl.VideoDataTransferService;
import com.usky.cdi.service.impl.VideoStreamPushService;
import com.usky.cdi.service.vo.video.VideoPlayInfoVO;
import com.usky.cdi.service.vo.video.VideoStreamSyncPacketVO;
import com.usky.common.core.bean.ApiResult;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * GB28181 视频对接运维接口
 */
@RestController
@RequestMapping("/video/gb28181")
@RequiredArgsConstructor
@ConditionalOnProperty(prefix = "gb28181.video", name = "enabled", havingValue = "true")
public class VideoIntegrationController {

    private final VideoDataTransferService videoDataTransferService;
    private final VideoStreamPushService videoStreamPushService;
    private final Gb28181VideoProperties properties;

    /**
     * 检查配置（SIP 端口 + 平台 API 登录）
     */
    @GetMapping("/check")
    public ApiResult<JSONObject> checkConfiguration() {
        return ApiResult.success(videoDataTransferService.checkConfiguration());
    }

    /**
     * 手动触发视频设备同步
     */
    @PostMapping("/sync")
    public ApiResult<Void> syncDevices() {
        videoDataTransferService.synchronizeVideoDevices();
        return ApiResult.success();
    }

    /**
     * 手动触发单路视频流推送
     */
    @PostMapping("/stream/push")
    public ApiResult<VideoPlayInfoVO> pushStream(@RequestParam String deviceId,
                                                 @RequestParam String channelId) {
        return ApiResult.success(videoStreamPushService.pushStream(deviceId, channelId));
    }

    /**
     * 批量推送所有在线通道视频流
     */
    @PostMapping("/stream/push-all")
    public ApiResult<VideoStreamSyncPacketVO> pushAllStreams() {
        return ApiResult.success(videoStreamPushService.pushAllOnlineStreams());
    }

    /**
     * 停止单路视频流
     */
    @PostMapping("/stream/stop")
    public ApiResult<Void> stopStream(@RequestParam String deviceId,
                                      @RequestParam String channelId) {
        videoStreamPushService.stopStream(deviceId, channelId);
        return ApiResult.success();
    }

    /**
     * 查看当前活跃推流会话
     */
    @GetMapping("/stream/sessions")
    public ApiResult<Map<String, VideoPlayInfoVO>> activeSessions() {
        return ApiResult.success(videoStreamPushService.getActiveSessions());
    }

    /**
     * 查看当前生效的国标配置（密码脱敏）
     */
    @GetMapping("/config")
    public ApiResult<JSONObject> currentConfig() {
        JSONObject config = new JSONObject();
        config.put("enabled", properties.isEnabled());
        config.put("sipId", properties.getSipId());
        config.put("sipDomain", properties.getSipDomain());
        config.put("sipIp", properties.getSipIp());
        config.put("sipPort", properties.getSipPort());
        config.put("cascadePort", properties.getCascadePort());
        config.put("rtpPortMin", properties.getRtpPortMin());
        config.put("rtpPortMax", properties.getRtpPortMax());
        config.put("apiBaseUrl", properties.getApiBaseUrl());
        config.put("mqttTopic", properties.getMqttTopic());
        config.put("syncCron", properties.getSyncCron());
        config.put("fetchPlayUrl", properties.isFetchPlayUrl());
        config.put("streamPushEnabled", properties.isStreamPushEnabled());
        config.put("streamPushCron", properties.getStreamPushCron());
        config.put("mqttStreamTopic", properties.getMqttStreamTopic());
        config.put("autoPushAllOnline", properties.isAutoPushAllOnline());
        config.put("maxConcurrentStreams", properties.getMaxConcurrentStreams());
        config.put("preferredProtocol", properties.getPreferredProtocol());
        return ApiResult.success(config);
    }
}
