package com.seeyon.apps.esign.service;

import com.seeyon.aicloud.common.JsonUtils;
import com.seeyon.apps.esign.config.EsignConfigProvider;
import com.seeyon.apps.esign.constants.EsignApiUrl;
import com.seeyon.apps.esign.constants.EsignConfigConstants;
import com.seeyon.apps.esign.po.esignapi.EsignApiHeader;
import com.seeyon.apps.esign.po.esignapi.EsignBaseResp;
import com.seeyon.apps.esign.utils.HttpClient;
import com.seeyon.ctp.common.AppContext;

import java.util.Map;


public class EsignAuthService {

    private EsignConfigProvider configProvider = (EsignConfigProvider) AppContext.getBean("esignConfigProvider");
    private TokenCacheManager tokenCacheManager = (TokenCacheManager) AppContext.getBean("tokenCacheManager");

    public void personAuth(String appId,Map<String,Object> authParams) throws Exception {
        EsignApiHeader esignApiHeader = EsignApiHeader.build();
        String jsonBody = JsonUtils.toJSONString(authParams);
        esignApiHeader.appId(appId)
                .httpMethod("POST")
                .heads("")
                .pathAndParameters(EsignApiUrl.PERSON_AUTH_URL)
                .contentMD5(jsonBody)
                .signature(tokenCacheManager.getSecretByAppId(appId));
        String url = configProvider.getBizConfigByKey(EsignConfigConstants.ESIGN_HOST) + EsignApiUrl.PERSON_AUTH_URL;
        String respStr = HttpClient.httpPostRaw(url, jsonBody, esignApiHeader.convert2Headers(), "UTF-8");
        EsignBaseResp esignBaseResp = JsonUtils.parseObject(respStr, EsignBaseResp.class);
        Map<String,Object> dataMap = (Map<String, Object>) esignBaseResp.getData();
    }
}
