package com.heyu.api.controller.face;


import com.aliyun.facebody20191230.models.DetectPedestrianIntrusionResponse;
import com.aliyun.facebody20191230.models.DetectPedestrianIntrusionResponseBody;
import com.heyu.api.alibaba.handle.facebody.ADetectPedestrianIntrusionHandle;
import com.heyu.api.alibaba.request.facebody.ADetectPedestrianIntrusionRequest;
import com.heyu.api.alibaba.request.facebody.ADetectPedestrianIntrusionRequestDetectRegion;
import com.heyu.api.alibaba.request.facebody.ADetectPedestrianIntrusionRequestDetectRegionRect;
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.face.DeepfakeFaceReq;
import com.heyu.api.request.face.DetectPedestrianIntrusionReq;
import com.heyu.api.request.face.DetectRegionDTO;
import com.heyu.api.resp.face.DataElementsBox;
import com.heyu.api.resp.face.DetectPedestrianIntrusionResp;
import lombok.Data;
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;

@Slf4j
@RestController
@RequestMapping("/pedestrian/intrusion")
@NotIntercept
public class DetectPedestrianIntrusionController extends BaseController {


    @Autowired
    private ADetectPedestrianIntrusionHandle aDetectPedestrianIntrusionHandle;


    @RequestMapping("/check")
    @CacheResult
    public R check(DetectPedestrianIntrusionReq req) {

        DetectPedestrianIntrusionResp resp = new DetectPedestrianIntrusionResp();

        ADetectPedestrianIntrusionRequest request = new ADetectPedestrianIntrusionRequest();
        request.setImageBase64(req.getImageBase64());
        request.setImageUrl(req.getImageUrl());

        request.setRegionType("rectangle");
        List<ADetectPedestrianIntrusionRequestDetectRegion> detectRegionList = new ArrayList<>();
        for (DetectRegionDTO detectRegionDTO : req.getDetectRegionDTOList()) {
            ADetectPedestrianIntrusionRequestDetectRegion detectRegion = new ADetectPedestrianIntrusionRequestDetectRegion();

            ADetectPedestrianIntrusionRequestDetectRegionRect rect = new ADetectPedestrianIntrusionRequestDetectRegionRect();
            rect.setLeft(detectRegionDTO.getLeft());
            rect.setRight(detectRegionDTO.getRight());
            rect.setTop(detectRegionDTO.getTop());
            rect.setBottom(detectRegionDTO.getBottom());
            detectRegion.setRect(rect);
            detectRegionList.add(detectRegion);
        }

        ApiR<DetectPedestrianIntrusionResponse> aR = aDetectPedestrianIntrusionHandle.handle(request);
        if (aR.isSuccess() && isSuccessStatusCode(aR.getData().getStatusCode())) {
            DetectPedestrianIntrusionResponseBody responseBody = aR.getData().getBody();
            DetectPedestrianIntrusionResponseBody.DetectPedestrianIntrusionResponseBodyData responseBodyData = responseBody.getData();
            if (CollectionUtils.isNotEmpty(responseBodyData.getElements())) {
                for (DetectPedestrianIntrusionResponseBody.DetectPedestrianIntrusionResponseBodyDataElements element : responseBodyData.getElements()) {
                    resp.setType(element.getType());
                    resp.setScore(element.getScore());
                    resp.setIsIntrude(element.getIsIntrude());
                    DataElementsBox dataElementsBox = new DataElementsBox();
                    DetectPedestrianIntrusionResponseBody.DetectPedestrianIntrusionResponseBodyDataElementsBox box = element.getBox();

                    dataElementsBox.setBottom(box.getBottom());
                    dataElementsBox.setTop(box.getTop());
                    dataElementsBox.setLeft(box.getLeft());
                    dataElementsBox.setRight(box.getRight());
                    resp.setBox(dataElementsBox);
                    return R.ok().setData(resp);
                }
            }
        }
        return R.error(aR.getErrorMsg());
    }
}
