package com.heyu.api.controller.objectdet;


import com.aliyun.objectdet20191230.models.DetectIPCObjectResponse;
import com.aliyun.objectdet20191230.models.DetectIPCObjectResponseBody;
import com.heyu.api.alibaba.handle.objectdet.ADetectIPCObjectHandle;
import com.heyu.api.alibaba.request.objectdet.ADetectIPCObjectRequest;
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.objectdet.DetectIPCObjectReq;
import com.heyu.api.resp.objectdet.DetectIPCObjectResp;
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/objectdet/2019-12-30/DetectIPCObject?tab=DOC
 * <p>
 * IPC图像目标检测
 * <p>
 * DetectIPCObject
 *
 * 应用场景
 * 智能看家：家中有人或宠物移动时可生成高精度视频，实现检测提醒功能。且可以关注宠物在家中的活动状况。
 * 智能看店：可对门店起到安防作用，尤其是在夜间，可及时知道有人进店。
 * 智能看厂：安全是厂区非常重要的事情，对于人员在岗以及人员入侵可以起到良好的监管作用。
 * 特色优势
 * 适用场景多：适应多种广泛的日常场景的目标检测，室内、室外、家居、店铺、商场、白天、夜间等。
 * 检测范围广：可检测的目标大小范围广，适用于大目标检测，也适用于较远摄像头下的小目标检测。
 * 检测效率高：高并发处理，处理时间短。
 */
@Slf4j
@RestController
@RequestMapping("/object")
@NotIntercept
public class DetectIPCObjectController extends BaseController {


    @Autowired
    private ADetectIPCObjectHandle aDetectIPCObjectHandle;


    @RequestMapping("/ipc")
    public R ipc(DetectIPCObjectReq req) {

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

        ADetectIPCObjectRequest aDetectIPCObjectRequest = new ADetectIPCObjectRequest();
        aDetectIPCObjectRequest.setImageBase64(req.getImageBase64());
        aDetectIPCObjectRequest.setImageUrl(req.getImageUrl());


        ApiR<DetectIPCObjectResponse> aR = aDetectIPCObjectHandle.handle(aDetectIPCObjectRequest);
        if (aR.isSuccess() && isSuccessStatusCode(aR.getData().getStatusCode())) {
            DetectIPCObjectResponseBody.DetectIPCObjectResponseBodyData responseBodyData = aR.getData().getBody().getData();
            if (CollectionUtils.isNotEmpty(responseBodyData.getElements())) {
                for (DetectIPCObjectResponseBody.DetectIPCObjectResponseBodyDataElements element : responseBodyData.getElements()) {

                    DetectIPCObjectResp resp = new DetectIPCObjectResp();
                    resp.setScore(element.getScore());
                    resp.setType(element.getType());

                    respList.add(resp);
                }

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

        }
        return R.error(aR.getErrorMsg());
    }
}
