package com.heyu.api.controller.certificate;


import com.heyu.api.baidu.handle.certificate.BHkMacauTaiwanExitentrypermitHandle;
import com.heyu.api.baidu.request.certificate.BHkMacauTaiwanExitentrypermitRequest;
import com.heyu.api.data.annotation.CacheResult;
import com.heyu.api.data.annotation.NotIntercept;
import com.heyu.api.data.constants.ApiConstants;
import com.heyu.api.data.utils.ApiR;
import com.heyu.api.data.utils.MapUtils;
import com.heyu.api.data.utils.R;
import com.heyu.api.request.certificate.TaiwanPassportReq;
import com.heyu.api.resp.certificate.TaiwanPassportBackResp;
import com.heyu.api.resp.certificate.TaiwanPassportFrontResp;
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;

import java.util.Map;


/**
 * https://console.bce.baidu.com/support/?_=1740219852952&timestamp=1740313880415#/api?product=AI&project=%E6%96%87%E5%AD%97%E8%AF%86%E5%88%AB&parent=%E5%8D%A1%E8%AF%81OCR&api=rest%2F2.0%2Focr%2Fv1%2Fhk_macau_taiwan_exitentrypermit&method=post
 * <p>
 * <p>
 * 港澳台证件识别
 * <p>
 * 接口描述
 * 支持识别4类港澳台出入境证件，包含港澳通行证正/反面、台湾通行证正/反面、台胞证（台湾居民来往大陆通行证）正/反面、返乡证（港澳居民来往内地通行证）正/反面，支持识别以上4类证件的全部字段信息。
 * <p>
 * <p>
 * <p>
 * tw_passport_front：台湾通行证正面
 * tw_passport_back：台湾通行证反面
 */
@Slf4j
@RestController
@RequestMapping("/tw/passport")
@NotIntercept
public class TaiwanPassportController {


    @Autowired
    private BHkMacauTaiwanExitentrypermitHandle bhkMacauTaiwanExitentrypermitHandle;


    @RequestMapping("/recognize")
    @CacheResult
    public R recognize(TaiwanPassportReq request) {
        BHkMacauTaiwanExitentrypermitRequest bHkMacauTaiwanExitentrypermitRequest = new BHkMacauTaiwanExitentrypermitRequest();
        bHkMacauTaiwanExitentrypermitRequest.setImageUrl(request.getImageUrl());
        bHkMacauTaiwanExitentrypermitRequest.setImageBase64(request.getImageBase64());
        bHkMacauTaiwanExitentrypermitRequest.setPdfFile(request.getPdfFile());
        bHkMacauTaiwanExitentrypermitRequest.setPdfFileNum(request.getPdfFileNum());
        bHkMacauTaiwanExitentrypermitRequest.setExitentrypermitType("tw_passport_" + request.getSite());

        ApiR<Map> bR = bhkMacauTaiwanExitentrypermitHandle.handle(bHkMacauTaiwanExitentrypermitRequest);
        if (bR.isSuccess()) {
            Map<String,Object> data = bR.getData();
            if (ApiConstants.front.equals(request.getSite())) {
                TaiwanPassportFrontResp resp = new TaiwanPassportFrontResp();
                resp.setCardNumber(MapUtils.getByExpr(data,"words_result.card_number.[0].word"));    //是	array[]	证件号码
                resp.setNameChn(MapUtils.getByExpr(data,"words_result.name_chn.[0].word"));    //是	array[]	姓名
                resp.setNameEng(MapUtils.getByExpr(data,"words_result.name_eng.[0].word"));    //是	array[]	姓名（英文）
                resp.setBirthday(MapUtils.getByExpr(data,"words_result.birthday.[0].word"));    //是	array[]	出生日期
                resp.setSex(MapUtils.getByExpr(data,"words_result.sex.[0].word"));    //是	array[]	性别
                resp.setValidDate(MapUtils.getByExpr(data,"words_result.valid_date.[0].word"));//	是	array[]	有效期限
                resp.setIssueAuthority(MapUtils.getByExpr(data,"words_result.issue_authority.[0].word"));    //是	array[]	签发机关
                resp.setIssuePlace(MapUtils.getByExpr(data,"words_result.issue_place.[0].word"));//	是	array[]	签发地点
                resp.setMRZCode(MapUtils.getByExpr(data,"words_result.MRZCode.[0].word"));//	是	array[]	证件下方第一行
                return R.ok().setData(resp);
            } else {
                TaiwanPassportBackResp resp = new TaiwanPassportBackResp();
                resp.setType(MapUtils.getByExpr(data,"words_result.type.[0].word"));//是	array[]	种类
                resp.setValidDate(MapUtils.getByExpr(data,"words_result.valid_date.[0].word"));//	是	array[]	有效期
                resp.setRemarks(MapUtils.getByExpr(data,"words_result.remarks.[0].word"));//是	array[]	备注
                resp.setRoundTripNumber(MapUtils.getByExpr(data,"words_result.round_trip_number.[0].word"));//	是	array[]	往返有效
                return R.ok().setData(resp);
            }
        }

        return R.error(bR.getErrorMsg());

    }
}
