package com.heyu.api.alibaba.handle.facebody;


import com.aliyun.facebody20191230.models.DetectPedestrianIntrusionRequest;
import com.aliyun.facebody20191230.models.DetectPedestrianIntrusionResponse;
import com.aliyun.teautil.models.RuntimeOptions;
import com.heyu.api.data.annotation.CustomPath;
import com.heyu.api.alibaba.AlibabaBaseHandle;
import com.heyu.api.alibaba.request.facebody.ADetectPedestrianIntrusionRequest;
import com.heyu.api.alibaba.request.facebody.ADetectPedestrianIntrusionRequestDetectRegion;
import com.heyu.api.alibaba.request.facebody.ADetectPedestrianIntrusionRequestDetectRegionLine;
import com.heyu.api.alibaba.request.facebody.ADetectPedestrianIntrusionRequestDetectRegionRect;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

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

/**
 * https://next.api.aliyun.com/api/facebody/2019-12-30/DetectLivingFace
 * 行人入侵检测
 */
@Component
@Slf4j
@CustomPath("detectPedestrianIntrusion")
public class ADetectPedestrianIntrusionHandle extends AlibabaBaseHandle<ADetectPedestrianIntrusionRequest, DetectPedestrianIntrusionResponse> {

    @Autowired
    private com.aliyun.facebody20191230.Client client;

    @Override
    public String check(ADetectPedestrianIntrusionRequest aDetectPedestrianIntrusionRequest) {
        return checkUrlBase64(aDetectPedestrianIntrusionRequest);
    }



    @Override
    public DetectPedestrianIntrusionResponse run(ADetectPedestrianIntrusionRequest aDetectPedestrianIntrusionRequest, RuntimeOptions runtime) throws Exception {

        DetectPedestrianIntrusionRequest detectPedestrianIntrusionRequest = new DetectPedestrianIntrusionRequest();
        detectPedestrianIntrusionRequest.setImageURL(aDetectPedestrianIntrusionRequest.getImageUrl());
        List<DetectPedestrianIntrusionRequest.DetectPedestrianIntrusionRequestDetectRegion> detectRegion = new ArrayList<>();
        for (ADetectPedestrianIntrusionRequestDetectRegion requestDetectRegion : aDetectPedestrianIntrusionRequest.getDetectRegion()) {
            ADetectPedestrianIntrusionRequestDetectRegionLine line = requestDetectRegion.getLine();
            ADetectPedestrianIntrusionRequestDetectRegionRect rect = requestDetectRegion.getRect();

            DetectPedestrianIntrusionRequest.DetectPedestrianIntrusionRequestDetectRegion detectPedestrianIntrusionRequestDetectRegion = new DetectPedestrianIntrusionRequest.DetectPedestrianIntrusionRequestDetectRegion();

            DetectPedestrianIntrusionRequest.DetectPedestrianIntrusionRequestDetectRegionLine requestDetectRegionLine = new DetectPedestrianIntrusionRequest.DetectPedestrianIntrusionRequestDetectRegionLine();
            requestDetectRegionLine.setX1(line.getX1());
            requestDetectRegionLine.setY1(line.getY1());
            requestDetectRegionLine.setX2(line.getX2());
            requestDetectRegionLine.setY2(line.getY2());

            DetectPedestrianIntrusionRequest.DetectPedestrianIntrusionRequestDetectRegionRect requestDetectRegionRect = new DetectPedestrianIntrusionRequest.DetectPedestrianIntrusionRequestDetectRegionRect();
            requestDetectRegionRect.setBottom(rect.getBottom());
            requestDetectRegionRect.setLeft(rect.getLeft());
            requestDetectRegionRect.setRight(rect.getRight());
            requestDetectRegionRect.setTop(rect.getTop());

            detectPedestrianIntrusionRequestDetectRegion.setLine(requestDetectRegionLine);
            detectPedestrianIntrusionRequestDetectRegion.setRect(requestDetectRegionRect);
            detectRegion.add(detectPedestrianIntrusionRequestDetectRegion);
        }

        // 复制代码运行请自行打印 API 的返回值
        return client.detectPedestrianIntrusionWithOptions(detectPedestrianIntrusionRequest, runtime);
    }
}
