package com.inspect.ivs.controller;

import com.inspect.base.core.domain.Response;
import com.inspect.base.core.utils.StringUtils;
import com.inspect.base.core.utils.uuid.UUID;
import com.inspect.base.redis.service.RedisService;
import com.inspect.ivs.constant.IvsConst;
import com.inspect.ivs.enums.RtspType;
import com.inspect.ivs.service.IvsCommonService;
import com.inspect.ivs.service.XinTongComponent;
import com.inspect.ivs.service.ZlMediaComponent;
import com.inspect.ivs.view.IvsVideoRtspView;
import com.inspect.ivs.view.IvsStreamStartView;
import com.inspect.ivs.vo.IvsVideoRtspVo;
import com.inspect.ivs.vo.IvsStreamStartVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping({"/api/v1/stream"})
public class IvsStreamController {
    private static final Logger log = LoggerFactory.getLogger(IvsStreamController.class);

    @Value("${ivs.test:false}")
    private Boolean bIvsTestMode;

    @Value("${rtsp.type:xintong}")
    private RtspType rtspType;

    private final IvsCommonService ivsCommonService;
    private final ZlMediaComponent zlMediaComponent;
    private final XinTongComponent xinTongComponent;

    @Resource
    private RedisService redisService;

    public IvsStreamController(IvsCommonService ivsCommonService, ZlMediaComponent zlMediaComponent, XinTongComponent xinTongComponent) {
        this.ivsCommonService = ivsCommonService;
        this.zlMediaComponent = zlMediaComponent;
        this.xinTongComponent = xinTongComponent;
    }

    @GetMapping({"start"})
    public IvsStreamStartView startStream(IvsStreamStartVo ivsStreamStartVo) {
        log.info("IVS_STREAM_START param: {}", ivsStreamStartVo);
        //ivsStreamStartVo.setCameraCode("02160558340532190147");
        IvsVideoRtspVo ivsVideoRtspVo = IvsVideoRtspVo.realStream(ivsStreamStartVo.getCameraCode());
        log.info("IVS_STREAM_START ivsVideoRtspVo: {}", ivsVideoRtspVo);
        IvsVideoRtspView ivsVideoRtspView;
        if (bIvsTestMode) {
//            ivsVideoRtspView = IvsVideoRtspView.builder()
//                    .rtspURL("rtsp://admin:wd19216811@192.168.1.249:554/h264/ch39/main/av_stream")
//                    .build();
            ivsVideoRtspView = new IvsVideoRtspView();
            ivsVideoRtspView.setRtspURL("rtsp://admin:htjc2018@192.168.1.134:554/cam/realmonitor?channel=3&subtype=0");
        } else {
            String label = java.util.UUID.randomUUID().toString().trim().replaceAll(StringUtils.DASH, StringUtils.EMPTY);
            ivsVideoRtspView = ivsCommonService.postJson(
                    label,
                    ivsVideoRtspVo,
                    IvsConst.URI_VIDEO_RTSP,
                    IvsVideoRtspView.class);
        }

        log.debug("[IVS] {}", ivsVideoRtspView);
        String streamId = UUID.randomUUID().toString();
        redisService.setCacheObject(String.format(IvsConst.IVS_STREAM_ID, streamId), ivsVideoRtspVo.getCameraCode());
        log.debug("[IVS] => => rtspType: {}, rtspType.ordinal: {}, streamId: {}", rtspType, rtspType.ordinal(), streamId);
        switch (rtspType.ordinal()) {
            case 0:
                zlMediaComponent.convert(ivsVideoRtspView.getRtspURL(), streamId, ivsVideoRtspView);
                break;
            case 1:
                xinTongComponent.convert(ivsVideoRtspView.getRtspURL(), streamId, ivsVideoRtspView);
        }

        IvsStreamStartView ivsStreamStartView = new IvsStreamStartView(streamId, ivsVideoRtspView);
        log.debug("[IVS] {}", ivsStreamStartView);
        log.debug("[IVS] end");
        return ivsStreamStartView;
    }

    @GetMapping({"stop"})
    public Response stopStream(String streamId) {
        log.debug("[GAB] streamId: {}", streamId);
        return Response.ok();
    }


    @GetMapping({"rtspUrl"})
    public IvsVideoRtspView rtspUrl(@RequestParam("cameraCode") String cameraCode) {
        log.info("IVS_STREAM_START rtspUrl param: {}", cameraCode);
        IvsVideoRtspVo ivsVideoRtspVo = IvsVideoRtspVo.realStream(cameraCode);
        log.info("IVS_STREAM_START rtspUrl ivsVideoRtspVo: {}", ivsVideoRtspVo);
        IvsVideoRtspView ivsVideoRtspView;
        if (bIvsTestMode) {
//            ivsVideoRtspView = IvsVideoRtspView.builder()
//                    .rtspURL("rtsp://admin:wd19216811@192.168.1.249:554/h264/ch39/main/av_stream")
//                    .build();
            ivsVideoRtspView = new IvsVideoRtspView();
            ivsVideoRtspView.setRtspURL("rtsp://admin:htjc2018@192.168.1.134:554/cam/realmonitor?channel=3&subtype=0");
        } else {
            String label = java.util.UUID.randomUUID().toString().trim().replaceAll(StringUtils.DASH, StringUtils.EMPTY);
            ivsVideoRtspView = ivsCommonService.postJson(
                    label,
                    ivsVideoRtspVo,
                    IvsConst.URI_VIDEO_RTSP,
                    IvsVideoRtspView.class);
        }

        log.debug("[IVS] {}", ivsVideoRtspView);

        return ivsVideoRtspView;
    }
}
