package com.huimv.video.dhicc.test.controller;

//拿视频回放


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.dahuatech.icc.exception.ClientException;
import com.dahuatech.icc.oauth.model.v202010.GeneralResponse;
import com.huimv.video.dhicc.entity.IccChannel;
import com.huimv.video.dhicc.mapper.IccChannelMapper;
import com.huimv.video.dhicc.result.R;
import com.huimv.video.dhicc.service.IIccChannelService;
import com.huimv.video.dhicc.test.config.OauthConfigBaseInfo;
import com.huimv.video.dhicc.test.config.OauthConfigBaseInfo02;
import com.huimv.video.dhicc.test.config.OauthConfigBaseInfo03;
import com.huimv.video.dhicc.test.request.VideoRecordRequestCn;
import com.alibaba.fastjson.JSONObject;
import com.huimv.video.dhicc.test.request.VideoRecordRequestHn;
import com.huimv.video.dhicc.test.request.VideoRecordRequestSg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

//方便前端操作   传入的是  事件时间  还有通道编号就可以播放了
@RestController
@RequestMapping("/videoTest")

public class VideoRecorControllerSg {

    @Autowired
    private IIccChannelService iIccChannelService;

    @Autowired
    private IccChannelMapper iccChannelMapper;
    @Autowired
    private OauthConfigBaseInfo oauthConfigBaseInfo;
    @Autowired
    private OauthConfigBaseInfo02 oauthConfigBaseInfo02;
    @Autowired
    private OauthConfigBaseInfo03 oauthConfigBaseInfo03;

    @RequestMapping("/getVideoRecord")
    public R getVideoRecord(@RequestBody Map<String, Object> params1) throws ClientException, ParseException {
        String happendTime = (String) params1.get("happendTime");
        String channelId = (String) params1.get("channelId");
        String farmId = (String) params1.get("farmId");

        Map<String, Object> value = new HashMap<>();
        value.put("nvrId", "");
        value.put("optional", "/evo-apigw/admin/API/SS/Playback/StartPlaybackByTime");
        value.put("recordType", "1");
        value.put("streamType", "1");
        value.put("recordSource", "2");
        value.put("channelId", channelId);

        //开始结束时间代为处理
        DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //格式化传入的时间---拿传入时间前后五分钟的视频
        Date Time = fmt.parse(happendTime);   //事件时间
        Date GapTimeBefor_gap = new Date(Time.getTime() - 1000 * 10);//star   事件十秒之前
        Date GapAfter = new Date(Time.getTime() + 1000 * 60 * 5);// end   事件五分钟之后
        String befor = String.valueOf(GapTimeBefor_gap.getTime() / 1000);
        String after = String.valueOf(GapAfter.getTime() / 1000);
        value.put("endTime", after);
        value.put("startTime", befor);


        if ("1".equals(farmId)) {
            System.out.println("曙光");
            VideoRecordRequestSg requestSg = new VideoRecordRequestSg(oauthConfigBaseInfo);
            requestSg.setClientMac("30:9c:23:79:40:08");
            requestSg.setClientPushId("");
            requestSg.setProject("PSDK");
            requestSg.setMethod("SS.Playback.StartPlaybackByTime");
            requestSg.setData(value);
            Object jsonObject1 = requestSg.doAction();
            System.out.println(jsonObject1);
            return R.ok().put("data",jsonObject1);
        } else if ("2".equals(farmId)) {
            System.out.println("海宁");
            VideoRecordRequestHn requestHn = new VideoRecordRequestHn(oauthConfigBaseInfo02);
            requestHn.setClientMac("30:9c:23:79:40:08");
            requestHn.setClientPushId("");
            requestHn.setProject("PSDK");
            requestHn.setMethod("SS.Playback.StartPlaybackByTime");
            requestHn.setData(value);
            Object jsonObject1 = requestHn.doAction();
            System.out.println(jsonObject1);
            return R.ok().put("data",jsonObject1);
        } else {
            System.out.println("苍南");
            VideoRecordRequestCn requestCn = new VideoRecordRequestCn(oauthConfigBaseInfo03);
            requestCn.setClientMac("30:9c:23:79:40:08");
            requestCn.setClientPushId("");
            requestCn.setProject("PSDK");
            requestCn.setMethod("SS.Playback.StartPlaybackByTime");
            requestCn.setData(value);
            Object jsonObject1 = requestCn.doAction();
            System.out.println(jsonObject1);
            return R.ok().put("data",jsonObject1);
        }

    }


}
