package com.inspect.patrol.controller;

import com.alibaba.fastjson.JSONObject;
import com.inspect.base.core.utils.HttpClientUtils;
import com.inspect.base.core.web.controller.BaseController;
import com.inspect.base.core.web.domain.AjaxResult;
import com.inspect.common.log.annotation.Log;
import com.inspect.common.log.enums.BizType;
import com.inspect.patrol.contants.AccessApiConstant;
import com.inspect.patrol.domain.RealTimeModel;
import com.inspect.patrol.linkage.util.JsonUtil;
import com.inspect.patrol.service.impl.Video2NvrComponent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
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 springfox.documentation.annotations.ApiIgnore;

@ApiIgnore
@RestController
@RequestMapping({"/video"})
public class VideoController extends BaseController {
    private static final Logger log = LoggerFactory.getLogger(VideoController.class);
    @Value("${gwb.url}")
    private String deviceUrl;
    private final Video2NvrComponent video2NvrComponent;

    public VideoController(Video2NvrComponent video2NvrComponent) {
        this.video2NvrComponent = video2NvrComponent;
    }

//    @Log(
//            title = "开启视频监控",
//            businessType = BizType.OTHER
//    )
//    @GetMapping({"/realtime"})
//    public AjaxResult realtime(RealTimeModel realTimeModel) throws Exception {
//        log.info("[VIDEO] realtime:{}", JSONObject.toJSONString(realTimeModel));
//        realTimeModel.setSerial(this.video2NvrComponent.toNvrCode(realTimeModel.getPatrolDeviceCode()));
//        String params = JsonUtil.json2Str(JSONObject.parseObject(JSONObject.toJSONString(realTimeModel)));
//        log.info("[VIDEO] realtime params:{}", params);
//        String result = HttpClientUtils.get(this.deviceUrl + AccessApiConstant.REALTIME, params);
//        return AjaxResult.success(JSONObject.parseObject(result));
//    }

    @Log(
            title = "开启视频监控",
            businessType = BizType.OTHER
    )
    @GetMapping({"/realtime"})
    public AjaxResult realtime(
            @RequestParam("patroldeviceCode") String patrolDeviceCode,
            @RequestParam("channelCode") String channelCode) throws Exception {
        RealTimeModel realTimeModel = new RealTimeModel();
        realTimeModel.setPatrolDeviceCode(patrolDeviceCode);
        realTimeModel.setChannelCode(channelCode);
        log.info("[VIDEO] realtime:{}", JSONObject.toJSONString(realTimeModel));
        realTimeModel.setSerial(video2NvrComponent.toNvrCode(realTimeModel.getPatrolDeviceCode()));
        String params = JsonUtil.json2Str(JSONObject.parseObject(JSONObject.toJSONString(realTimeModel)));
        log.info("[VIDEO] realtime params:{}", params);
        String result = null;
        try {
            result = HttpClientUtils.get(this.deviceUrl + AccessApiConstant.REALTIME, params);
        } catch (Exception e) {
            log.error("开启视频监控失败", e);
        }
        return AjaxResult.success(JSONObject.parseObject(result));
    }
}
