package com.zehong.web.controller.system;

import com.alibaba.fastjson.JSONObject;
import com.hikvision.artemis.sdk.ArtemisHttpUtil;
import com.hikvision.artemis.sdk.config.ArtemisConfig;
import com.zehong.common.core.domain.AjaxResult;
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 java.util.HashMap;
import java.util.Map;

/**
 * 海康视频接口
 */
@RestController
@RequestMapping("artemis")
public class ArtemisController {

    /**
     * 请根据自己的appKey和appSecret更换static静态块中的三个参数. [1 host]
     * 如果你选择的是和现场环境对接,host要修改为现场环境的ip,https端口默认为443,http端口默认为80.例如10.33.25.22:443 或者10.33.25.22:80
     * appKey和appSecret请按照或得到的appKey和appSecret更改.
     */
    static {
        // 代理API网关nginx服务器ip端口
        ArtemisConfig.host = "27.128.189.137:1443";
        // 秘钥appkey
        ArtemisConfig.appKey = "28616162";
        // 秘钥appSecret
        ArtemisConfig.appSecret = "5ueTWDOJ21jRbpHACAzF";
    }
    /**
     * 能力开放平台的网站路径
     */
    private static final String ARTEMIS_PATH = "/artemis";

    /**
     * 获取监控点预览取流URL
     */
    private static final String GET_PREVIEWURLS = ARTEMIS_PATH + "/api/video/v1/cameras/previewURLs";
    /**
     * 获取对讲url
     */
    private static final String GET_TALKURLS = ARTEMIS_PATH + "/api/video/v1/cameras/talkURLs";

    /**
     * 分页获取监控点资源
     */
    private static final String GET_CAMERAS = ARTEMIS_PATH + "/api/resource/v1/cameras";
    //搜索
    private static final String GET_CAMERAS_SEARCH = ARTEMIS_PATH + "/api/resource/v2/camera/search";

    private static final String VEDIO_CONTROLLING = ARTEMIS_PATH + "/api/video/v1/ptzs/controlling";

    /**
     * 获取监控点预览取流URL
     * @return
     */
    @GetMapping("/getPreviewURLs")
    public Object getPreviewURLs(@RequestParam(value = "cameraIndexCode") String cameraIndexCode){
        /**
         * 根据API文档可以看出来,这是一个POST请求的Rest接口,而且传入的参数值为一个json
         * ArtemisHttpUtil工具类提供了doPostStringArtemis这个函数,一共六个参数在文档里写明其中的意思,因为接口是https,
         * 所以第一个参数path是一个hashmap类型,请put一个key-value,query为传入的参数,body为传入的json数据
         * 传入的contentType为application/json,accept不指定为null
         * header没有额外参数可不传,指定为null
         *
         */

        Map<String, String> path = new HashMap<String, String>(2) {
            {
                //根据现场环境部署确认是http还是https
                put("https://", GET_PREVIEWURLS);
            }
        };

        JSONObject jsonBody = new JSONObject();

        //jsonBody.put("cameraIndexCode", "2a9891a194c24747b277f3ea4836d433");
        jsonBody.put("cameraIndexCode", cameraIndexCode);
        jsonBody.put("streamType", 1);
        jsonBody.put("protocol", "ws");
        jsonBody.put("transmode", 1);

        String body = jsonBody.toJSONString();
        // post请求application/json类型参数
        String result = ArtemisHttpUtil.doPostStringArtemis(path,body,null,null,"application/json",null);
        return AjaxResult.success(JSONObject.parseObject(result));
    }

    /**
     * 分页获取监控点资源
     * @param pageNo    页码
     * @param pageSize  每页条数
     * @return
     */
    @GetMapping("/getCameras")
    public AjaxResult getCameras(@RequestParam("pageNum") int pageNo ,@RequestParam("pageSize") int pageSize){

        Map<String, String> path = new HashMap<String, String>(2) {
            {
                //根据现场环境部署确认是http还是https
                put("https://", GET_CAMERAS);
            }
        };

        JSONObject jsonBody = new JSONObject();

        jsonBody.put("pageNo", pageNo);
        jsonBody.put("pageSize", pageSize);
        jsonBody.put("treeCode", 0);
        String body = jsonBody.toJSONString();
        // post请求application/json类型参数
        String result = ArtemisHttpUtil.doPostStringArtemis(path,body,null,null,"application/json",null);
        //return JSONObject.parseObject(result);
        return AjaxResult.success(JSONObject.parseObject(result));
    }
    /**
     * 分页获取监控点资源
     * @param pageNo    页码
     * @param pageSize  每页条数
     * @return
     */
    @GetMapping("/search")
    public AjaxResult getCamerasSearch(@RequestParam("pageNum") int pageNo ,@RequestParam("pageSize") int pageSize,@RequestParam("name") String name){

        Map<String, String> path = new HashMap<String, String>(2) {
            {
                //根据现场环境部署确认是http还是https
                put("https://", GET_CAMERAS_SEARCH);
            }
        };

        JSONObject jsonBody = new JSONObject();

        jsonBody.put("pageNo", pageNo);
        jsonBody.put("pageSize", pageSize);
        jsonBody.put("name", name);
        jsonBody.put("treeCode", 0);
        String body = jsonBody.toJSONString();
        // post请求application/json类型参数
        String result = ArtemisHttpUtil.doPostStringArtemis(path,body,null,null,"application/json",null);
        //return JSONObject.parseObject(result);
        return AjaxResult.success(JSONObject.parseObject(result));
    }
    /**
     * 云控操作
     * @param cameraIndexCode
     * @param command
     * @param action
     * @return
     */
    @RequestMapping("/videoControlling")
    public Object videoControlling(@RequestParam("cameraIndexCode") String cameraIndexCode, @RequestParam("command") String command, @RequestParam("action") String action){
        Map<String, String> path = new HashMap<String, String>(2) {
            {
                //根据现场环境部署确认是http还是https
                put("https://", VEDIO_CONTROLLING);
            }
        };
        JSONObject jsonBody = new JSONObject();

        jsonBody.put("cameraIndexCode", cameraIndexCode);
        jsonBody.put("action", action);
        jsonBody.put("command", command);
        jsonBody.put("speed", 40);
        String body = jsonBody.toJSONString();
        // post请求application/json类型参数
        String result = ArtemisHttpUtil.doPostStringArtemis(path,body,null,null,"application/json",null);
        return JSONObject.parseObject(result);
    }

    /**
     * 获取通话链接
     * @param cameraIndexCode
     * @return
     */
    @GetMapping("/getTalkURLs")
    public Object getTalkURLs(@RequestParam(value = "cameraIndexCode") String cameraIndexCode,
                              @RequestParam(value = "expand",required=false) String expand,
                              @RequestParam(value = "streamType",required=false) Integer streamType){

        Map<String, String> path = new HashMap<String, String>(2) {
            {
                //根据现场环境部署确认是http还是https
                put("https://", GET_TALKURLS);
            }
        };

        JSONObject jsonBody = new JSONObject();
        if(expand==null){
            expand = "streamform=ps";
        }
        if(streamType==null){
            streamType = 1;
        }
        //jsonBody.put("cameraIndexCode", "2a9891a194c24747b277f3ea4836d433");
        jsonBody.put("cameraIndexCode", cameraIndexCode);
        jsonBody.put("streamType", streamType);
        jsonBody.put("expand", expand);
        jsonBody.put("eurlExpand", "url");
        String body = jsonBody.toJSONString();
        // post请求application/json类型参数
        String result = ArtemisHttpUtil.doPostStringArtemis(path,body,null,null,"application/json",null);
        return AjaxResult.success(JSONObject.parseObject(result));
    }
}