package com.heyu.api.controller.objectdet;


import com.aliyun.objectdet20191230.models.DetectObjectResponse;
import com.aliyun.objectdet20191230.models.DetectObjectResponseBody;
import com.heyu.api.alibaba.handle.objectdet.ADetectObjectHandle;
import com.heyu.api.alibaba.request.objectdet.ADetectObjectRequest;
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.DetectObjectReq;
import com.heyu.api.resp.imageseg.DetectObjectResp;
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/DetectObject?tab=DOC
 *
 *物体检测
 *
 * DetectObject
 *
 *
 * 功能描述
 * 物体检测能力可以检测输入图像中的物体。 关于该接口功能的示例图如下：
 *
 *
 */
@Slf4j
@RestController
@RequestMapping("/object")
@NotIntercept
public class DetectObjectController extends BaseController {


    @Autowired
    private ADetectObjectHandle aDetectObjectHandle;


    @RequestMapping("/detect")
    public R ipc(DetectObjectReq req) {

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

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


        ApiR<DetectObjectResponse> aR = aDetectObjectHandle.handle(request);
        if (aR.isSuccess() && isSuccessStatusCode(aR.getData().getStatusCode())) {

            DetectObjectResponseBody.DetectObjectResponseBodyData responseBodyData = aR.getData().getBody().getData();
            if (CollectionUtils.isNotEmpty(responseBodyData.getElements())) {
                for (DetectObjectResponseBody.DetectObjectResponseBodyDataElements element : responseBodyData.getElements()) {

                    DetectObjectResp detectObjectResp = new DetectObjectResp();
                    detectObjectResp.setType(element.getType());
                    detectObjectResp.setBoxes(element.getBoxes());
                    detectObjectResp.setType(element.getType());

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


        }


        return R.error();
    }

}
