package com.heyu.api.controller.video;


import com.aliyun.facebody20191230.models.DetectVideoLivingFaceResponse;
import com.aliyun.facebody20191230.models.DetectVideoLivingFaceResponseBody;
import com.heyu.api.alibaba.handle.facebody.ADetectVideoLivingFaceHandle;
import com.heyu.api.alibaba.request.facebody.ADetectVideoLivingFaceRequest;
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.request.video.DetectVideoLivingFaceReq;
import com.heyu.api.request.video.RecognizeVideoCharacterReq;
import com.heyu.api.resp.video.DetectVideoLivingFaceResp;
import com.lz.mybatis.plugins.interceptor.RestoreQueryDataScopeInterceptor;
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.ArrayList;
import java.util.List;

/***
 * https://next.api.aliyun.com/api/facebody/2019-12-30/DetectVideoLivingFace?tab=DEMO&lang=JAVA&RegionId=cn-shanghai
 *视频活体检测
 *
 * DetectVideoLivingFace
 *
 *
 *
 * 功能描述
 * 视频活体检测能力可以检测输入视频中的人脸来自直接拍摄还是翻拍。当视频中出现多个人脸时，对画面中面积最大的人脸进行判断，需要确保待检测人脸五官清晰可见。
 *
 *
 */
@Slf4j
@RestController
@RequestMapping("/video/living")
@NotIntercept
public class DetectVideoLivingFaceController extends BaseController {


    @Autowired
    private ADetectVideoLivingFaceHandle aDetectVideoLivingFaceHandle;


    @RequestMapping("/recognize")
    @CacheResult
    public R recognize(DetectVideoLivingFaceReq req) {

        List<DetectVideoLivingFaceResp> resps = new ArrayList<>();

        ADetectVideoLivingFaceRequest request = new ADetectVideoLivingFaceRequest();
        request.setImageUrl(req.getVideoURL());
        request.setImageBase64(req.getVideoBase64());
        request.setVideoSuffix(req.getVideoSuffix());


        ApiR<DetectVideoLivingFaceResponse> aR = aDetectVideoLivingFaceHandle.handle(request);
        if (aR.isSuccess() && isSuccessStatusCode(aR.getData().getStatusCode())) {
            List<DetectVideoLivingFaceResponseBody.DetectVideoLivingFaceResponseBodyDataElements> elements = aR.getData().getBody().getData().getElements();
            for (DetectVideoLivingFaceResponseBody.DetectVideoLivingFaceResponseBodyDataElements element : elements) {

                DetectVideoLivingFaceResp detectVideoLivingFaceResp = new DetectVideoLivingFaceResp();
                detectVideoLivingFaceResp.setFaceConfidence(element.getFaceConfidence());
                detectVideoLivingFaceResp.setLiveConfidence(element.getLiveConfidence());

                resps.add(detectVideoLivingFaceResp);

            }
            return R.ok().setData(resps);
        }

        return R.error();

    }


}
