package com.sptg.gateway.controller.videocloud;

import cn.hutool.json.JSONUtil;
import com.sptg.video.cloud.api.dto.VCDeviceDto;
import com.sptg.video.cloud.api.entity.VCDevice;
import com.sptg.video.cloud.api.feigns.VCDeviceFeign;
import lombok.AllArgsConstructor;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * MediaServer回调事件
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/videoCloud/index/hook")
public class MediaServerWebHookController {

    private final VCDeviceFeign vcDeviceFeign;

    /**
     * 监听流注册和注释事件
     * @param params
     * @return
     * on_stream_changed ---> {"app":"live","schema":"ts","vhost":"__defaultVhost__","mediaServerId":"27dwhzCb79Lx79RC","stream":"64012100491327627561","regist":false}
     */
    @PostMapping(value = "/on_stream_changed")
    public Map<String, Object> onStreamChanged(@RequestBody Map<String, Object> params){
        log.error("【MediaServer】on_stream_changed ---> " + JSONUtil.toJsonStr(params));
        Boolean regist = (Boolean)params.get("regist");
        String stream = (String)params.get("stream");
        VCDevice vcDevice = vcDeviceFeign.findDeviceByNum(stream);
        if(vcDevice != null){
            if(regist){
                vcDevice.setPusherStatus(0); //注册成功，即在推流中
            }else{
                vcDevice.setPusherStatus(1); //已注释，即不存在流
            }
            vcDeviceFeign.update(vcDevice);
        }
        Map<String, Object> returnMap = new HashMap<>();
        returnMap.put("code", 0);
        returnMap.put("msg", "success");
        return returnMap;
    }

//    /**
//     * 监听流无人观看事件
//     * @param params
//     * @return
//     */
//    @PostMapping(value = "/on_stream_none_reader")
//    public Map<String, Object> onStreamNoneReader(@RequestBody Map<String, Object> params){
//        log.error("【MediaServer】on_stream_none_reader ---> " + JSONUtil.toJsonStr(params));
//        Map<String, Object> returnMap = new HashMap<>();
//        returnMap.put("code", 0);
//        returnMap.put("close", false);
//        return returnMap;
//    }

    /**
     * 监听流未找到事件
     * @param params
     * @return
     * on_stream_not_found ---> {"app":"live","schema":"rtmp","ip":"218.19.139.228","params":"","vhost":"__defaultVhost__","mediaServerId":"27dwhzCb79Lx79RC","port":36330,"stream":"64012100491327627561","id":"139839437813456"}
     */
    @PostMapping(value = "/on_stream_not_found")
    public Map<String, Object> onStreamNotFound(@RequestBody Map<String, Object> params){
        log.error("【MediaServer】on_stream_not_found ---> " + JSONUtil.toJsonStr(params));
        String stream = (String)params.get("stream");
        VCDevice vcDevice = vcDeviceFeign.findDeviceByNum(stream);
        if(vcDevice != null){
            vcDevice.setPusherStatus(1);
            vcDeviceFeign.update(vcDevice);
            //向MediaServer关闭流
            VCDeviceDto vcDeviceDto = new VCDeviceDto();
            vcDeviceDto.setDeviceNum(vcDevice.getDeviceNum());
            vcDeviceDto.setKey(vcDevice.getKey());
            if(StringUtils.isNotBlank(vcDevice.getKey())){
                this.vcDeviceFeign.msDelFFmpegSource(vcDeviceDto);
            }
        }
        Map<String, Object> returnMap = new HashMap<>();
        returnMap.put("code", 0);
        returnMap.put("msg", "success");
        return returnMap;
    }
}
