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

import com.alibaba.fastjson.JSONObject;
import com.aliyun.teautil.models.RuntimeOptions;
import com.heyu.api.alibaba.AlibabaBaseHandle;
import com.heyu.api.alibaba.request.common.text.ABank4MetaVerifyRequest;
import com.heyu.api.alibaba.resp.TBankMetaVerifyResponse;
import com.heyu.api.data.constants.ApiConstants;
import com.heyu.api.data.dao.api.ApiBankCardDao;
import com.heyu.api.data.entity.api.ApiBankCardEntity;
import com.heyu.api.data.service.api.ApiRechargeMoneyService;
import com.heyu.api.data.utils.BankCardUtils;
import com.heyu.api.data.utils.StringUtils;
import com.heyu.api.data.utils.HttpUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/****
 * https://market.aliyun.com/apimarket/detail/cmapi00066584#sku=yuncode60584000011
 *
 * 银行卡二，三，四要素核验
 *
 * BankMetaVerify
 *
 *
 */
@Component
@Slf4j
public class ABank2MetaVerifyHandle extends AlibabaBaseHandle<ABank4MetaVerifyRequest, TBankMetaVerifyResponse> {


    @Autowired
    private ApiBankCardDao apiBankCardDao;

    @Autowired
    private ApiRechargeMoneyService apiRechargeMoneyService;


    @Override
    public String check(ABank4MetaVerifyRequest p) {


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

        /***
         * 调用产品类型：
         *
         * BANK_CARD_2_META：银⾏卡号+姓名核验。
         * BANK_CARD_3_META：银⾏卡号+姓名+身份证号核验。
         * BANK_CARD_4_META：银⾏卡号+姓名+身份证号+⼿机号核验。
         * 示例值:
         * BANK_CARD_2_META
         */
        if (StringUtils.isBlank(p.getBankCard())
                || StringUtils.isBlank(p.getRealName())) {
            return "请输入银⾏卡号+姓名";
        }

        return null;
    }


    @Override
    public TBankMetaVerifyResponse run(ABank4MetaVerifyRequest ap, RuntimeOptions runtime) throws Exception {
        ApiBankCardEntity apiBankCard = apiBankCardDao.selectApiBankCardByBankCardRealName(ap.getBankCard(), ap.getRealName(), ApiConstants.third, 1);
        if (apiBankCard != null) {
            TBankMetaVerifyResponse bank2MetaVerifyResponse = JSONObject.parseObject(apiBankCard.getApiResult(), TBankMetaVerifyResponse.class);

            apiBankCardDao.updateCoverApiBankCardUseCountById(1L, apiBankCard.getId());
            return bank2MetaVerifyResponse;
        }

        String host = "https://kzbank3v1.market.alicloudapi.com";
        String path = "/api/bankcard2/check";
        String method = "POST";
        String appcode = ApiConstants.APP_CODE;
        Map<String, String> headers = new HashMap<String, String>();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + appcode);
        //根据API的要求，定义相对应的Content-Type
        headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
        Map<String, String> querys = new HashMap<String, String>();
        Map<String, String> bodys = new HashMap<String, String>();

        bodys.put("bankcard", ap.getBankCard());

        bodys.put("name", ap.getRealName());

        /**
         * 重要提示如下:
         * HttpUtils请从
         * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java
         * 下载
         *
         * 相应的依赖请参照
         * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/pom.xml
         */
        HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys);

        //获取response的body
        String result = EntityUtils.toString(response.getEntity());


        apiBankCard = new ApiBankCardEntity();

        apiBankCard.setBankCard(ap.getBankCard());
        apiBankCard.setRealName(ap.getRealName());
        apiBankCard.setApiResult(result);
        apiBankCard.setUseCount(1L);
        apiBankCard.setChannel( ApiConstants.third);
        apiBankCard.setEnableStatus(1);

        apiBankCardDao.insertApiBankCard(apiBankCard);


        apiRechargeMoneyService.addUseCountAndSendWarnMsg(ApiConstants.bankcard_verification_4, 1);

        TBankMetaVerifyResponse bank2MetaVerifyResponse = JSONObject.parseObject(result, TBankMetaVerifyResponse.class);
        log.info("bankMetaVerifyRequest json : {}", result);
        // 复制代码运行请自行打印 API 的返回值
        return bank2MetaVerifyResponse;


    }
}
