package com.huimv.video.dhicc.test.request.Video;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.huimv.video.dhicc.test.config.OauthConfigBaseInfo;
import com.huimv.video.dhicc.test.config.OauthConfigBaseInfo02;
import com.huimv.video.dhicc.test.constant.Constant;
import com.huimv.video.dhicc.test.request.BaseRequest02;
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 VideoRecordRequestHn extends BaseRequest02<Object> {

    private String clientMac;

    private String clientPushId;

    private String project;

    private String method;

    private Map<String,Object> data;


    public VideoRecordRequestHn() {
    }

    public VideoRecordRequestHn(OauthConfigBaseInfo02 oauthConfigBaseInfo) {
        super(oauthConfigBaseInfo, Constant.HTTP_POST, oauthConfigBaseInfo.getHttpConfigInfo().getPrefixUrl() + Constant.CHANNEL_URL_VIDEO_RECORD, 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);
        Object data = jsonObject.get("data");
        // PersonListResponseSg devicePageResponse = JSON.parseObject(res, PersonListResponseSg.class);

        return data;
    }

    private void setBody() {
        //设置请求参数
        Map<String, Object> map = new HashMap<>();
        map.put("pageNum", this.clientMac);
        map.put("pageSize", this.clientPushId);
        map.put("isContain", this.project);
        map.put("searchKey", this.method);
        map.put("data", this.data);
        this.setBody(JSON.toJSONString(map));

    }


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