package com.trade.service.contract.util.esign;

import com.trade.common.utils.StringUtils;
import com.trade.service.contract.config.ESignConfigHolder;

import java.util.ArrayList;
import java.util.List;

/**
 * 电子合同_常用配置信息类
 */
public class ESignConfig {

    // 域名 - 测试环境
    public static String API_HOST;
    // 签署完成重定向地址 ,默认签署完成停在当前页面 - 测试环境
    public static String ESign_REDIRECT_URL;
    // 回调通知地址 ,默认取项目配置通知地址 - 测试环境
    public static String ESign_NOTICE_DEVELOPER_URL;
    /* V2 标准版 配置文件 */
    public static String PROJECT_ID_V2;// V2标准版
    public static String PROJECT_SECRET_V2;// V2标准版
    static {
        API_HOST = ESignConfigHolder.getApiHost();
        ESign_REDIRECT_URL = ESignConfigHolder.getRedirectUrl();
        ESign_NOTICE_DEVELOPER_URL = ESignConfigHolder.getCallbackUrl();
        PROJECT_ID_V2 = ESignConfigHolder.getProjectId();
        PROJECT_SECRET_V2 = ESignConfigHolder.getProjectSecret();
    }
    /** 编码格式 **/
    public static final String ENCODING = "UTF-8";
    /** 哈希算法 **/
    public static final String ALGORITHM = "HmacSHA256";
    // 印章id 需要在官网控制台授权
    // 深圳长银保
    // public static final String ESign_COMPANY_SZCYB_SEAL_ID = "40094168-133b-43bb-a8dd-a3724c1687e4";
    // 深圳长银科技
    // public static final String ESign_COMPANY_SZCYKJ_SEAL_ID = "c2077609-780c-4fed-a6f4-3f96d9cbd275";
    // 长安银科资产管理有限公司
    // public static final String ESign_COMPANY_CYZG_SEAL_ID = "7e53012c-cbce-4eea-82d8-c3480dd00bf2";

    // 获取Token
    public static String getToken_URL(String appId, String secret) {
        return API_HOST + "/v1/oauth2/access_token?appId=" + appId + "&secret=" + secret
                + "&grantType=client_credentials";
    }

    // 刷新Token
    public static String refreshToken_URL(String appId, String refreshToken) {
        return API_HOST + "/v1/oauth2/access_token?appId=" + appId + "&refreshToken=" + refreshToken
                + "&grantType=refresh_token";
    }

    // 根据身份证号获取“个人第三方平台的用户id”
    public static String getPersonThirdPartyUserId(String idNumber) {
        return ESignConstant.ESign_PREFIX + idNumber + "_CRED_PSN_CH_IDCARD";
    }

    // 根据身份证号获取“机构第三方平台的用户id”
    public static String getOrgThirdPartyUserId(String idNumber, String IdType) {
        /*
         * if ("HK".equals(country)) { return ESignConstant.ESign_PREFIX + idNumber + "_CRED_ORG_UNKNOWN"; }
         */
        return ESignConstant.ESign_PREFIX + idNumber + "_" + IdType;
    }
    // ---个人账号---

    // 创建个人账号
    public static String createPerAcc_URL() {
        return API_HOST + "/v1/accounts/createByThirdPartyUserId";
    }

    // 修改个人账号(根据账号ID)
    public static String modifyPerAccById_URL(String accountId) {
        return API_HOST + "/v1/accounts/" + accountId;
    }

    // 修改个人账号（根据第三方用户ID）
    public static String modifyPerAccByThirdId_URL(String thirdPartyUserId) {
        return API_HOST + "/v1/accounts/updateByThirdId?thirdPartyUserId=" + thirdPartyUserId;
    }

    // 查询个人账号(根据账号ID)
    public static String queryAccById_URL(String accountId) {
        return API_HOST + "/v1/accounts/" + accountId;
    }

    // 查询个人账号(根据第三方用户ID)
    public static String queryAccByThirdId_URL(String thirdPartyUserId) {
        return API_HOST + "/v1/accounts/getByThirdId?thirdPartyUserId=" + thirdPartyUserId;
    }

    // 注销个人账号(根据账号ID)
    public static String logoutAccById_URL(String accountId) {
        return API_HOST + "/v1/accounts/" + accountId;
    }

    // 注销个人账号(根据第三方用户ID)
    public static String logoutAccByThirdId_URL(String thirdPartyUserId) {
        return API_HOST + "/v1/accounts/deleteByThirdId?thirdPartyUserId=" + thirdPartyUserId;
    }

    // 创建机构账号
    public static String createOrgAcc_URL() {
        return API_HOST + "/v1/organizations/createByThirdPartyUserId";
    }

    // 修改机构账号(根据账号ID)
    public static String modifyOrgAccById_URL(String orgId) {
        return API_HOST + "/v1/organizations/" + orgId;
    }

    // 查询机构账号(根据第三方机构ID)
    public static String queryOrgAccByThirdId_URL(String thirdPartyUserId) {
        return API_HOST + "/v1/organizations/getByThirdId?thirdPartyUserId=" + thirdPartyUserId;
    }

    // 获取文件直传地址
    public static String fileUpload_URL() {
        return API_HOST + "/v1/files/getUploadUrl";
    }

    // 创建签署流程
    public static String createFlows_URL() {
        return API_HOST + "/v1/signflows";
    }

    // ---流程文档(请求方式决定增删或者下载)---
    public static String aboutDocument_URL(String flowId, String fileIds) {
        String url = API_HOST + "/v1/signflows/" + flowId + "/documents";
        if (StringUtils.isNotBlank(fileIds)) {
            url += "?fileIds=" + fileIds;
        }
        return url;
    }

    // 流程附件添加
    public static String aboutAttachment_URL(String flowId) {
        return API_HOST + "/v1/signflows/" + flowId + "/attachments";
    }

    // 添加流程签名域(对接平台自动盖章签名域)
    public static String addAutoSignfieldsForPlatform_URL(String flowId) {
        return API_HOST + "/v1/signflows/" + flowId + "/signfields/platformSign";
    }

    // 创建个人模板印章
    public static String createPerSeal_URL(String accountId) {
        return API_HOST + "/v1/accounts/" + accountId + "/seals/personaltemplate";
    }

    // 创建机构模板印章
    public static String createOfficialSeal_URL(String orgId) {
        return API_HOST + "/v1/organizations/" + orgId + "/seals/officialtemplate";
    }

    // 创建个人/机构图片印章
    public static String createImageSeal_URL(String accountId) {
        return API_HOST + "/v1/accounts/" + accountId + "/seals/image";
    }

    // 添加流程签名域(用户自动盖章签名域)
    public static String addAutoSignfieldsForPerson_URL(String flowId) {
        return API_HOST + "/v1/signflows/" + flowId + "/signfields/autoSign";
    }

    // 添加流程签名域(用户手动盖章签名域)
    public static String addHandSignfieldsForPerson_URL(String flowId) {
        return API_HOST + "/v1/signflows/" + flowId + "/signfields/handSign";
    }

    // 开启签署流程
    public static String startFlows_URL(String flowId) {
        return API_HOST + "/v1/signflows/" + flowId + "/start";
    }

    // 撤销签署流程
    public static String revokeFlows_URL(String flowId) {
        return API_HOST + "/v1/signflows/" + flowId + "/revoke";
    }

    // 归档签署流程
    public static String archiveFlows_URL(String flowId) {
        return API_HOST + "/v1/signflows/" + flowId + "/archive";
    }

    // ---签署授权---
    // 设置/撤销静默签署(POST为设置静默授权,DELETE为撤销静默授权)
    public static String signAuth_URL(String accountId) {
        return API_HOST + "/v1/signAuth/" + accountId;
    }

    // 查询流程文档关键字坐标
    public static String getKeywordsPosition_URL(String flowId, String fileId, String keywords) {
        StringBuffer keywordsPosition = new StringBuffer();
        keywordsPosition.append(API_HOST).append("/v1/signflows/");
        keywordsPosition.append(flowId);
        keywordsPosition.append("/documents/");
        keywordsPosition.append(fileId);
        keywordsPosition.append("/searchWordsPosition");
        if (StringUtils.isNotEmpty(keywords)) {
            keywordsPosition.append("?keywords=" + keywords);
        }
        return keywordsPosition.toString();
    }

    // 获取签署地址
    public static String sign_URL(String flowId, String accountId, String organizeId, String urlType) {
        String url = API_HOST + "/v1/signflows/" + flowId + "/executeUrl?accountId=" + accountId;
        if (StringUtils.isNotBlank(organizeId)) {
            url += "&organizeId=" + organizeId;
        }
        if (StringUtils.isNotBlank(urlType)) {
            url += "&urlType=" + urlType;
        }
        return url;
    }

    // 归档签署流程
    public static String getVoucher_URL(String flowId) {
        return API_HOST + "/api/v2/signflows/" + flowId + "/archive";
    }

    // 查询签署中意愿认证信息
    public static String identityDetail_URL() {
        return API_HOST + "/v2/signflows/identity/detail";
    }

    // 根据签署流程查询账号认证状态
    public static String getIdentityStatus(String flowId, String accountId) {
        return API_HOST + "/v2/signflows/" + flowId + "/identityRecord?accountId=" + accountId;
    }

    // 根据认证流程id查询认证信息
    public static String getIdentityInfo(String authId) {
        return API_HOST + "/v2/identity/auth/api/common/" + authId + "/detail";
    }

    // 机构账号注销
    public static String deleteOrganization(String orgId) {
        return API_HOST + "/v1/organizations/" + orgId;
    }

    // 【人脸识别认证】个人实名
    public static String individualIdentity_URL(String accountId) {
        return API_HOST + "/v2/identity/auth/api/individual/" + accountId + "/face";
    }

    // 【3要素】企业实名认证
    public static String organizationIdentityThree_URL(String accountId) {
        return API_HOST + "/v2/identity/auth/api/organization/" + accountId + "/threeFactors";
    }

    // 【反向打款认证】发起反向打款
    public static String organizationReversePayment_URL(String flowId) {
        return API_HOST + "/v2/identity/auth/pub/organization/" + flowId + "/reversePayment";
    }

    // 【反向打款认证】发起反向打款
    public static String reversePaymentProcess_URL(String flowId) {
        return API_HOST + "/v2/identity/auth/pub/organization/" + flowId + "/query/reversePaymentProcess";
    }

    // 个人运营商3要素信息比对
    public static String individualTelecom3Factors_URL() {
        return API_HOST + "/v2/identity/verify/individual/telecom3Factors";
    }

    /* 获取意愿认证方式 */
    public static List<String> getWillTypes() {
        List<String> willTypes = new ArrayList<>();
        willTypes.add(ESignConstant.FACE_ZHIMA_XY);// 支付宝刷脸
        willTypes.add(ESignConstant.FACE_TECENT_CLOUD_H5);// 腾讯云刷脸
        /* 新增了短信验证码 意愿认证方式 add by zcm 20221012 */
        willTypes.add(ESignConstant.CODE_SMS);
        return willTypes;
    }
}
