package com.huiju.estateagents.eContract.service.impl;

import com.fadada.sdk.client.FddClientBase;
import com.fadada.sdk.util.crypt.FddEncryptTool;
import com.fadada.sdk.util.http.HttpsUtil;
import com.huiju.estateagents.common.StringUtils;
import com.huiju.estateagents.config.FadadaProperties;
import com.huiju.estateagents.entity.TaMiniapp;
import com.huiju.estateagents.mapper.TaMiniappMapper;
import org.apache.http.message.BasicNameValuePair;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.File;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

@Component
public class TaCompanySealUtil {

    @Autowired
    private FadadaProperties fadadaProperties;

    @Autowired
    private TaMiniappMapper taMiniappMapper;

    public String addSignaTure(String customerId, String imgUrl) throws Exception {
        FddClientBase base = new FddClientBase(fadadaProperties.getAppId(), fadadaProperties.getAppSecret(), fadadaProperties.getVersion(), fadadaProperties.getServeHost());
        String base64Url = FddEncryptTool.ImageToBase64ByOnline(imgUrl);
        return base.invokeaddSignature(customerId, base64Url);
    }

    public String replaceSignature(String customerId, String signatureId, String imgUrl){
        String base64Url = FddEncryptTool.ImageToBase64ByOnline(imgUrl);
        ArrayList params = new ArrayList();

        try {
            String timeStamp = HttpsUtil.getTimeStamp();
            String sha1 = FddEncryptTool.sha1(fadadaProperties.getAppId() + FddEncryptTool.md5Digest(timeStamp) + FddEncryptTool.sha1(fadadaProperties.getAppSecret() + customerId + signatureId + base64Url));
            String msgDigest = new String(FddEncryptTool.Base64Encode(sha1.getBytes()));
            params.add(new BasicNameValuePair("customer_id", customerId));
            params.add(new BasicNameValuePair("signature_img_base64", base64Url));
            params.add(new BasicNameValuePair("app_id", fadadaProperties.getAppId()));
            params.add(new BasicNameValuePair("signature_id", signatureId));
            params.add(new BasicNameValuePair("timestamp", timeStamp));
            params.add(new BasicNameValuePair("v", fadadaProperties.getVersion()));
            params.add(new BasicNameValuePair("msg_digest", msgDigest));
        } catch (Exception var7) {
            var7.printStackTrace();
            throw new RuntimeException(var7);
        }

        return HttpsUtil.doPost(fadadaProperties.getServeHost() + "replace_signature.api", params);
    }

    public String delSignature(String customerId, String signatureId){
        ArrayList params = new ArrayList();

        try {
            String timeStamp = HttpsUtil.getTimeStamp();
            String sha1 = FddEncryptTool.sha1(fadadaProperties.getAppId() + FddEncryptTool.md5Digest(timeStamp) + FddEncryptTool.sha1(fadadaProperties.getAppSecret() + customerId + signatureId));
            String msgDigest = new String(FddEncryptTool.Base64Encode(sha1.getBytes()));
            params.add(new BasicNameValuePair("customer_id", customerId));
            params.add(new BasicNameValuePair("app_id", fadadaProperties.getAppId()));
            params.add(new BasicNameValuePair("timestamp", timeStamp));
            params.add(new BasicNameValuePair("v", fadadaProperties.getVersion()));
            params.add(new BasicNameValuePair("signature_id", signatureId));
            params.add(new BasicNameValuePair("msg_digest", msgDigest));
        } catch (Exception var7) {
            var7.printStackTrace();
            throw new RuntimeException(var7);
        }

        return HttpsUtil.doPost(fadadaProperties.getServeHost() + "remove_signature.api", params);
    }

    public String autoAuthCompanySeal(String transactionId, Integer authType, String contractId, String customerId, String companyId) throws Exception {
        String timeStamp = HttpsUtil.getTimeStamp();
        String sha1 = FddEncryptTool.sha1(fadadaProperties.getAppId() + FddEncryptTool.md5Digest(transactionId + timeStamp) + FddEncryptTool.sha1(fadadaProperties.getAppSecret() + customerId));
        String msgDigest = new String(FddEncryptTool.Base64Encode(sha1.getBytes()));
        StringBuilder stringBuilder = new StringBuilder(fadadaProperties.getServeHost() + "before_authsign.api?");
        stringBuilder.append("app_id="+fadadaProperties.getAppId()+"&");
        stringBuilder.append("customer_id="+customerId+"&");
        stringBuilder.append("timestamp="+timeStamp+"&");
        stringBuilder.append("v="+fadadaProperties.getVersion()+"&");
        stringBuilder.append("transaction_id="+transactionId+"&");
        stringBuilder.append("auth_type="+authType+"&");
        stringBuilder.append("contract_id="+contractId+"&");
        stringBuilder.append("msg_digest="+msgDigest+"&");
        stringBuilder.append("notify_url="+fadadaProperties.getAuthNotifyUrl()+"&");
        stringBuilder.append("return_url="+ URLEncoder.encode(fadadaProperties.getAuthReturnUrl(), "UTF-8") + companyId);

        return stringBuilder.toString();
    }

    public String getAuthStatus(String customerId){
        ArrayList params = new ArrayList();

        try {
            String timeStamp = HttpsUtil.getTimeStamp();
            String sha1 = FddEncryptTool.sha1(fadadaProperties.getAppId() + FddEncryptTool.md5Digest(timeStamp) + FddEncryptTool.sha1(fadadaProperties.getAppSecret() + customerId));
            String msgDigest = new String(FddEncryptTool.Base64Encode(sha1.getBytes()));
            params.add(new BasicNameValuePair("customer_id", customerId));
            params.add(new BasicNameValuePair("app_id", fadadaProperties.getAppId()));
            params.add(new BasicNameValuePair("timestamp", timeStamp));
            params.add(new BasicNameValuePair("v", fadadaProperties.getVersion()));
            params.add(new BasicNameValuePair("msg_digest", msgDigest));
        } catch (Exception var7) {
            var7.printStackTrace();
            throw new RuntimeException(var7);
        }

        return HttpsUtil.doPost(fadadaProperties.getAuthServeHost() + "get_auth_status.api", params);
    }

    public String setDefaultSeal(String signatureId, String customerId){
        ArrayList params = new ArrayList();

        try {
            String timeStamp = HttpsUtil.getTimeStamp();
            String sha1 = FddEncryptTool.sha1(fadadaProperties.getAppId() + FddEncryptTool.md5Digest(timeStamp) + FddEncryptTool.sha1(fadadaProperties.getAppSecret() + customerId + signatureId));
            String msgDigest = new String(FddEncryptTool.Base64Encode(sha1.getBytes()));
            params.add(new BasicNameValuePair("customer_id", customerId));
            params.add(new BasicNameValuePair("app_id", fadadaProperties.getAppId()));
            params.add(new BasicNameValuePair("timestamp", timeStamp));
            params.add(new BasicNameValuePair("v", fadadaProperties.getVersion()));
            params.add(new BasicNameValuePair("msg_digest", msgDigest));
            params.add(new BasicNameValuePair("signature_id", signatureId));
        } catch (Exception var7) {
            var7.printStackTrace();
            throw new RuntimeException(var7);
        }

        return HttpsUtil.doPost(fadadaProperties.getAuthServeHost() + "default_signature.api", params);
    }

    public String sendFddSms(String sourceUrl, String mobile){
        ArrayList params = new ArrayList();

        try {
            String timeStamp = HttpsUtil.getTimeStamp();
            String messageContent = "[${platform}]因小程序不支持人脸识别认证，请点击下面的链接开始人脸识别认证，认证通过后请回到小程序重新开始签署合同！${shortUrl}";
            String sha1 = FddEncryptTool.sha1(fadadaProperties.getAppId() + FddEncryptTool.md5Digest(timeStamp) + FddEncryptTool.sha1(fadadaProperties.getAppSecret() + "10080" + messageContent + "2" + mobile + "2" + sourceUrl));
            String msgDigest = new String(FddEncryptTool.Base64Encode(sha1.getBytes()));
            params.add(new BasicNameValuePair("source_url", sourceUrl));
            params.add(new BasicNameValuePair("expire_time", "10080"));
            params.add(new BasicNameValuePair("mobile", mobile));
            params.add(new BasicNameValuePair("message_type", "2"));
            params.add(new BasicNameValuePair("message_content", messageContent));
            params.add(new BasicNameValuePair("sms_template_type", "2"));
        } catch (Exception var7) {
            var7.printStackTrace();
            throw new RuntimeException(var7);
        }

        return HttpsUtil.doPost(fadadaProperties.getAuthServeHost() + "push_short_url_sms.api", params);
    }
}
