package com.weaver.seconddev.chapanda.esign.esb;

import com.weaver.common.base.entity.result.WeaResult;
import com.weaver.esb.api.rpc.EsbServerlessRpcRemoteInterface;
import com.weaver.seconddev.chapanda.esign.constant.Constants;
import com.weaver.seconddev.chapanda.esign.util.Esb2EsignUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.Map;

@Service("Esb2EsignQuerySealsAction")
public class Esb2EsignQuerySealsAction implements EsbServerlessRpcRemoteInterface {
    private final static Logger log = LoggerFactory.getLogger(Esb2EsignQuerySealsAction.class);

    @Autowired
    Esb2EsignUtil esb2EsignUtil;
    @Override
    public WeaResult<Map<String, Object>> execute(Map<String, Object> params) {
        log.error("Esb2EsignQuerySealsAction start");

        String pageIndex = String.valueOf(params.get("pageIndex"));
        String pageSize = String.valueOf(params.get("pageSize"));
        String organizeNo = String.valueOf(params.get("organizeNo"));
        // pageIndex=1&pageSize=10&organizeNo=91510115MA62QJQJ4N
        log.error("pageIndex:"+pageIndex);
        log.error("pageSize:"+pageSize);
        log.error("organizeNo:"+organizeNo);

        String querySealsUrl = Constants.querySealsUrl+"?pageIndex="+pageIndex+"&pageSize="+pageSize+"&organizeNo="+organizeNo;
        log.error("querySealsUrl:{}",querySealsUrl);

        String msg = queryEsignSealTemplate(querySealsUrl,organizeNo);
        Map<String, Object> actionMap = new HashMap<String, Object>();
        actionMap.put("code",200);
        actionMap.put("msg",msg);

        return WeaResult.success(actionMap);
    }

    public String queryEsignSealTemplate(String querySealsUrl,String organizeNo){

//        String secretKey = "7f067a08f5c675137e6e72aa298e2c07";
//        String xTimevaleProjectId = "1001003";
//        String eSignHost = "http://110.185.172.132:8086";

        String bodyData = "";
        String xTimevaleSignature = esb2EsignUtil.encryptionHmacSHA256(bodyData);
        log.error("xTimevaleSignature:"+xTimevaleSignature);
        String msg = esb2EsignUtil.doGetEsignQuerySeals(xTimevaleSignature,querySealsUrl);
        log.error("msg:"+msg);
        return msg;
    }


//    public String doHttpPost(String eSignHost,String eSignUrl,String bodyData,String xTimevaleSignature,String xTimevaleProjectId){
//        String msg = "";
//        OkHttpClient client = new OkHttpClient().newBuilder()
//                .build();
//        MediaType mediaType = MediaType.parse("text/plain");
//        System.out.println(eSignHost+eSignUrl);
//        try {
//            Request request = new Request.Builder()
//                    .url(eSignHost+eSignUrl)
//                    .method("GET", null)
//                    .addHeader("x-timevale-project-id", xTimevaleProjectId)
//                    .addHeader("x-timevale-signature", xTimevaleSignature)
//                    .addHeader("Content-Type", "application/json")
//                    .build();
//            Response response = client.newCall(request).execute();
//            int code = response.code();
//            if(code == 200){
//                msg = response.body().string();
//            }
//            log.error("msg:"+msg);
//        } catch (IOException e) {
//            throw new RuntimeException(e);
//        }
//        return msg;
//    }

    public static void main(String[] args) {
        Esb2EsignQuerySealsAction esb2EsignQuerySealsAction = new Esb2EsignQuerySealsAction();
        String pageIndex = "1";
        String pageSize = "10";
        String organizeNo = "91510115MA62QJQJ4N";

        String querySealsUrl = Constants.querySealsUrl+"?pageIndex="+pageIndex+"&pageSize="+pageSize+"&organizeNo="+organizeNo;
        esb2EsignQuerySealsAction.queryEsignSealTemplate(querySealsUrl,organizeNo);
    }


//    public String encryptionHmacSHA256(String secretKey,String bodyData){
//        String encryptionData = "";
////        String secretKey = "7f067a08f5c675137e6e72aa298e2c07";
//        // 要签名的数据
////        JSONObject dataObject = new JSONObject();
////        dataObject.put("templateId","JMHT-ZXSJ");
////        String data = "{\"templateId\":\"JMHT-ZXSJ\"}" ;
//        try {
//            // 初始化Mac对象
//            Mac sha256Hmac = Mac.getInstance("HmacSHA256");
//            SecretKeySpec secretKeySpec = new SecretKeySpec(secretKey.getBytes(StandardCharsets.UTF_8), "HmacSHA256");
//            sha256Hmac.init(secretKeySpec);
//            byte[] result = sha256Hmac.doFinal(bodyData.getBytes(StandardCharsets.UTF_8));
//            encryptionData = Hex.encodeHexString(result);
//        } catch (NoSuchAlgorithmException e) {
//            throw new RuntimeException(e);
//        }catch (InvalidKeyException e) {
//            throw new RuntimeException(e);
//        }
//        return encryptionData;
//    }
}
