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

//拿视频回放


import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.dahuatech.icc.exception.ClientException;
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.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.Video.*;
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("/videoTest")

public class VideoRecorControllerSg {

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

    @Autowired
    private IccChannelMapper iccChannelMapper;

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

        LambdaQueryWrapper<IccChannel> wrapper3 = Wrappers.lambdaQuery();
        wrapper3.eq(IccChannel::getChannelName, channelName);
        wrapper3.eq(IccChannel::getRemark, farmId);
        IccChannel iccChannel = iccChannelMapper.selectOne(wrapper3);
        System.out.println(iccChannel);

        //开始结束时间代为处理
        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);


        Map<String, Object> test = new HashMap<>();
        test.put("channelId", iccChannel.getChannelId());
        test.put("recordSource", "2");
        test.put("startTime", befor);
        test.put("endTime", after);
        test.put("streamType", "0");
        test.put("recordType", "0");
        if (farmId == 1) {
            System.out.println("曙光");
            VideoRecordQueryRequestSg queryRequestSg = new VideoRecordQueryRequestSg(oauthConfigBaseInfo);
            queryRequestSg.setData(test);
            System.out.println(queryRequestSg);
            com.alibaba.fastjson.JSONObject jsonObject2 = queryRequestSg.doAction();
            System.out.println(jsonObject2);
            JSONArray jsonArray = JSONArray.parseArray(jsonObject2.get("records").toString());
            System.out.println(jsonArray);
            com.alibaba.fastjson.JSONObject json = (com.alibaba.fastjson.JSONObject) jsonArray.get(0);
            System.out.println(json);
            Map<String, Object> value = new HashMap<>();
            value.put("recordType", json.get("recordType"));
            value.put("streamType", json.get("streamType"));
            value.put("recordSource", json.get("recordSource"));
            value.put("channelId", json.get("channelId"));
            value.put("endTime", json.get("endTime"));
            value.put("startTime", json.get("startTime"));

            VideoRecordRequestSg requestSg = new VideoRecordRequestSg(oauthConfigBaseInfo);
            requestSg.setData(value);
            Object jsonObject1 = requestSg.doAction();
            System.out.println(jsonObject1);
            String action = (String) JSONObject.fromObject(jsonObject1).get("url");
            String token = (String) JSONObject.fromObject(jsonObject1).get("token");
            String[] split = action.split("\\|");// |需要转义符
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("url", split[1] + "?token=" + token + "&trackID=701");
            return R.ok().put("data", jsonObject);
        } else if (farmId == 22) {
            System.out.println("海宁");
            VideoRecordQueryRequestHn queryRequestHn = new VideoRecordQueryRequestHn(oauthConfigBaseInfo02);
            queryRequestHn.setData(test);
            System.out.println(queryRequestHn);
            com.alibaba.fastjson.JSONObject jsonObject2 = queryRequestHn.doAction();
            System.out.println(jsonObject2);
            JSONArray jsonArray = JSONArray.parseArray(jsonObject2.get("records").toString());
            System.out.println(jsonArray);
            com.alibaba.fastjson.JSONObject json = (com.alibaba.fastjson.JSONObject) jsonArray.get(0);
            System.out.println(json);
            Map<String, Object> value = new HashMap<>();
            value.put("recordType", json.get("recordType"));
            value.put("streamType", json.get("streamType"));
            value.put("recordSource", json.get("recordSource"));
            value.put("channelId", json.get("channelId"));
            value.put("endTime", json.get("endTime"));
            value.put("startTime", json.get("startTime"));

            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();
            String action = (String) JSONObject.fromObject(jsonObject1).get("url");
            String token = (String) JSONObject.fromObject(jsonObject1).get("token");
            String[] split = action.split("\\|");// |需要转义符
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("url", split[1] + "?token=" + token);
            return R.ok().put("data", jsonObject);
        } else if (farmId == 21) {
            System.out.println("苍南");
            VideoRecordQueryRequestCn queryRequestCn = new VideoRecordQueryRequestCn(oauthConfigBaseInfo03);
            queryRequestCn.setData(test);
            System.out.println(queryRequestCn);
            com.alibaba.fastjson.JSONObject jsonObject2 = queryRequestCn.doAction();
            System.out.println(jsonObject2);
            JSONArray jsonArray = JSONArray.parseArray(jsonObject2.get("records").toString());
            System.out.println(jsonArray);
            com.alibaba.fastjson.JSONObject json = (com.alibaba.fastjson.JSONObject) jsonArray.get(0);
            System.out.println(json);
            Map<String, Object> value = new HashMap<>();
            value.put("recordType", json.get("recordType"));
            value.put("streamType", json.get("streamType"));
            value.put("recordSource", json.get("recordSource"));
            value.put("channelId", json.get("channelId"));
            value.put("endTime", json.get("endTime"));
            value.put("startTime", json.get("startTime"));

            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();
            String action = (String) JSONObject.fromObject(jsonObject1).get("url");
            String token = (String) JSONObject.fromObject(jsonObject1).get("token");
            String[] split = action.split("\\|");// |需要转义符
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("url", split[1] + "?token=" + token + "&trackID=701");
            return R.ok().put("data", jsonObject);
        } else {
            return R.error();
        }

    }


}
