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

import com.alibaba.fastjson.JSON;
import com.dahuatech.icc.util.SignUtil;
import com.huimv.video.dhicc.test.config.OauthConfigBaseInfo;
import com.huimv.video.dhicc.test.config.OauthConfigUserPwdInfoTest01;
import com.huimv.video.dhicc.test.constant.Constant;
import com.huimv.video.dhicc.test.response.GetPublicKeyResponse;
import com.huimv.video.dhicc.test.response.GetTokenResponse;
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;

/**
 * 获取token请求类
 */
@Data
public class GetTokenRequestTest01 extends BaseRequest<GetTokenResponse> {
    //授权类型
    private String grant_type;
    //用户名
    private String username;
    //通过RSA加密密码,详见获取公钥接口
    private String password;
    //客户端id，由鉴权中心下发
    private String client_id;
    //客户端secret，由鉴权中心下发
    private String client_secret;
    //公钥，由鉴权中心下发
    private String public_key;
    //默认关闭验证码校验，即无需关注verifyCodeFlag和code参数
    private Integer verifyCodeFlag = 0;
    private String code;

    public GetTokenRequestTest01(){}

    public GetTokenRequestTest01(OauthConfigBaseInfo oauthConfigBaseInfo){

        super(oauthConfigBaseInfo, Constant.HTTP_POST, oauthConfigBaseInfo.getHttpConfigInfo().getPrefixUrl() + Constant.GET_TOKEN, false);
        OauthConfigUserPwdInfoTest01 oauthConfigUserPwdInfo = (OauthConfigUserPwdInfoTest01) oauthConfigBaseInfo;
        this.grant_type = oauthConfigUserPwdInfo.getGrantType().name();
        this.username = oauthConfigUserPwdInfo.getUsername();
        this.client_id = oauthConfigUserPwdInfo.getClientId();
        this.client_secret = oauthConfigUserPwdInfo.getClientSecret();
        GetPublicKeyRequest request = new GetPublicKeyRequest(oauthConfigBaseInfo);
        GetPublicKeyResponse getPublicKeyResponse = request.doAction();
        this.public_key = getPublicKeyResponse.getData().getPublicKey();
        String enPassword = SignUtil.encryptRSA(oauthConfigUserPwdInfo.getPassword(), this.public_key);
        this.password = enPassword;
    }

    public GetTokenResponse 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());
        //获取响应
        GetTokenResponse getTokenResponse = JSON.parseObject(res, GetTokenResponse.class);
        return getTokenResponse;
    }

    /**
     * 设置请求参数
     * **/
    public void setBody(){
        //设置请求参数
        Map<String, String> paramsMap = new HashMap();
        paramsMap.put("grant_type", this.grant_type);
        paramsMap.put("username", this.username);
        paramsMap.put("password", this.password);
        paramsMap.put("client_id", this.client_id);
        paramsMap.put("client_secret", this.client_secret);
        paramsMap.put("public_key", this.public_key);
        paramsMap.put("verifyCodeFlag", String.valueOf(this.verifyCodeFlag));
        this.setBody(JSON.toJSONString(paramsMap));
    }

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