package com.sooka.sponest.monitor.dahua.utils;

import com.dahuatech.hutool.http.Method;
import com.dahuatech.hutool.json.JSONUtil;
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.http.IccHttpHttpRequest;
import com.dahuatech.icc.oauth.http.IccTokenResponse;
import com.dahuatech.icc.oauth.model.v202010.GeneralRequest;
import com.dahuatech.icc.oauth.model.v202010.GeneralResponse;
import com.dahuatech.icc.util.BeanUtil;
import com.sooka.sponest.monitor.dahua.domain.*;
import com.sooka.sponest.monitor.dahua.enums.EventCategoryEnums;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/******************************
 * TODO
 * @author yanhongliang
 * @date 2022-06-20 10:22 AM
 ******************************/

public class DSSUtils {


    private static org.slf4j.Logger log = LoggerFactory.getLogger(DSSUtils.class);
    private static String HOST = "https://192.168.100.2:8443";

    private static String CLIENT_ID = "sooka";
    private static String CLIENT_SECRET = "55834a3c-61e4-4c1d-9a93-3587ba310bc1";
    private static String VERSION = "1.0.0";
    //获取token
    private static String TOKEN_URL=HOST+"/evo-apigw/evo-oauth/"+VERSION+"/oauth/extend/token";
    //事件订阅
    private static String SUBSCRIE_URL=HOST+"/evo-apigw/evo-event/"+VERSION+"/subscribe/mqinfo";

    String receiveIp = "192.168.2.9";//第三方接收ip
    String receivePort = "10004";//第三方接收端口
    String monitor = "http://" + receiveIp + ":" + receivePort + "/URI";//第三方接收URL 要求post application/json格式r

    // access_token='ddc234ce-44b2-4502-9634-67c50cbfaf4d'

    /**
     * 申请鉴权token
     * */
    public static String getToken() {
        try {
            log.info("----开始执行----{}------请求地址:{}", "客户端模式_认证申请",TOKEN_URL);
            Map<String, Object> map = new HashMap();
            map.put("grant_type", "client_credentials");
            map.put("client_id", CLIENT_ID);
            map.put("client_secret", CLIENT_SECRET);
            IccHttpHttpRequest pr = new IccHttpHttpRequest(TOKEN_URL, Method.POST, JSONUtil.toJsonStr(map));
            String prBody = pr.execute();
            IccTokenResponse keyResp =  BeanUtil.toBean(prBody, IccTokenResponse.class);
            log.info("----结束执行----{}------返回报文:{}", "客户端模式_认证申请",keyResp);
            String token = keyResp.getData().getAccess_token();
            bean b = new bean();
            b.time = System.currentTimeMillis();
            b.token = token;
            map.put("token", b);
            return keyResp.getData().getAccess_token();
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 事件订阅示例
     *
     * @throws ClientException
     */
    public static void eventSubscribe() throws ClientException {
        log.info("----开始执行----{}------请求地址:{}", "事件订阅",SUBSCRIE_URL);
        // 接收事件地址
        String mqinfo ="{\"param\":{\"monitors\":[{\"monitor\":\"http://10.35.239.145:8922/evo-demo/1.0.0/api/common-push\",\"monitorType\":\"url\",\"events\":[{\"category\":\"alarm\",\"subscribeAll\":1,\"domainSubscribe\":2},{\"category\":\"business\",\"subscribeAll\":1,\"domainSubscribe\":2},{\"category\":\"state\",\"subscribeAll\":1,\"domainSubscribe\":2},{\"category\":\"perception\",\"subscribeAll\":1,\"domainSubscribe\":2}]}],\"subsystem\":{\"subsystemType\":0,\"name\":\"10.35.239.145_8922\",\"magic\":\"10.35.239.145_8922\"}}}";
        IccHttpHttpRequest pr = new IccHttpHttpRequest(SUBSCRIE_URL, Method.POST, mqinfo);
        pr.getHttpRequest().header("Authorization","bearer 2c7eefbe-3326-4c5d-8f8d-49bc4ad2bdf3");
        String prBody = pr.execute();
        log.info("----结束执行----{}------返回报文:{}", "事件订阅",prBody);
        // - ----结束执行----事件订阅------返回报文:{"success":true,"data":{},"code":"0","errMsg":""}
    }

    public static void main(String[] args) {
//            DSSUtils.getToken();
        try {
            DSSUtils.eventSubscribe();
        } catch (ClientException e) {
            throw new RuntimeException(e);
        }
    }

}

class bean {
    long time;
    String token;
}

