package com.huimv.video.dhicc.test.request.event.Sg;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.huimv.video.dhicc.test.config.OauthConfigBaseInfo;
import com.huimv.video.dhicc.test.constant.Constant;
import com.huimv.video.dhicc.test.request.BaseRequest;
import com.huimv.video.dhicc.test.utils.HttpURLConnectionUtil;
import com.huimv.video.dhicc.test.utils.HttpsURLConnectionUtil;
import com.huimv.video.dhicc.test.utils.UrlUtil;
import lombok.Data;

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

@Data
public class ClientAllEventRequestSg extends BaseRequest<Object> {



    private Integer alarmType;
    private Integer dbType;
    private Integer deviceCategory;
    private String[] nodeCodeList;
    private String alarmStartDateString;
    private String alarmEndDateString;







    public ClientAllEventRequestSg() {
    }

    public ClientAllEventRequestSg(OauthConfigBaseInfo oauthConfigBaseInfo) {
        super(oauthConfigBaseInfo, Constant.HTTP_POST, oauthConfigBaseInfo.getHttpConfigInfo().getPrefixUrl() + Constant.COUNT_TIME, true);
    }

    @Override
    public Object doAction() {
        //重置url信息，替换版本号
        this.setUrl(UrlUtil.dealVersion(this.getUrl()));
        //设置请求参数
        setBody();
        //发送请求
        String res = isEnableHttpTest() ? HttpURLConnectionUtil.doPost(this.getUrl(), this.getBody(), this.getHeader()) : HttpsURLConnectionUtil.doPost(this.getUrl(), this.getBody(), this.getHeader());

        System.out.println(res);
        //获取响应
        JSONObject jsonObject=JSONObject.parseObject(res);
        System.out.println(jsonObject);
        Object action = jsonObject.get("data");
        return action;
    }

    private void setBody() {
        //设置请求参数

        Map<String, Object> map = new HashMap<>();
        map.put("alarmType", this.alarmType);
        map.put("nodeCodeList", this.nodeCodeList);
        map.put("dbType", this.dbType);
        map.put("alarmStartDateString", this.alarmStartDateString);
        map.put("alarmEndDateString", this.alarmEndDateString);
        map.put("deviceCategory", this.deviceCategory);
        this.setBody(JSON.toJSONString(map));

    }


    public boolean isEnableHttpTest() {
        return super.getOauthConfigBaseInfo().getHttpConfigInfo().isEnableHttpTest();
    }
}
