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

//拿视频回放

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.dahuatech.hutool.http.Method;
import com.dahuatech.icc.exception.ClientException;
import com.dahuatech.icc.oauth.http.DefaultClient;
import com.dahuatech.icc.oauth.http.IClient;
import com.dahuatech.icc.oauth.model.v202010.GeneralRequest;
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 net.sf.json.JSONObject;
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("/video")

public class VideoRecorController {

    @Autowired
    private IIccChannelService iIccChannelService;

    @Autowired
    private IccChannelMapper iccChannelMapper;

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

        if (farmId != 1) {
            return R.ok("当前牧场无设备").put("URL", null);
        }

        LambdaQueryWrapper<IccChannel> wrapper3 = Wrappers.lambdaQuery();
        wrapper3.eq(IccChannel::getChannelName, channelNmae);
        IccChannel iccChannel = iccChannelMapper.selectOne(wrapper3);
        System.out.println(iccChannel);
        String URL = "/evo-apigw/admin/API/SS/Playback/StartPlaybackByTime";  //获取事件URL    post请求
        IClient iClient = new DefaultClient();
        GeneralRequest generalRequest = new GeneralRequest(URL, Method.POST);
        System.out.println("开始执行");
        Map<String, Object> params = new HashMap<>();
        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", iccChannel.getChannelId());
        System.out.println(iccChannel.toString());
        //开始结束时间代为处理
        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);
        params.put("clientMac", "30:9c:23:79:40:08");
        params.put("clientPushId", "");
        params.put("project", "PSDK");
        params.put("method", "SS.Playback.StartPlaybackByTime");
        params.put("data", value);
        generalRequest.body(JSON.toJSONString(params));
        System.out.println(JSON.toJSONString(params));
        //这种已经在配置文件里面安排了账号ip以及密码
        generalRequest.header("Content-Type", " application/json");
        //发起请求处理应答
        GeneralResponse generalResponse = iClient.doAction(generalRequest, generalRequest.getResponseClass());
        JSONObject jsonObject = JSONObject.fromObject(generalResponse.getResult());
        System.out.println(jsonObject.toString());
        Object data = jsonObject.get("data");
        String url = (String) JSONObject.fromObject(data).get("url");
        String token = (String) JSONObject.fromObject(data).get("token");
        // "|"  特殊字符需要加转义字符
        String[] sourceStrArray = url.split("\\|");
        String recordUrl = sourceStrArray[1] + "?token=" + token + "&trackID=701";
        return R.ok("请求成功").put("URL", recordUrl);
    }


}
