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


import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
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.icc.IccUtil;
import com.huimv.video.dhicc.result.R;
import com.huimv.video.dhicc.util.Result;
import com.huimv.video.dhicc.util.ResultCode;
import com.huimv.video.video.entity.Camera;
import com.huimv.video.video.service.ICameraService;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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

@RestController
@RequestMapping("/clientAllVideoPlayBack")
public class ClientAllVideoPlayBack {

    @Autowired
    private ICameraService iCameraService;

    @PostMapping("/startPlaybackByTime")
    public R videoPlayBack(@RequestBody Map<String,String> map) throws ClientException {
        String id = map.get("id");

        if (StringUtils.isBlank(id)){
            return R.error("摄像头选择有误");
        }
        Camera byId = iCameraService.getById(id);
        if (ObjectUtil.isEmpty(byId) || ObjectUtil.isEmpty(byId.getIccChannel())){
            return R.error("该摄像头未进行回放配置");

        }
        String startTime = map.get("startTime");
        String endTime = map.get("endTime");

        String iccChannel = byId.getIccChannel();

        String URL = "/evo-apigw/admin/API/SS/Playback/StartPlaybackByTime";  //回放URL    post请求
        IClient iClient = new DefaultClient();
        GeneralRequest generalRequest = new GeneralRequest(URL, Method.POST);
        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);
        //开始结束时间代为处理
        value.put("endTime", endTime);
        value.put("startTime",startTime);
//        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);

    }
    @PostMapping("/test")
    public R test() throws ClientException {


        String iccChannel = "1003238$1$0$2";

        String URL = "/evo-apigw/admin/API/SS/Record/QueryRecords";  //
        IClient iClient = new DefaultClient();
        GeneralRequest generalRequest = new GeneralRequest(URL, Method.POST);
        Map<String, Object> params = new HashMap<>();
        Map<String, Object> value = new HashMap<>();
        value.put("recordType", "0");
        value.put("streamType", "0");
        value.put("recordSource", "2");
        value.put("channelId",iccChannel);
        //开始结束时间代为处理
        value.put("endTime", "1654023445");
        value.put("startTime","1654019845");
//        params.put("clientMac", "30:9c:23:79:40:08");
        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", jsonObject);

    }
//    @GetMapping("/test")
//    public void test(){
//        String  data = "{\"success\":true,\"data\":{\"pageData\":[{\"channelName\":\"有机肥后东南围墙\",\"channelCode\":\"1003470$1$0$0\",\"unitType\":1,\"deviceCode\":\"1003470\",\"deviceName\":\"192.168.1.123\",\"ownerCode\":\"001\",\"orgName\":\"根节点\",\"isOnline\":1,\"onlineUpdateVersion\":69,\"updateTime\":1656558450000,\"createTime\":1643079695000},{\"channelName\":\"有机肥后西南围墙\",\"channelCode\":\"1003469$1$0$0\",\"unitType\":1,\"deviceCode\":\"1003469\",\"deviceName\":\"192.168.1.121\",\"ownerCode\":\"001\",\"orgName\":\"根节点\",\"isOnline\":1,\"onlineUpdateVersion\":69,\"updateTime\":1656558472000,\"createTime\":1643079666000},{\"channelName\":\"厂区后北围墙2\",\"channelCode\":\"1003468$1$0$0\",\"unitType\":1,\"deviceCode\":\"1003468\",\"deviceName\":\"192.168.1.138\",\"ownerCode\":\"001\",\"orgName\":\"根节点\",\"isOnline\":1,\"onlineUpdateVersion\":23,\"updateTime\":1653003018000,\"createTime\":1643079628000},{\"channelName\":\"厂区后北围墙1\",\"channelCode\":\"1003467$1$0$0\",\"unitType\":1,\"deviceCode\":\"1003467\",\"deviceName\":\"192.168.1.137\",\"ownerCode\":\"001\",\"orgName\":\"根节点\",\"isOnline\":1,\"onlineUpdateVersion\":23,\"updateTime\":1653003022000,\"createTime\":1643079595000},{\"channelName\":\"厂区后南围墙2\",\"channelCode\":\"1003466$1$0$0\",\"unitType\":1,\"deviceCode\":\"1003466\",\"deviceName\":\"192.168.1.136\",\"ownerCode\":\"001\",\"orgName\":\"根节点\",\"isOnline\":1,\"onlineUpdateVersion\":25,\"updateTime\":1653003015000,\"createTime\":1643079555000},{\"channelName\":\"厂区后南围墙1\",\"channelCode\":\"1003465$1$0$0\",\"unitType\":1,\"deviceCode\":\"1003465\",\"deviceName\":\"192.168.1.135\",\"ownerCode\":\"001\",\"orgName\":\"根节点\",\"isOnline\":1,\"onlineUpdateVersion\":25,\"updateTime\":1653003023000,\"createTime\":1643079525000},{\"channelName\":\"环保区后西围墙1\",\"channelCode\":\"1003464$1$0$0\",\"unitType\":1,\"deviceCode\":\"1003464\",\"deviceName\":\"192.168.1.130\",\"ownerCode\":\"001\",\"orgName\":\"根节点\",\"isOnline\":1,\"onlineUpdateVersion\":25,\"updateTime\":1653003021000,\"createTime\":1643079434000},{\"channelName\":\"环保区后西围墙2\",\"channelCode\":\"1003463$1$0$0\",\"unitType\":1,\"deviceCode\":\"1003463\",\"deviceName\":\"192.168.1.131\",\"ownerCode\":\"001\",\"orgName\":\"根节点\",\"isOnline\":1,\"onlineUpdateVersion\":27,\"updateTime\":1653003020000,\"createTime\":1643079385000},{\"channelName\":\"环保区加药房围墙\",\"channelCode\":\"1003461$1$0$0\",\"unitType\":1,\"deviceCode\":\"1003461\",\"deviceName\":\"192.168.1.129\",\"ownerCode\":\"001\",\"orgName\":\"根节点\",\"isOnline\":1,\"onlineUpdateVersion\":23,\"updateTime\":1653003020000,\"createTime\":1643079289000},{\"channelName\":\"有机肥南围墙1\",\"channelCode\":\"1003460$1$0$0\",\"unitType\":1,\"deviceCode\":\"1003460\",\"deviceName\":\"192.168.1.128\",\"ownerCode\":\"001\",\"orgName\":\"根节点\",\"isOnline\":1,\"onlineUpdateVersion\":73,\"updateTime\":1656558472000,\"createTime\":1643079040000},{\"channelName\":\"有机肥南围墙2\",\"channelCode\":\"1003297$1$0$0\",\"unitType\":1,\"deviceCode\":\"1003297\",\"deviceName\":\"192.168.1.127\",\"ownerCode\":\"001\",\"orgName\":\"根节点\",\"isOnline\":1,\"onlineUpdateVersion\":73,\"updateTime\":1656558455000,\"createTime\":1643024628000},{\"channelName\":\"大门西南围墙1\",\"channelCode\":\"1003296$1$0$0\",\"unitType\":1,\"deviceCode\":\"1003296\",\"deviceName\":\"192.168.1.126\",\"ownerCode\":\"001\",\"orgName\":\"根节点\",\"isOnline\":1,\"onlineUpdateVersion\":75,\"updateTime\":1656558455000,\"createTime\":1643024596000},{\"channelName\":\"大门西南围墙2\",\"channelCode\":\"1003295$1$0$0\",\"unitType\":1,\"deviceCode\":\"1003295\",\"deviceName\":\"192.168.1.125\",\"ownerCode\":\"001\",\"orgName\":\"根节点\",\"isOnline\":1,\"onlineUpdateVersion\":73,\"updateTime\":1656558471000,\"createTime\":1643024519000},{\"channelName\":\"大门北洗消院墙\",\"channelCode\":\"1003284$1$0$0\",\"unitType\":1,\"deviceCode\":\"1003284\",\"deviceName\":\"192.168.1.27\",\"ownerCode\":\"001\",\"orgName\":\"根节点\",\"isOnline\":1,\"onlineUpdateVersion\":37,\"updateTime\":1656574556000,\"createTime\":1643021699000},{\"channelName\":\"门卫室外向北\",\"channelCode\":\"1003281$1$0$0\",\"unitType\":1,\"deviceCode\":\"1003281\",\"deviceName\":\"192.168.1.29\",\"ownerCode\":\"001\",\"orgName\":\"根节点\",\"isOnline\":1,\"onlineUpdateVersion\":39,\"updateTime\":1656574553000,\"createTime\":1643021017000},{\"channelName\":\"门卫室外向南\",\"channelCode\":\"1003278$1$0$0\",\"unitType\":1,\"deviceCode\":\"1003278\",\"deviceName\":\"192.168.1.31\",\"ownerCode\":\"001\",\"orgName\":\"根节点\",\"isOnline\":1,\"onlineUpdateVersion\":38,\"updateTime\":1656574552000,\"createTime\":1643020463000},{\"channelName\":\"门卫室\",\"channelCode\":\"1003276$1$0$0\",\"unitType\":1,\"deviceCode\":\"1003276\",\"deviceName\":\"192.168.1.26\",\"ownerCode\":\"001\",\"orgName\":\"根节点\",\"isOnline\":1,\"onlineUpdateVersion\":36,\"updateTime\":1656574555000,\"createTime\":1643020009000},{\"channelName\":\"饲料仓库围墙2\",\"channelCode\":\"1003238$1$0$9\",\"unitType\":1,\"deviceCode\":\"1003238\",\"deviceName\":\"192.168.1.6\",\"ownerCode\":\"001\",\"orgName\":\"根节点\",\"isOnline\":1,\"onlineUpdateVersion\":691,\"updateTime\":1655510863000,\"createTime\":1643008893000},{\"channelName\":\"饲料仓库围墙1\",\"channelCode\":\"1003238$1$0$8\",\"unitType\":1,\"deviceCode\":\"1003238\",\"deviceName\":\"192.168.1.6\",\"ownerCode\":\"001\",\"orgName\":\"根节点\",\"isOnline\":1,\"onlineUpdateVersion\":690,\"updateTime\":1655510863000,\"createTime\":1643008893000},{\"channelName\":\"饲料塔围墙1\",\"channelCode\":\"1003238$1$0$7\",\"unitType\":1,\"deviceCode\":\"1003238\",\"deviceName\":\"192.168.1.6\",\"ownerCode\":\"001\",\"orgName\":\"根节点\",\"isOnline\":1,\"onlineUpdateVersion\":689,\"updateTime\":1655510863000,\"createTime\":1643008893000},{\"channelName\":\"饲料塔围墙2\",\"channelCode\":\"1003238$1$0$6\",\"unitType\":1,\"deviceCode\":\"1003238\",\"deviceName\":\"192.168.1.6\",\"ownerCode\":\"001\",\"orgName\":\"根节点\",\"isOnline\":1,\"onlineUpdateVersion\":688,\"updateTime\":1655510863000,\"createTime\":1643008893000},{\"channelName\":\"综合楼北围墙2\",\"channelCode\":\"1003238$1$0$5\",\"unitType\":1,\"deviceCode\":\"1003238\",\"deviceName\":\"192.168.1.6\",\"ownerCode\":\"001\",\"orgName\":\"根节点\",\"isOnline\":1,\"onlineUpdateVersion\":687,\"updateTime\":1655510862000,\"createTime\":1643008893000},{\"channelName\":\"综合楼北围墙1\",\"channelCode\":\"1003238$1$0$4\",\"unitType\":1,\"deviceCode\":\"1003238\",\"deviceName\":\"192.168.1.6\",\"ownerCode\":\"001\",\"orgName\":\"根节点\",\"isOnline\":1,\"onlineUpdateVersion\":686,\"updateTime\":1655510862000,\"createTime\":1643008893000},{\"channelName\":\"大门北洗消院墙\",\"channelCode\":\"1003238$1$0$3\",\"unitType\":1,\"deviceCode\":\"1003238\",\"deviceName\":\"192.168.1.6\",\"ownerCode\":\"001\",\"orgName\":\"根节点\",\"isOnline\":1,\"onlineUpdateVersion\":699,\"updateTime\":1656574545000,\"createTime\":1643008893000},{\"channelName\":\"门卫室\",\"channelCode\":\"1003238$1$0$2\",\"unitType\":1,\"deviceCode\":\"1003238\",\"deviceName\":\"192.168.1.6\",\"ownerCode\":\"001\",\"orgName\":\"根节点\",\"isOnline\":1,\"onlineUpdateVersion\":698,\"updateTime\":1656574545000,\"createTime\":1643008893000},{\"channelName\":\"配电房北院墙\",\"channelCode\":\"1003238$1$0$11\",\"unitType\":1,\"deviceCode\":\"1003238\",\"deviceName\":\"192.168.1.6\",\"ownerCode\":\"001\",\"orgName\":\"根节点\",\"isOnline\":1,\"onlineUpdateVersion\":596,\"updateTime\":1650092431000,\"createTime\":1643008893000},{\"channelName\":\"配电房东院墙\",\"channelCode\":\"1003238$1$0$10\",\"unitType\":1,\"deviceCode\":\"1003238\",\"deviceName\":\"192.168.1.6\",\"ownerCode\":\"001\",\"orgName\":\"根节点\",\"isOnline\":1,\"onlineUpdateVersion\":606,\"updateTime\":1650092432000,\"createTime\":1643008893000},{\"channelName\":\"门卫室外向北\",\"channelCode\":\"1003238$1$0$1\",\"unitType\":1,\"deviceCode\":\"1003238\",\"deviceName\":\"192.168.1.6\",\"ownerCode\":\"001\",\"orgName\":\"根节点\",\"isOnline\":1,\"onlineUpdateVersion\":697,\"updateTime\":1656574545000,\"createTime\":1643008893000},{\"channelName\":\"门卫室外向南\",\"channelCode\":\"1003238$1$0$0\",\"unitType\":1,\"deviceCode\":\"1003238\",\"deviceName\":\"192.168.1.6\",\"ownerCode\":\"001\",\"orgName\":\"根节点\",\"isOnline\":1,\"onlineUpdateVersion\":696,\"updateTime\":1656574545000,\"createTime\":1643008893000},{\"channelName\":\"厂区后北围墙1\",\"channelCode\":\"1003236$1$0$9\",\"unitType\":1,\"deviceCode\":\"1003236\",\"deviceName\":\"192.168.1.5\",\"ownerCode\":\"001\",\"orgName\":\"根节点\",\"isOnline\":1,\"onlineUpdateVersion\":483,\"updateTime\":1653003006000,\"createTime\":1643008836000},{\"channelName\":\"厂区后南围墙2\",\"channelCode\":\"1003236$1$0$8\",\"unitType\":1,\"deviceCode\":\"1003236\",\"deviceName\":\"192.168.1.5\",\"ownerCode\":\"001\",\"orgName\":\"根节点\",\"isOnline\":1,\"onlineUpdateVersion\":482,\"updateTime\":1653003006000,\"createTime\":1643008836000},{\"channelName\":\"厂区后南围墙1\",\"channelCode\":\"1003236$1$0$7\",\"unitType\":1,\"deviceCode\":\"1003236\",\"deviceName\":\"192.168.1.5\",\"ownerCode\":\"001\",\"orgName\":\"根节点\",\"isOnline\":1,\"onlineUpdateVersion\":480,\"updateTime\":1653003005000,\"createTime\":1643008836000},{\"channelName\":\"环保区后西围墙2\",\"channelCode\":\"1003236$1$0$6\",\"unitType\":1,\"deviceCode\":\"1003236\",\"deviceName\":\"192.168.1.5\",\"ownerCode\":\"001\",\"orgName\":\"根节点\",\"isOnline\":1,\"onlineUpdateVersion\":479,\"updateTime\":1653003005000,\"createTime\":1643008836000},{\"channelName\":\"环保区后西围墙1\",\"channelCode\":\"1003236$1$0$5\",\"unitType\":1,\"deviceCode\":\"1003236\",\"deviceName\":\"192.168.1.5\",\"ownerCode\":\"001\",\"orgName\":\"根节点\",\"isOnline\":1,\"onlineUpdateVersion\":478,\"updateTime\":1653003005000,\"createTime\":1643008836000},{\"channelName\":\"环保区加药房围墙\",\"channelCode\":\"1003236$1$0$4\",\"unitType\":1,\"deviceCode\":\"1003236\",\"deviceName\":\"192.168.1.5\",\"ownerCode\":\"001\",\"orgName\":\"根节点\",\"isOnline\":1,\"onlineUpdateVersion\":477,\"updateTime\":1653003005000,\"createTime\":1643008836000},{\"channelName\":\"有机肥南围墙1\",\"channelCode\":\"1003236$1$0$3\",\"unitType\":1,\"deviceCode\":\"1003236\",\"deviceName\":\"192.168.1.5\",\"ownerCode\":\"001\",\"orgName\":\"根节点\",\"isOnline\":1,\"onlineUpdateVersion\":680,\"updateTime\":1656558460000,\"createTime\":1643008836000},{\"channelName\":\"有机肥南围墙2\",\"channelCode\":\"1003236$1$0$2\",\"unitType\":1,\"deviceCode\":\"1003236\",\"deviceName\":\"192.168.1.5\",\"ownerCode\":\"001\",\"orgName\":\"根节点\",\"isOnline\":1,\"onlineUpdateVersion\":679,\"updateTime\":1656558460000,\"createTime\":1643008836000},{\"channelName\":\"有机肥后东南围墙\",\"channelCode\":\"1003236$1$0$12\",\"unitType\":1,\"deviceCode\":\"1003236\",\"deviceName\":\"192.168.1.5\",\"ownerCode\":\"001\",\"orgName\":\"根节点\",\"isOnline\":1,\"onlineUpdateVersion\":676,\"updateTime\":1656558459000,\"createTime\":1643008836000},{\"channelName\":\"有机肥后西南围墙\",\"channelCode\":\"1003236$1$0$11\",\"unitType\":1,\"deviceCode\":\"1003236\",\"deviceName\":\"192.168.1.5\",\"ownerCode\":\"001\",\"orgName\":\"根节点\",\"isOnline\":1,\"onlineUpdateVersion\":681,\"updateTime\":1656558464000,\"createTime\":1643008836000},{\"channelName\":\"厂区后北围墙2\",\"channelCode\":\"1003236$1$0$10\",\"unitType\":1,\"deviceCode\":\"1003236\",\"deviceName\":\"192.168.1.5\",\"ownerCode\":\"001\",\"orgName\":\"根节点\",\"isOnline\":1,\"onlineUpdateVersion\":481,\"updateTime\":1653003005000,\"createTime\":1643008836000},{\"channelName\":\"大门西南围墙1\",\"channelCode\":\"1003236$1$0$1\",\"unitType\":1,\"deviceCode\":\"1003236\",\"deviceName\":\"192.168.1.5\",\"ownerCode\":\"001\",\"orgName\":\"根节点\",\"isOnline\":1,\"onlineUpdateVersion\":678,\"updateTime\":1656558460000,\"createTime\":1643008836000},{\"channelName\":\"大门西南围墙2\",\"channelCode\":\"1003236$1$0$0\",\"unitType\":1,\"deviceCode\":\"1003236\",\"deviceName\":\"192.168.1.5\",\"ownerCode\":\"001\",\"orgName\":\"根节点\",\"isOnline\":1,\"onlineUpdateVersion\":677,\"updateTime\":1656558460000,\"createTime\":1643008836000}],\"currentPage\":1,\"totalPage\":1,\"pageSize\":100,\"totalRows\":42},\"code\":\"0\",\"errMsg\":\"\"}";
//        JSONObject jsonObject = JSONObject.fromObject(data);
//        System.out.println(jsonObject);
//        Object data1 = jsonObject.get("data");
//        List<Map> list =(List<Map>) JSONObject.fromObject(data1).get("pageData");
//        for (Map map : list) {
//            String channelName = (String)map.get("channelName");
////            String replace = channelName.replace("楼", "层");
//
//            Camera farmId = iCameraService.getOne(new QueryWrapper<Camera>().eq("camera_name", channelName).eq("farm_id", 1));
//            if (ObjectUtil.isNotEmpty(farmId)){
//                farmId.setIccChannel((String)map.get("channelCode"));
//                iCameraService.updateById(farmId);
//            }
//        }
//    }


}
