package com.zhgd.xmgl.modules.basicdata.api.camera;

import com.alibaba.fastjson.JSONObject;
import com.hikvision.artemis.sdk.ArtemisHttpUtil;
import com.hikvision.artemis.sdk.config.ArtemisConfig;
import lombok.experimental.UtilityClass;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;

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

/**
 * 视频监控外部接口
 */
@Slf4j
@UtilityClass
public class CameraPreview {

    // artemis网关服务器ip端口
    @Value("${artemisConfig.host}")
    private String host;

    // 秘钥appkey
    @Value("${artemisConfig.appKey}")
    private String appKey;

    // 秘钥appSecret
    @Value("${artemisConfig.appSecret}")
    private String appSecret;

    /**
     * 调用
     * @param requestUrl    请求路径
     * @param body          请求参数体
     * @return
     */
    public JSONObject GetCameraPreviewURL(String requestUrl, String body) {
        /**
         * STEP1：设置平台参数，根据实际情况,设置host appkey appsecret 三个参数.
         */
        ArtemisConfig.host = host;
        ArtemisConfig.appKey = appKey;
        ArtemisConfig.appSecret = appSecret;
//        ArtemisConfig.host = "182.101.141.23:18443";
//        ArtemisConfig.appKey = "24017757";
//        ArtemisConfig.appSecret = "VJz0FbzmE6drPQ7egsBi";

        /**
         * STEP2：设置OpenAPI接口的上下文
         */
        final String ARTEMIS_PATH = "/artemis";

        /**
         * STEP3：设置接口的URI地址
         */
        final String previewURLsApi = ARTEMIS_PATH + requestUrl;
        Map<String, String> path = new HashMap<String, String>(2) {
            {
                put("https://", previewURLsApi);//根据现场环境部署确认是http还是https
            }
        };

        /**
         * STEP4：设置参数提交方式
         */
        String contentType = "application/json";

        /**
         * STEP5：组装请求参数
         */
//        JSONObject jsonBody = new JSONObject();
//        String [] array = new String[]{"00b66706ed894851aa77aa727133182a"};
//        jsonBody.put("indexCodes", array);
//        String body = jsonBody.toJSONString();

        /**
         * STEP6：调用接口
         */
        String result = ArtemisHttpUtil.doPostStringArtemis(path, body, null, null,
                contentType, null);// post请求application/json类型参数
        JSONObject jsonResult = JSONObject.parseObject(result);
        String code = jsonResult.getString("code");
        if (!code.equals("0")) {
            log.error("调用海康开发平台接口出现错误,请求接口：" + requestUrl + "请求参数 ：" + body + "返回错误码：" + code);
            jsonResult = null;
        }
        return jsonResult;
    }

    public static void main(String[] args) {
        String path = "/api/video/v1/manualCapture";
        JSONObject jsonBody = new JSONObject();
        jsonBody.put("cameraIndexCode", "ac57fd12eca64fd3b6ce756e158613e7");
        JSONObject result = CameraPreview.GetCameraPreviewURL(path, jsonBody.toJSONString());
        System.out.println("result结果示例: " + result);
    }
}
