package com.heyu.api.controller.face;


import com.aliyun.facebody20191230.models.DetectInfraredLivingFaceResponse;
import com.aliyun.facebody20191230.models.DetectInfraredLivingFaceResponseBody;
import com.heyu.api.alibaba.handle.facebody.ADetectInfraredLivingFaceHandle;
import com.heyu.api.alibaba.request.facebody.ADetectInfraredLivingFaceRequest;
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.face.DetectInfraredLivingFaceListResp;
import com.heyu.api.resp.face.DetectInfraredLivingFaceResp;
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://next.api.aliyun.com/api/facebody/2019-12-30/DetectInfraredLivingFace?tab=DOC&lang=JAVA&RegionId=cn-shanghai
 *红外人脸活体检测
 *
 * DetectInfraredLivingFace
 *
 */
@Slf4j
@RestController
@RequestMapping("/face")
@NotIntercept
public class DetectInfraredLivingFaceController extends BaseController {

    @Autowired
    private ADetectInfraredLivingFaceHandle aDetectInfraredLivingFaceHandle;


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

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


        ApiR<DetectInfraredLivingFaceResponse> aR = aDetectInfraredLivingFaceHandle.handle(request);

        if (aR.isSuccess() && isSuccessStatusCode(aR.getData().getStatusCode())) {

            DetectInfraredLivingFaceResponseBody responseBody = aR.getData().getBody();
            DetectInfraredLivingFaceResponseBody.DetectInfraredLivingFaceResponseBodyData responseBodyData = responseBody.getData();
            List<DetectInfraredLivingFaceResponseBody.DetectInfraredLivingFaceResponseBodyDataElements> elementsList = responseBodyData.getElements();

            List<DetectInfraredLivingFaceResp> detectInfraredLivingFaceResps = new ArrayList<>();
            if (CollectionUtils.isNotEmpty(elementsList)) {
                for (DetectInfraredLivingFaceResponseBody.DetectInfraredLivingFaceResponseBodyDataElements elements : elementsList) {
                    DetectInfraredLivingFaceResp resp = new DetectInfraredLivingFaceResp();

                    resp.setFaceNumber(elements.getFaceNumber());
                    resp.setImageURL(elements.getImageURL());
                    List<DetectInfraredLivingFaceListResp> detectInfraredLivingFaceListResps = new ArrayList<>();
                    if (CollectionUtils.isNotEmpty(elements.getResults())) {
                        for (DetectInfraredLivingFaceResponseBody.DetectInfraredLivingFaceResponseBodyDataElementsResults result : elements.getResults()) {

                            DetectInfraredLivingFaceListResp detectInfraredLivingFaceListResp = new DetectInfraredLivingFaceListResp();
                            detectInfraredLivingFaceListResp.setRate(result.getRate());
                            detectInfraredLivingFaceListResp.setSuggestion(result.getSuggestion());
                            detectInfraredLivingFaceListResp.setLabel(result.getLabel());
                            detectInfraredLivingFaceListResp.setMessageTips(result.getMessageTips());
                            detectInfraredLivingFaceListResps.add(detectInfraredLivingFaceListResp);

                        }
                    }

                    resp.setFaceListRespList(detectInfraredLivingFaceListResps);

                    respList.add(resp);
                    detectInfraredLivingFaceResps.add(resp);
                }
                return R.ok().setData(detectInfraredLivingFaceResps);
            }
        }
        return R.error(aR.getErrorMsg());
    }


}
