package com.sifang.modules.esign.cn.tsign.hz;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.sifang.modules.config.EsignConfig;
import com.sifang.modules.esign.cn.tsign.hz.comm.EsignHttpHelper;
import com.sifang.modules.esign.cn.tsign.hz.comm.EsignHttpResponse;
import com.sifang.modules.esign.cn.tsign.hz.enums.EsignRequestType;
import com.sifang.modules.esign.cn.tsign.hz.exception.EsignDemoException;
import com.sifang.modules.esign.query.EnterpriseSignature;


import java.util.Map;

public class DomeUtils {

    private static String eSignHost= EsignConfig.EsignHost;
    private static String eSignAppId=EsignConfig.EsignAppId;
    private static String eSignAppSecret=EsignConfig.EsignAppSecret;




    public static void main(String[] args) throws EsignDemoException {
        Gson gson = new Gson();

        EnterpriseSignature n = new EnterpriseSignature();
        n.setThirdPartyUserId("91310000MA1FPFRF44");
        n.setIdType("CRED_ORG_USCC");
        n.setName("极易净水科技(上海)有限公司");
        n.setIdNumber("91310000MA1FPFRF44");
        n.setOrgLegalIdNumber("340825199201060932");
        n.setOrgLegalName("李宗平");
        String jsonParm = gson.toJson(n);
        System.out.println(jsonParm);
/*        String jsonParm="{\n" +
                "    \"orgAuthConfig\": {\n" +
                "        \"orgName\": \"极易净水科技(上海)有限公司\"\n" +
                "    },\n" +
                "    \"repeatableRealName\": true,\n" +
                "    \"clientType\": \"all\",\n" +
                "    \"notifyUrl\":\"https://www.xxx.cn/notify\",\n" +
                "    \"redirectConfig\": {\n" +
                "        \"redirectUrl\": \"https://www.esign.cn\"\n" +
                "    },\n" +
                "    \"authorizeConfig\":{\n" +
                "        \"authorizedScopes\":[\"get_org_identity_info\",\"get_psn_identity_info\",\"org_initiate_sign\",\"psn_initiate_sign\",\"manage_org_resource\",\"manage_psn_resource\",\"use_org_order\"]\n" +
                "    }\n" +
                "}";*/

        /* 获取企业认证授权链接*/
        EsignHttpResponse getOrgAuthUrl = createUser(jsonParm);
        JsonObject getOrgAuthUrlObject = gson.fromJson(getOrgAuthUrl.getBody(), JsonObject.class);

      //  String orgAuthUrl =	getOrgAuthUrlObject.getAsJsonObject("data").get("orgId").getAsString();
        System.err.println("企业授权链接:"+getOrgAuthUrlObject);
        //




    }

    public static EsignHttpResponse createUser(String jsonParm) throws EsignDemoException {
        String apiaddr="/v1/organizations/createByThirdPartyUserId";
        //请求参数body体,json格式。get或者delete请求时jsonString传空json:"{}"或者null
        //请求方法
        EsignRequestType requestType= EsignRequestType.POST;
        //生成签名鉴权方式的的header
        Map<String, String> header = EsignHttpHelper.signAndBuildSignAndJsonHeader(eSignAppId,eSignAppSecret,jsonParm,requestType.name(),apiaddr,true);
        //发起接口请求
        return EsignHttpHelper.doCommHttp(eSignHost, apiaddr,requestType , jsonParm, header,true);
    }

    public static EsignHttpResponse getOrgAuthUrl( String jsonParm) throws EsignDemoException {
        String apiaddr="/v3/org-auth-url";
        //请求参数body体,json格式。get或者delete请求时jsonString传空json:"{}"或者null
        //请求方法
        EsignRequestType requestType= EsignRequestType.POST;
        //生成签名鉴权方式的的header
        Map<String, String> header = EsignHttpHelper.signAndBuildSignAndJsonHeader(eSignAppId,eSignAppSecret,jsonParm,requestType.name(),apiaddr,true);
        //发起接口请求
        return EsignHttpHelper.doCommHttp(eSignHost, apiaddr,requestType , jsonParm, header,true);
    }


}
