package com.heyu.api.controller.car;


import com.aliyun.ocr20191230.models.RecognizeDrivingLicenseResponse;
import com.aliyun.ocr20191230.models.RecognizeDrivingLicenseResponseBody;
import com.heyu.api.alibaba.handle.common.text.ARecognizeDrivingLicenseHandle;
import com.heyu.api.alibaba.request.common.text.ARecognizeDrivingLicenseRequest;
import com.heyu.api.baidu.handle.traffic.BVehicleLicenseHandle;
import com.heyu.api.baidu.request.traffic.BVehicleLicenseRequest;
import com.heyu.api.controller.BaseController;
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.resp.car.RecognizeDrivingLicenseBackResp;
import com.heyu.api.resp.car.RecognizeDrivingLicenseFaceResp;
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://next.api.aliyun.com/api/ocr/2019-12-30/RecognizeDrivingLicense?tab=DEMO&lang=JAVA
 *行驶证识别
 *
 * RecognizeDrivingLicense
 * 行驶证识别能力可以识别行驶证首页和副页关键字段内容，输出品牌型号、车辆类型、车牌号码、检验记录、核定载质量、核定载人数等 21 个关键字段信息。
 *
 * https://console.bce.baidu.com/support/?_=1740219852952&timestamp=1740323990690#/api?product=AI&project=%E6%96%87%E5%AD%97%E8%AF%86%E5%88%AB&parent=%E4%BA%A4%E9%80%9A%E5%9C%BA%E6%99%AFOCR&api=rest%2F2.0%2Focr%2Fv1%2Fvehicle_license&method=post
 *
 *
 */
@Slf4j
@RestController
@RequestMapping("/driving/license")
@NotIntercept
public class RecognizeDrivingLicenseController extends BaseController {


    @Autowired
    private ARecognizeDrivingLicenseHandle arRecognizeDrivingLicenseHandle;


@Autowired
    private BVehicleLicenseHandle bVehicleLicenseHandle;


    @RequestMapping("/recognize")
    public R recognize(ARecognizeDrivingLicenseRequest aRecognizeDrivingLicenseRequest) {
        RecognizeDrivingLicenseBackResp backResp = new RecognizeDrivingLicenseBackResp();
        RecognizeDrivingLicenseFaceResp faceResp = new RecognizeDrivingLicenseFaceResp();

        BVehicleLicenseRequest bVehicleLicenseRequest = new BVehicleLicenseRequest();
        bVehicleLicenseRequest.setVehicleLicenseSide(aRecognizeDrivingLicenseRequest.getSide());
        bVehicleLicenseRequest.setImageUrl(aRecognizeDrivingLicenseRequest.getImageUrl());
        bVehicleLicenseRequest.setImageBase64(aRecognizeDrivingLicenseRequest.getImageBase64());
        ApiR<Map> bR = bVehicleLicenseHandle.handle(bVehicleLicenseRequest);

        if (bR.isSuccess()) {
            Map<String, Object> data = bR.getData();
            if (ApiConstants.face.equals(aRecognizeDrivingLicenseRequest.getSide())) {
                faceResp.setIssueDate(MapUtils.getByExpr(data, "words_result.发证日期.words"));
                faceResp.setModel(MapUtils.getByExpr(data, "words_result.品牌型号.words"));   // 品牌型号
                faceResp.setVehicleType(MapUtils.getByExpr(data, "words_result.车辆类型.words"));// 车辆类型
                faceResp.setOwner(MapUtils.getByExpr(data, "words_result.所有人.words"));
                faceResp.setEngineNumber(MapUtils.getByExpr(data, "words_result.发动机号码.words"));
                faceResp.setPlateNumber(MapUtils.getByExpr(data, "words_result.号牌号码.words"));
                faceResp.setAddress(MapUtils.getByExpr(data, "words_result.住址.words"));
                faceResp.setUseCharacter(MapUtils.getByExpr(data, "words_result.使用性质.words"));
                faceResp.setVin(MapUtils.getByExpr(data, "words_result.车辆识别代号.words"));
                faceResp.setRegisterDate(MapUtils.getByExpr(data, "words_result.发动机号码.words"));
                return R.ok().setData(faceResp);
            }

            if (ApiConstants.back.equals(aRecognizeDrivingLicenseRequest.getSide())) {
                backResp.setOverallDimension(MapUtils.getByExpr(data, "words_result.外廓尺寸.words"));
                backResp.setInspectionRecord(MapUtils.getByExpr(data, "words_result.检验记录.words"));
                backResp.setUnladenMass(MapUtils.getByExpr(data, "words_result.整备质量.words"));
                backResp.setFileNumber(MapUtils.getByExpr(data, "words_result.档案编号.words"));
                backResp.setTractionMass(MapUtils.getByExpr(data, "words_result.准牵引总质量.words"));
                backResp.setGrossMass(MapUtils.getByExpr(data, "words_result.总质量.words"));
                backResp.setPlateNumber(MapUtils.getByExpr(data, "words_result.号牌号码.words"));
                backResp.setApprovedPassengerCapacity(MapUtils.getByExpr(data, "words_result.核定载人数.words"));
                backResp.setEnergyType(MapUtils.getByExpr(data, "words_result.燃油类型.words"));
                backResp.setApprovedLoad(MapUtils.getByExpr(data, "words_result.核定载质量.words"));
                return R.ok().setData(backResp);
            }
        }

        ApiR<RecognizeDrivingLicenseResponse> aR = arRecognizeDrivingLicenseHandle.handle(aRecognizeDrivingLicenseRequest);
        if (aR.isSuccess() && aR.getData() != null && isSuccessStatusCode(aR.getData().statusCode)) {
            RecognizeDrivingLicenseResponse response = aR.getData();
            RecognizeDrivingLicenseResponseBody responseBody = response.getBody();
            RecognizeDrivingLicenseResponseBody.RecognizeDrivingLicenseResponseBodyData responseBodyData = responseBody.getData();

            RecognizeDrivingLicenseResponseBody.RecognizeDrivingLicenseResponseBodyDataFaceResult faceResult = responseBodyData.getFaceResult();
            RecognizeDrivingLicenseResponseBody.RecognizeDrivingLicenseResponseBodyDataBackResult backResult = responseBodyData.getBackResult();
            if (ApiConstants.face.equals(aRecognizeDrivingLicenseRequest.getSide())) {
                faceResp.setIssueDate(faceResult.getIssueDate());
                faceResp.setModel(faceResult.getModel());
                faceResp.setVehicleType(faceResult.getVehicleType());
                faceResp.setOwner(faceResult.getOwner());
                faceResp.setEngineNumber(faceResult.getEngineNumber());
                faceResp.setPlateNumber(faceResult.getPlateNumber());
                faceResp.setAddress(faceResult.getAddress());
                faceResp.setUseCharacter(faceResult.getUseCharacter());
                faceResp.setVin(faceResult.getVin());
                faceResp.setRegisterDate(faceResult.getRegisterDate());
                return R.ok().setData(faceResp);
            }
            if (ApiConstants.back.equals(aRecognizeDrivingLicenseRequest.getSide())) {
                backResp.setOverallDimension(backResult.getOverallDimension());
                backResp.setInspectionRecord(backResult.getInspectionRecord());
                backResp.setUnladenMass(backResult.getUnladenMass());
                backResp.setFileNumber(backResult.getFileNumber());
                backResp.setTractionMass(backResult.getTractionMass());
                backResp.setGrossMass(backResult.getGrossMass());
                backResp.setPlateNumber(backResult.getPlateNumber());
                backResp.setApprovedPassengerCapacity(backResult.getApprovedPassengerCapacity());
                backResp.setEnergyType(backResult.getEnergyType());
                backResp.setApprovedLoad(backResult.getApprovedLoad());
                return R.ok().setData(backResp);
            }
        }

        return R.ok();
    }

}