package com.heyu.api.controller.face;


import com.aliyun.facebody20191230.models.MonitorExaminationResponse;
import com.aliyun.facebody20191230.models.MonitorExaminationResponseBody;
import com.heyu.api.alibaba.handle.facebody.AMonitorExaminationHandle;
import com.heyu.api.alibaba.request.facebody.AMonitorExaminationRequest;
import com.heyu.api.controller.BaseController;
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.face.MonitorExaminationScreenReq;
import com.heyu.api.resp.face.MonitorExaminationFaceResp;
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;

/***
 * https://next.api.aliyun.com/api/facebody/2019-12-30/MonitorExamination?tab=DOC&params={%22ImageURL%22:%22http%3A%2F%2Fworkbench-file-transfer.oss-cn-shanghai.aliyuncs.com%2Fuser-files%2F84753ea9-9809-4348-ac27-71437b89fee9-WechatIMG29.jpg%3FOSSAccessKeyId%3DLTAI5tRvL6vYdjKSfTFZ156m%26Expires%3D1742056256%26Signature%3Dl%252B1vglgYe%252BIN3wHE4Eror6e5tj4%253D%26response-content-disposition%3Dattachment%22,%22Type%22:1}&lang=JAVA
 *线上监考
 *
 * MonitorExamination
 *
 * 人脸监控
 *
 *
 */
@Slf4j
@RestController
@RequestMapping("/monitor")
@NotIntercept
public class MonitorExaminationFaceController extends BaseController {


    @Autowired
    private AMonitorExaminationHandle aMonitorExaminationHandle;

    @RequestMapping("/face")
    public R screen(MonitorExaminationScreenReq req) {


        MonitorExaminationFaceResp monitorExaminationScreenResp = new MonitorExaminationFaceResp();

        AMonitorExaminationRequest aMonitorExaminationRequest = new AMonitorExaminationRequest();
        aMonitorExaminationRequest.setImageUrl(req.getImageUrl());
        aMonitorExaminationRequest.setImageBase64(req.getImageBase64());
        // 1：考生状态检测
        aMonitorExaminationRequest.setType(1L);
        ApiR<MonitorExaminationResponse> aR = aMonitorExaminationHandle.handle(aMonitorExaminationRequest);
        if (aR.isSuccess() && isSuccessStatusCode(aR.getData().getStatusCode())) {
            /**
             * {
             *   "RequestId": "00FA2FA3-3773-50B4-B07D-46E585034E5A",
             *   "Data": {
             *     "PersonInfo": {
             *       "PersonNumber": 1,
             *       "EarPhone": {
             *         "Score": 0.9309924244880676,
             *         "Threshold": 0.6
             *       },
             *       "CellPhone": {
             *         "Score": 0.3302510678768158,
             *         "Threshold": 0.6
             *       }
             *     },
             *     "FaceInfo": {
             *       "Completeness": 1,
             *       "Pose": {
             *         "Pitch": 6.967320919036865,
             *         "Roll": -1.4554272890090942,
             *         "Yaw": -28.746829986572266
             *       },
             *       "FaceNumber": 1
             *     }
             *   }
             * }
             */
            MonitorExaminationResponseBody.MonitorExaminationResponseBodyData responseBodyData = aR.getData().getBody().getData();

            MonitorExaminationResponseBody.MonitorExaminationResponseBodyDataFaceInfo dataFaceInfo = responseBodyData.getFaceInfo();

            MonitorExaminationResponseBody.MonitorExaminationResponseBodyDataPersonInfo dataPersonInfo = responseBodyData.getPersonInfo();
            MonitorExaminationResponseBody.MonitorExaminationResponseBodyDataPersonInfoEarPhone  infoEarPhone = dataPersonInfo.getEarPhone();
            MonitorExaminationResponseBody.MonitorExaminationResponseBodyDataPersonInfoCellPhone infoCellPhone = dataPersonInfo.getCellPhone();


            MonitorExaminationFaceResp.FaceInfoDTO faceInfoDTO = new MonitorExaminationFaceResp.FaceInfoDTO();
            MonitorExaminationFaceResp.PersonInfoDTO personInfoDTO = new MonitorExaminationFaceResp.PersonInfoDTO();
            faceInfoDTO.setFaceNumber(dataFaceInfo.getFaceNumber());
            faceInfoDTO.setCompleteness(dataFaceInfo.getCompleteness());
            MonitorExaminationFaceResp.FaceInfoDTO.PoseDTO poseDTO = new MonitorExaminationFaceResp.FaceInfoDTO.PoseDTO();
            MonitorExaminationResponseBody.MonitorExaminationResponseBodyDataFaceInfoPose pose = dataFaceInfo.getPose();
            poseDTO.setPitch(pose.getPitch());
            poseDTO.setYaw(pose.getYaw());
            poseDTO.setRoll(pose.getRoll());

            faceInfoDTO.setPose(poseDTO);


            personInfoDTO.setPersonNumber(dataPersonInfo.getPersonNumber());

            MonitorExaminationFaceResp.PersonInfoDTO.EarPhoneDTO earPhoneDTO = new MonitorExaminationFaceResp.PersonInfoDTO.EarPhoneDTO();
            MonitorExaminationFaceResp.PersonInfoDTO.CellPhoneDTO cellPhoneDTO = new MonitorExaminationFaceResp.PersonInfoDTO.CellPhoneDTO();
            earPhoneDTO.setScore(infoEarPhone.getScore());
            earPhoneDTO.setThreshold(infoEarPhone.getThreshold());


            cellPhoneDTO.setScore(infoCellPhone.getScore());
            cellPhoneDTO.setThreshold(infoCellPhone.getThreshold());

            personInfoDTO.setEarPhone(earPhoneDTO);
            personInfoDTO.setCellPhone(cellPhoneDTO);


            monitorExaminationScreenResp.setFaceInfo(faceInfoDTO);
            monitorExaminationScreenResp.setPersonInfo(personInfoDTO);

            return R.error().setData(monitorExaminationScreenResp);
        }
        return R.error();
    }


}
