package com.heyu.api.controller.bankcard;


import com.aliyun.ocr20191230.models.RecognizeBankCardResponse;
import com.aliyun.ocr20191230.models.RecognizeBankCardResponseBody;
import com.heyu.api.alibaba.handle.common.text.ARecognizeBankCardHandle;
import com.heyu.api.alibaba.request.common.text.ARecognizeBankCardRequest;
import com.heyu.api.baidu.handle.certificate.BBankcardHandle;
import com.heyu.api.baidu.request.certificate.BBankcardRequest;
import com.heyu.api.baidu.response.certificate.BBankcardResp;
import com.heyu.api.controller.BaseController;
import com.heyu.api.data.annotation.CacheResult;
import com.heyu.api.data.annotation.NotIntercept;
import com.heyu.api.data.enums.BankCardTypeEnums;
import com.heyu.api.data.utils.ApiR;
import com.heyu.api.data.utils.R;
import com.heyu.api.resp.certificate.BankCardRecognizeResp;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/***
 *
 * https://next.api.aliyun.com/api/ocr/2019-12-30/RecognizeBankCard?RegionId=cn-shanghai&tab=DOC&lang=JAVA
 *功能描述
 *
 * 银行卡识别能力可以检测输入的主流银行卡图像，输出 3 个信息，包括：发卡行、银行卡号、有效日期。
 *
 */
@Slf4j
@RestController
@RequestMapping("/bank/card")
@NotIntercept
public class BankCardRecognizeController extends BaseController {


    @Autowired
    private ARecognizeBankCardHandle recognizeBankCardHandle;


    @Autowired
    private BBankcardHandle bBankcardHandle;



    @RequestMapping("/recognize")
    @CacheResult
    public R recognize(ARecognizeBankCardRequest commonTextRequest) {
        BBankcardRequest bBankcardRequest = new BBankcardRequest();
        bBankcardRequest.setImageUrl(commonTextRequest.getImageUrl());
        bBankcardRequest.setImageBase64(commonTextRequest.getImageBase64());
        BankCardRecognizeResp bankCardRecognizeResp = new BankCardRecognizeResp();

        ApiR<BBankcardResp> bR = bBankcardHandle.handle(bBankcardRequest);
        if (bR.isSuccess()) {
            BBankcardResp bBankcardResp = bR.getData();
            if (bBankcardResp.getResult() != null) {
                BBankcardResp.ResultDTO resultDTO = bBankcardResp.getResult();
                bankCardRecognizeResp.setBankCardNumber(resultDTO.getBankCardNumber());
                bankCardRecognizeResp.setBankName(resultDTO.getBankName());
                bankCardRecognizeResp.setValidDate(resultDTO.getValidDate());
                bankCardRecognizeResp.setHolderName(resultDTO.getHolderName());
                BankCardTypeEnums bankCardTypeEnums = BankCardTypeEnums.getBaidu(resultDTO.getBankCardType() + "");

                bankCardRecognizeResp.setBankCardType(bankCardTypeEnums.getResult());
                bankCardRecognizeResp.setBankCardTypeDesc(bankCardTypeEnums.getDesc());
                return R.ok().setData(bankCardRecognizeResp);
            }
        }




        ApiR<RecognizeBankCardResponse> aR = recognizeBankCardHandle.handle(commonTextRequest);
        if (aR.isSuccess()) {
            RecognizeBankCardResponse response = aR.getData();
            if (isSuccessStatusCode(response.getStatusCode())) {
                RecognizeBankCardResponseBody body = response.getBody();
                RecognizeBankCardResponseBody.RecognizeBankCardResponseBodyData recognizeBankCardResponseBodyData = body.getData();
                bankCardRecognizeResp.setBankCardNumber(recognizeBankCardResponseBodyData.getCardNumber());
                bankCardRecognizeResp.setBankName(recognizeBankCardResponseBodyData.getBankName());
                bankCardRecognizeResp.setValidDate(recognizeBankCardResponseBodyData.getValidDate());

                BankCardTypeEnums bankCardTypeEnums = BankCardTypeEnums.getBaidu(recognizeBankCardResponseBodyData.getCardType() + "");

                bankCardRecognizeResp.setBankCardType(bankCardTypeEnums.getResult());
                bankCardRecognizeResp.setBankCardTypeDesc(bankCardTypeEnums.getDesc());
                return R.ok().setData(bankCardRecognizeResp);
            }
        }

        return R.ok(aR.getErrorMsg());
    }


}
