package com.heyu.api.controller.traffic;

import com.heyu.api.baidu.handle.traffic.BLicensePlateHandle;
import com.heyu.api.baidu.request.traffic.BLicensePlateRequest;
import com.heyu.api.baidu.response.traffic.BLicensePlateResp;
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.utils.ApiR;
import com.heyu.api.data.utils.R;
import com.heyu.api.resp.traffic.LicensePlateResp;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;


/**
 * https://console.bce.baidu.com/support/?_=1740219852952&timestamp=1740325926517#/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%2Flicense_plate&method=post
 * <p>
 * 车牌识别
 *
 *
 * 接口描述
 * 支持识别中国大陆机动车蓝牌、黄牌（单双行）、绿牌、大型新能源（黄绿）、领使馆车牌、警牌、武警牌（单双行）、军牌（单双行）、港澳出入境车牌、
 * 农用车牌、民航车牌、非机动车车牌（北京地区）的地域编号和车牌号，并能同时识别图像中的多张车牌。
 *
 *
 */
@Slf4j
@RestController
@RequestMapping("/license/plate")
@NotIntercept
public class LicensePlateController extends BaseController {


    @Autowired
    private BLicensePlateHandle bLicensePlateHandle;


    @RequestMapping("/recognize")
    @CacheResult
    public R recognize(BLicensePlateRequest request) {

        List<LicensePlateResp> respList = new ArrayList<>();


        ApiR<BLicensePlateResp> bR = bLicensePlateHandle.handle(request);
        if (bR.isSuccess()) {
            BLicensePlateResp bLicensePlateResp = bR.getData();
            List<BLicensePlateResp.WordsResultDTO> wordsResultDTOS = bLicensePlateResp.getWordsResult();
            for (BLicensePlateResp.WordsResultDTO wordsResultDTO : wordsResultDTOS) {
                LicensePlateResp licensePlateResp = new LicensePlateResp();

                licensePlateResp.setColor(wordsResultDTO.getColor());
                licensePlateResp.setProbability(wordsResultDTO.getProbability());
                licensePlateResp.setCoverInfo(wordsResultDTO.getCoverInfo());
                licensePlateResp.setNumber(wordsResultDTO.getNumber());
                licensePlateResp.setEditTool(wordsResultDTO.getEditTool());


                List<BLicensePlateResp.WordsResultDTO.VertexesLocationDTO> vertexesLocationDTOS = wordsResultDTO.getVertexesLocation();
                if (CollectionUtils.isNotEmpty(vertexesLocationDTOS)) {
                    List<LicensePlateResp.VertexesLocationDTO> vertexesLocationDTOS1 = new ArrayList<>();
                    for (BLicensePlateResp.WordsResultDTO.VertexesLocationDTO vertexesLocationDTO : vertexesLocationDTOS) {
                        LicensePlateResp.VertexesLocationDTO locationDTO = new LicensePlateResp.VertexesLocationDTO();
                        locationDTO.setX(vertexesLocationDTO.getX());
                        locationDTO.setY(vertexesLocationDTO.getY());
                        vertexesLocationDTOS1.add(locationDTO);

                    }
                    licensePlateResp.setVertexesLocation(vertexesLocationDTOS1);
                }

                respList.add(licensePlateResp);
            }

            return R.ok().setData(respList);

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