package com.inspect.ivs.controller;

import com.inspect.base.core.domain.Response;
import com.inspect.base.core.utils.DateUtils;
import com.inspect.base.core.utils.StringUtils;
import com.inspect.base.redis.service.RedisService;
import com.inspect.ivs.constant.IvsConst;
import com.inspect.ivs.service.IvsCommonService;
import com.inspect.ivs.service.ZlMediaComponent;
import com.inspect.ivs.util.UriUtils;
import com.inspect.ivs.view.IvsRecListView;
import com.inspect.ivs.view.IvsVideoRtspView;
import com.inspect.ivs.view.IvsRecStopView;
import com.inspect.ivs.vo.IvsPlaybackRec2ListVo;
import com.inspect.ivs.vo.IvsRecStartVo;
import com.inspect.ivs.vo.IvsVideoRtspVo;
import com.inspect.ivs.vo.IvsVo;

import java.util.Iterator;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping({"/api/v1/record"})
public class IvsRecordController {
    private static final Logger log = LoggerFactory.getLogger(IvsRecordController.class);
    private final IvsCommonService ivsCommonService;
    private final RedisService redisService;
    private final ZlMediaComponent zlmediaComponent;

    public IvsRecordController(IvsCommonService ivsCommonService, RedisService redisService, ZlMediaComponent zlmediaComponent) {
        this.ivsCommonService = ivsCommonService;
        this.redisService = redisService;
        this.zlmediaComponent = zlmediaComponent;
    }

    @GetMapping({"start"})
    public Response start(String streamid) {
        log.info("START_RECORD record start streamId:{}", streamid);
        String code = redisService.getCacheObject(String.format(IvsConst.IVS_STREAM_ID, streamid));
        log.info("START_RECORD record start code:{}", code);
        IvsRecStartVo ivsRecStartVo = new IvsRecStartVo();
        ivsRecStartVo.setCode(code);
        String key = String.format(IvsConst.IVS_RECORD_START_TIME, streamid);
        redisService.setCacheObject(key, DateUtils.dateTimeNow(), IvsConst.IVS_RECORD_START_TIME_EXPIRE, TimeUnit.MINUTES);
        ivsCommonService.get(getLabel(), UriUtils.parse(IvsConst.URI_START_RECORD, ivsRecStartVo));
        log.info("START_RECORD record start end");
        return Response.ok();
    }

    @GetMapping({"stop"})
    public IvsRecStopView stop(String streamid) {
        log.info("STOP_RECORD record stop streamid:{}", streamid);
        String key = String.format(IvsConst.IVS_STREAM_ID, streamid);
        String code = redisService.getCacheObject(key);
        IvsVo ivsRecordStartVo = new IvsVo();
        ivsRecordStartVo.setCode(code);
        ivsCommonService.get(getLabel(), UriUtils.parse(IvsConst.URI_STOP_RECORD, ivsRecordStartVo));
        redisService.deleteObject(key);
        key = String.format(IvsConst.IVS_RECORD_START_TIME, streamid);
        String startTime = redisService.getCacheObject(key);
        redisService.deleteObject(key);
        IvsPlaybackRec2ListVo ivsPlaybackRec2ListVo = new IvsPlaybackRec2ListVo(code, startTime, DateUtils.dateTimeNow());
        IvsRecListView ivsRecListView = ivsCommonService.get(getLabel(), UriUtils.parse(IvsConst.URI_RECORD_LIST, ivsPlaybackRec2ListVo), IvsRecListView.class);
        IvsRecStopView ivsRecStopView = new IvsRecStopView(streamid, ivsRecListView.getRecordInfos().getTotal());

        for (IvsRecListView.RecordInfo recordInfo : ivsRecListView.getRecordInfos().getRecordInfoList().getRecordInfo()) {
            IvsVideoRtspVo ivsVideoRtspVo = IvsVideoRtspVo.download(code, recordInfo);
            IvsVideoRtspView ivsVideoRtspView = ivsCommonService.postJson(getLabel(), ivsVideoRtspVo, IvsConst.URI_VIDEO_RTSP, IvsVideoRtspView.class);
            zlmediaComponent.convert(ivsVideoRtspView.getRtspURL(), streamid, ivsVideoRtspView);
            ivsRecStopView.addRecord(recordInfo.getRecordTime().getStartTime(), recordInfo.getRecordTime().getEndTime(), ivsVideoRtspView.getDownloadURL());
        }

        key = String.format(IvsConst.IVS_DOWNLOAD_STREAM_ID, streamid);
        redisService.setCacheObject(key, ivsRecStopView.toJSON(), IvsConst.IVS_STREAM_ID_EXPIRE, TimeUnit.HOURS);
        log.info("STOP_RECORD record stop end streamid:{}", streamid);
        return ivsRecStopView;
    }

    public String getLabel() {
        return UUID.randomUUID().toString().trim().replaceAll(StringUtils.DASH, StringUtils.EMPTY);
    }
}
