package com.multictrl.modules.business.controller;

import cn.hutool.json.JSONArray;
import com.multictrl.common.utils.Result;
import com.multictrl.modules.business.dto.SrsCallBackDTO;
import com.multictrl.modules.business.service.SrsService;
import io.swagger.v3.oas.annotations.Hidden;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * SRS直播流管理
 *
 * @author Sdy
 * @since 1.0.0 2026-05-07
 */
@Slf4j
@Hidden
@RestController
@RequestMapping("/srs")
@Tag(name = "SRS直播流管理")
@RequiredArgsConstructor
public class SrsController {
    private final SrsService srsService;

    @PostMapping("/streamsBySn/{dockSn}")
    @Operation(summary = "根据sn号查询流列表（查所有数据时sn传all）")
    public Result<JSONArray> streamsBySn(@PathVariable String dockSn) {

        return new Result<JSONArray>().ok(srsService.streamsBySn(dockSn));
    }

    @Operation(hidden = true)
    @PostMapping("/on_connect")
    public String onConnect(@RequestBody SrsCallBackDTO srsCallBack) {
//        log.info("on_connect  {}", srsCallBack);
        return "0";
    }

    @Operation(hidden = true)
    @PostMapping("/on_close")
    public String onClose(@RequestBody SrsCallBackDTO srsCallBack) {
//        log.info("on_close  {}", srsCallBack);
        return "0";
    }


    @Operation(hidden = true)
    @PostMapping("/on_publish")
    public String onPublish(@RequestBody SrsCallBackDTO srsCallBack) {
//        log.info("on_publish  {}", srsCallBack);
        if (srsService.checkToken(srsCallBack)) {
            return "0";
        }
        return "-1";
    }


    @Operation(hidden = true)
    @PostMapping("/on_unpublish")
    public String onUnpublish(@RequestBody SrsCallBackDTO srsCallBack) {
//        log.info("on_unpublish  {}", srsCallBack);
        return "0";
    }


    @Operation(hidden = true)
    @PostMapping("/on_play")
    public String onPlay(@RequestBody SrsCallBackDTO srsCallBack) {
//        log.debug("on_play  {}", srsCallBack);
        return "0";
    }

    @Operation(hidden = true)
    @PostMapping("/on_stop")
    public String onStop(@RequestBody SrsCallBackDTO srsCallBack) {
//        log.debug("on_stop  {}", srsCallBack);
        return "0";
    }

    @Operation(hidden = true)
    @PostMapping("/on_dvr")
    public String onDvr(@RequestBody SrsCallBackDTO srsCallBack) {
//        log.info("on_dvr  {}", srsCallBack);
        srsService.saveSrsRecord(srsCallBack);
        return "0";
    }

    public static void main(String[] args) {
        String file = "/live_record/zc/aaaaa/2025-04-03_16-33-43-404.mp4";
        String dateTime = (file = file.substring(file.lastIndexOf("/") + 1))
                .substring(0, file.indexOf("."));
        dateTime = dateTime.split("_")[0] + " " + dateTime.split("_")[1]
                .replaceAll("-", ":");
        System.out.println(dateTime);
    }

    @Operation(hidden = true)
    @PostMapping("/on_hls")
    public String onHls(@RequestBody SrsCallBackDTO srsCallBack) {
//        log.debug("on_hls  {}", srsCallBack);

        return "0";
    }

    /*@Operation(hidden = true)
    @PostMapping("/on_hls_notify")
    public String onHlsNotify(@RequestBody SrsCallBackDTO srsCallBack) {
        log.debug("on_hls_notify  {}", srsCallBack);
        return "0";
    }*/

    @Operation(hidden = true)
    @RequestMapping(value = "/on_hls_notify", method = {RequestMethod.GET, RequestMethod.POST})
    public String onHlsNotify(@RequestParam Map<String, String> allParams) {
//        log.debug("on_hls_notify: {}", allParams);
        return "0";
    }
}
