package com.heyu.api.controller.objectdet;


import com.aliyun.objectdet20191230.models.DetectWhiteBaseImageResponse;
import com.aliyun.objectdet20191230.models.DetectWhiteBaseImageResponseBody;
import com.heyu.api.alibaba.handle.objectdet.ADetectWhiteBaseImageHandle;
import com.heyu.api.alibaba.request.objectdet.ADetectWhiteBaseImageRequest;
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.DetectWhiteBaseImageReq;
import com.heyu.api.resp.objectdet.DetectWhiteBaseImageResp;
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;

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 DetectWhiteBaseImageController extends BaseController {


    @Autowired
    private ADetectWhiteBaseImageHandle aDetectWhiteBaseImageHandle;

    @RequestMapping("/detectWhiteBase")
    public R ipc(DetectWhiteBaseImageReq req) {

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

        ADetectWhiteBaseImageRequest aDetectWhiteBaseImageRequest = new ADetectWhiteBaseImageRequest();
        aDetectWhiteBaseImageRequest.setImageBase64(req.getImageBase64());
        aDetectWhiteBaseImageRequest.setImageUrl(req.getImageUrl());

        ApiR<DetectWhiteBaseImageResponse> aR = aDetectWhiteBaseImageHandle.handle(aDetectWhiteBaseImageRequest);
        if (aR.isSuccess() && isSuccessStatusCode(aR.getData().getStatusCode())) {
            DetectWhiteBaseImageResponseBody.DetectWhiteBaseImageResponseBodyData responseBodyData = aR.getData().getBody().getData();
            for (DetectWhiteBaseImageResponseBody.DetectWhiteBaseImageResponseBodyDataElements element : responseBodyData.getElements()) {

                DetectWhiteBaseImageResp resp = new DetectWhiteBaseImageResp();
                resp.setWhiteBase(element.getWhiteBase());
                respList.add(resp);
            }
            return R.ok().setData(respList);
        }
        return R.error(aR.getErrorMsg());
    }

}
