package com.heyu.api.alibaba.handle.common.text;

import com.alibaba.fastjson.JSON;
import com.aliyun.cloudauth20190307.models.BankMetaVerifyRequest;
import com.aliyun.cloudauth20190307.models.BankMetaVerifyResponse;
import com.aliyun.teautil.models.RuntimeOptions;
import com.heyu.api.alibaba.AlibabaBaseHandle;
import com.heyu.api.alibaba.request.common.text.ABankMetaVerifyRequest;
import com.heyu.api.data.constants.ApiConstants;
import com.heyu.api.data.utils.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/****
 * https://next.api.aliyun.com/api/Cloudauth/2019-03-07/BankMetaVerify?tab=DOC&lang=JAVA
 *
 * 银行卡二，三，四要素核验
 *
 * BankMetaVerify
 *
 *
 */
@Component
@Slf4j
public class ABankMetaVerifyHandle extends AlibabaBaseHandle<ABankMetaVerifyRequest, BankMetaVerifyResponse> {

    @Autowired
    private com.aliyun.cloudauth20190307.Client client;

    @Override
    public String check(ABankMetaVerifyRequest p) {
        if (!ApiConstants.bank_card_meta_list.contains(p.getProductType())) {
            return "productType请传，BANK_CARD_2_META：银⾏卡号+姓名核验,BANK_CARD_3_META：银⾏卡号+姓名+身份证号核验,BANK_CARD_4_META：银⾏卡号+姓名+身份证号+⼿机号核验。";
        }

        if (!BankCardUtils.isValidBankCard(p.getBankCard())) {
            log.info("银行卡不合法");
            return "无效银行卡";
        }

        /***
         * 调用产品类型：
         *
         * BANK_CARD_2_META：银⾏卡号+姓名核验。
         * BANK_CARD_3_META：银⾏卡号+姓名+身份证号核验。
         * BANK_CARD_4_META：银⾏卡号+姓名+身份证号+⼿机号核验。
         * 示例值:
         * BANK_CARD_2_META
         */
        if (ApiConstants.BANK_CARD_2_META.contains(p.getProductType())) {
            if (StringUtils.isBlank(p.getBankCard())
                    || StringUtils.isBlank(p.getUserName())) {
                return "请输入银⾏卡号+姓名";
            }
        }
        if (ApiConstants.BANK_CARD_3_META.contains(p.getProductType())) {
            if (StringUtils.isBlank(p.getBankCard())
                    || StringUtils.isBlank(p.getUserName())
                    || StringUtils.isBlank(p.getIdentifyNum())) {
                return "请输入银⾏卡号+姓名+身份证号";
            }
            if (!IDCardValidatorUtils.validateIDCard(p.getIdentifyNum())) {
                return "身份证号必须传入且有效";
            }
        }
        if (ApiConstants.BANK_CARD_4_META.contains(p.getProductType())) {
            if (StringUtils.isBlank(p.getBankCard())
                    || StringUtils.isBlank(p.getUserName())
                    || StringUtils.isBlank(p.getIdentifyNum())
                    || StringUtils.isBlank(p.getMobile())) {
                return "银⾏卡号+姓名+身份证号+⼿机号";
            }

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

    @Override
    public BankMetaVerifyResponse run(ABankMetaVerifyRequest ap, RuntimeOptions runtime) throws Exception {
        BankMetaVerifyRequest bankMetaVerifyRequest = new BankMetaVerifyRequest();

        bankMetaVerifyRequest.setParamType(ap.getParamType());
        bankMetaVerifyRequest.setVerifyMode(ap.getVerifyMode());
        bankMetaVerifyRequest.setProductType(ap.getProductType());
        bankMetaVerifyRequest.setBankCard(ap.getBankCard());
        bankMetaVerifyRequest.setUserName(ap.getUserName());
        bankMetaVerifyRequest.setIdentifyNum(ap.getIdentifyNum());
        bankMetaVerifyRequest.setMobile(ap.getMobile());
        bankMetaVerifyRequest.setIdentityType(ap.getIdentityType());
        log.info("bankMetaVerifyRequest json : {}", JSON.toJSONString(bankMetaVerifyRequest));
        // 复制代码运行请自行打印 API 的返回值
        return client.bankMetaVerifyWithOptions(bankMetaVerifyRequest, runtime);
    }
}
