package com.heyu.api.tencent.handle;

import com.heyu.api.data.utils.IDCardValidatorUtils;
import com.heyu.api.data.utils.MobileUtils;
import com.heyu.api.data.utils.StringUtils;
import com.heyu.api.tencent.TencentBaseHandle;
import com.heyu.api.tencent.request.TPhone3MetaVerificationRequest;
import com.tencentcloudapi.faceid.v20180301.FaceidClient;
import com.tencentcloudapi.faceid.v20180301.models.PhoneVerificationRequest;
import com.tencentcloudapi.faceid.v20180301.models.PhoneVerificationResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/****
 *  https://console.cloud.tencent.com/api/explorer?Product=faceid&Version=2018-03-01&Action=PhoneVerification
 *
 *  手机号三要素核验
 */
@Component
public class TPhone3MetaVerificationHandle extends TencentBaseHandle<TPhone3MetaVerificationRequest, PhoneVerificationResponse> {


    @Autowired
    private FaceidClient client;


    @Override
    public String check(TPhone3MetaVerificationRequest tPhone3MetaVerificationRequest) {
        if (StringUtils.isBlank(tPhone3MetaVerificationRequest.getPhone())
                || StringUtils.isBlank(tPhone3MetaVerificationRequest.getName())
                || StringUtils.isBlank(tPhone3MetaVerificationRequest.getIdCard())) {
            return "姓名 + 身份证号 + 手机号不能为空";
        }

        if (!IDCardValidatorUtils.validateIDCard(tPhone3MetaVerificationRequest.getIdCard())) {
            return "身份证号码验证不正确";
        }


        if (!MobileUtils.validateMobile(tPhone3MetaVerificationRequest.getPhone())) {
            return "必须传入正确的手机号";
        }
        return "";
    }

    @Override
    public PhoneVerificationResponse run(TPhone3MetaVerificationRequest ap) throws Exception {
        PhoneVerificationRequest phoneVerificationRequest = new PhoneVerificationRequest();
        phoneVerificationRequest.setPhone(ap.getPhone());
        phoneVerificationRequest.setName(ap.getName());
        phoneVerificationRequest.setIdCard(ap.getIdCard());
        phoneVerificationRequest.setVerifyMode(ap.getVerifyMode());
        phoneVerificationRequest.setCiphertextBlob(ap.getCiphertextBlob());
        phoneVerificationRequest.setEncryptList(ap.getEncryptList().toArray(new String[0]));
        phoneVerificationRequest.setIv(ap.getIv());
        return client.PhoneVerification(phoneVerificationRequest);
    }
}
