package com.zcloud.util.hk;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hikvision.artemis.sdk.ArtemisHttpUtil;
import com.hikvision.artemis.sdk.config.ArtemisConfig;

import java.util.HashMap;
import java.util.Map;

/**
 * 海康工具类
 */
public class HKUtil {
    static {
        // 代理API网关nginx服务器ip端口
        ArtemisConfig.host = "192.168.0.253:443";
        // 秘钥appkey
        ArtemisConfig.appKey = "27510476";
        // 秘钥appSecret
        ArtemisConfig.appSecret = "vyuVh14pAnD0oMuJdcc0";
    }
    /**
     * 能力开放平台的网站路径
     * TODO 路径不用修改，就是/artemis
     */
    private static final String ARTEMIS_PATH = "/artemis";
    /**
     * 通用海康接口
     * 调用POST请求类型(application/json)接口*
     * @return
     */
    public static Map<String,Object> publicHkInterface(JSONObject jsonBody,String url){
        final String getCamsApi = ARTEMIS_PATH +url;
        Map<String, String> path = new HashMap<String, String>(2);
        path.put("https://", getCamsApi);
        // post请求application/json类型参数
        String result =ArtemisHttpUtil.doPostStringArtemis(path,jsonBody.toJSONString(),null,null,"application/json",null);
        JSONObject parseObject = JSONArray.parseObject(result);
        return parseObject;
    }


    /**
     * 获取监控点预览取流URL
     * @param id 设备编号
     * @return
     */
    public static Map<String,Object> camerasPreviewURLs(String id,String type){
        JSONObject jsonBody = new JSONObject();
        jsonBody.put("cameraIndexCode", id);
        jsonBody.put("netZoneCode", "1");
        jsonBody.put("transmode", 1);
        jsonBody.put("streamType", 0);
        jsonBody.put("protocol",type);
        jsonBody.put("expireTime", -1);
        jsonBody.put("expand","transcode=1&streamform=rtp");
        Map<String,Object> returnMap=publicHkInterface(jsonBody,"/api/video/v1/cameras/previewURLs");
        return returnMap;
    }

    /**
     * API名称：
     * 查询监控点列表v2
     * 分组：
     * 视频资源接口
     * 提供方名称：
     * 资源目录服务
     * qps：
     * 描述：根据条件查询目录下有权限的监控点列表
     * @return
     */
    public static Map<String,Object> cameraSearch(Map<String,String> map){
        JSONObject jsonBody = new JSONObject();
        jsonBody.put("pageNo", map.get("pageNo"));
        jsonBody.put("pageSize", map.get("pageSize"));
        if(map.get("name")!=null && !"".equals(map.get("name")))
            jsonBody.put("name", map.get("name"));
        jsonBody.put("orderBy", "name");
        jsonBody.put("orderType", "desc");
        Map<String,Object> returnMap=publicHkInterface(jsonBody,"/api/resource/v2/camera/search");
        return returnMap;
    }
}
