package com.heyu.api.controller.idcard;


import com.aliyun.ocr20191230.models.RecognizeIdentityCardResponse;
import com.aliyun.ocr20191230.models.RecognizeIdentityCardResponseBody;
import com.heyu.api.alibaba.handle.common.text.ARecognizeIdentityCardHandle;
import com.heyu.api.alibaba.request.common.text.ARecognizeIdentityCardRequest;
import com.heyu.api.baidu.handle.certificate.BIdcardHandle;
import com.heyu.api.baidu.request.certificate.BIdcardRequest;
import com.heyu.api.baidu.response.certificate.BIdcardResp;
import com.heyu.api.request.certificate.ApiIdentityCardRequest;
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.dto.certificate.ApiIdentityCardBackResp;
import com.heyu.api.data.dto.certificate.ApiIdentityCardFrontResp;
import com.heyu.api.data.utils.ApiR;
import com.heyu.api.data.utils.R;
import com.heyu.api.data.utils.StringUtils;
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;

/***
 * 识别身份证
 */
@Slf4j
@RestController
@RequestMapping("/id/card")
@NotIntercept
public class IdCardController {

    @Autowired
    private ARecognizeIdentityCardHandle aRecognizeIdentityCardHandle;

    @Autowired
    private BIdcardHandle bIdcardHandle;

    @RequestMapping("/recognize")
    @CacheResult
    public R recognize(ApiIdentityCardRequest apiIdentityCardRequest) {
        ApiIdentityCardFrontResp frontResp = new ApiIdentityCardFrontResp(apiIdentityCardRequest.getIdCardSide());

        ApiIdentityCardBackResp backResp = new ApiIdentityCardBackResp(apiIdentityCardRequest.getIdCardSide());
        // 阿里识别
        ARecognizeIdentityCardRequest aRecognizeIdentityCardRequest = new ARecognizeIdentityCardRequest();
        ApiR<RecognizeIdentityCardResponse> aR = aRecognizeIdentityCardHandle.handle(aRecognizeIdentityCardRequest);
        aRecognizeIdentityCardRequest.setImageBase64(apiIdentityCardRequest.getImageBase64());
        aRecognizeIdentityCardRequest.setImageUrl(apiIdentityCardRequest.getImageUrl());
        aRecognizeIdentityCardRequest.setSide(apiIdentityCardRequest.getIdCardSide());

        if (aR.isSuccess()) {
            RecognizeIdentityCardResponse executeServerSideVerificationResponse = aR.getData();
            if (executeServerSideVerificationResponse.getBody() != null
                    && executeServerSideVerificationResponse.getStatusCode() == 200) {
                RecognizeIdentityCardResponseBody body = executeServerSideVerificationResponse.getBody();
                RecognizeIdentityCardResponseBody.RecognizeIdentityCardResponseBodyData bodyData = body.getData();
                if (StringUtils.equals(ApiConstants.front, apiIdentityCardRequest.getIdCardSide())) {
                    RecognizeIdentityCardResponseBody.RecognizeIdentityCardResponseBodyDataFrontResult frontResult = bodyData.getFrontResult();
                    frontResp.setAddress(frontResult.getAddress());
                    frontResp.setGender(frontResult.getGender());
                    frontResp.setBirthday(frontResult.getBirthDate());
                    frontResp.setIdCardNumber(frontResult.getIDNumber());
                    frontResp.setNationality(frontResult.getNationality());
                    frontResp.setRealName(frontResult.getName());
                    frontResp.setCheckResult(1);
                    return R.ok("正面识别成功").setData(frontResp);
                } else {
                    RecognizeIdentityCardResponseBody.RecognizeIdentityCardResponseBodyDataBackResult backResult = bodyData.getBackResult();
                    backResp.setStartDate(backResult.getStartDate());
                    backResp.setIssue(backResult.getIssue());
                    backResp.setEndDate(backResult.getEndDate());
                    backResp.setCheckResult(1);
                    return R.ok("反面识别成功").setData(backResp);
                }
            }
        }
        // 百度识别
        BIdcardRequest bIdcardRequest = new BIdcardRequest();
        bIdcardRequest.setImageUrl(apiIdentityCardRequest.getImageUrl());
        bIdcardRequest.setImageBase64(apiIdentityCardRequest.getImageBase64());
        bIdcardRequest.setIdCardSide(apiIdentityCardRequest.getIdCardSide());
        ApiR<BIdcardResp> bR = bIdcardHandle.handle(bIdcardRequest);
        if (bR.isSuccess()) {
            BIdcardResp bIdcardResp = bR.getData();
            if (bIdcardResp != null && StringUtils.equals(bIdcardResp.getImageStatus(), ApiConstants.normal)) {
                if (StringUtils.equals(ApiConstants.front, apiIdentityCardRequest.getIdCardSide())) {
                    BIdcardResp.WordsResultDTO wordsResultDTO = bIdcardResp.getWordsResult();
                    frontResp.setAddress(wordsResultDTO.get住址().getWords());
                    frontResp.setGender(wordsResultDTO.get性别().getWords());
                    frontResp.setBirthday(wordsResultDTO.get出生().getWords());
                    frontResp.setIdCardNumber(wordsResultDTO.get公民身份号码().getWords());
                    frontResp.setNationality(wordsResultDTO.get民族().getWords());
                    frontResp.setRealName(wordsResultDTO.get姓名().getWords());
                    frontResp.setCheckResult(1);
                    return R.ok("正面识别成功").setData(frontResp);
                } else {
                    BIdcardResp.WordsResultDTO wordsResultDTO = bIdcardResp.getWordsResult();
                    backResp.setStartDate(wordsResultDTO.get签发日期().getWords());
                    backResp.setIssue(wordsResultDTO.get签发机关().getWords());
                    backResp.setEndDate(wordsResultDTO.get失效日期().getWords());
                    backResp.setCheckResult(1);
                    return R.ok("反向识别成功").setData(frontResp);
                }
            }
        }


        return R.error("识别失败").setData(frontResp);
    }

}
