package com.heyu.api.controller.objectdet;


import com.aliyun.objectdet20191230.models.DetectMainBodyResponse;
import com.aliyun.objectdet20191230.models.DetectMainBodyResponseBody;
import com.heyu.api.alibaba.handle.objectdet.ADetectMainBodyHandle;
import com.heyu.api.alibaba.request.objectdet.ADetectMainBodyRequest;
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.DetectMainBodyReq;
import com.heyu.api.resp.imageseg.DetectMainBodyResp;
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;

/**
 * https://next.api.aliyun.com/api/objectdet/2019-12-30/DetectMainBody?tab=DEMO&lang=JAVA
 * <p>
 * 主体检测
 * <p>
 * DetectMainBody
 */

@Slf4j
@RestController
@RequestMapping("/main/body")
@NotIntercept
public class DetectMainBodyController extends BaseController {


    @Autowired
    private ADetectMainBodyHandle aDetectMainBodyHandle;


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


        DetectMainBodyResp detectMainBodyResp = new DetectMainBodyResp();
        ADetectMainBodyRequest aDetectMainBodyRequest = new ADetectMainBodyRequest();
        aDetectMainBodyRequest.setImageBase64(req.getImageBase64());
        aDetectMainBodyRequest.setImageUrl(req.getImageUrl());

        ApiR<DetectMainBodyResponse> aR = aDetectMainBodyHandle.handle(aDetectMainBodyRequest);

        if (aR.isSuccess() && isSuccessStatusCode(aR.getData().getStatusCode())) {

            DetectMainBodyResponseBody.DetectMainBodyResponseBodyData responseBodyData = aR.getData().getBody().getData();
            DetectMainBodyResponseBody.DetectMainBodyResponseBodyDataLocation responseBodyDataLocation = responseBodyData.getLocation();

            detectMainBodyResp.setX(responseBodyDataLocation.getX());
            detectMainBodyResp.setY(responseBodyDataLocation.getY());
            detectMainBodyResp.setWidth(responseBodyDataLocation.getWidth());
            detectMainBodyResp.setHeight(responseBodyDataLocation.getHeight());
            return R.ok().setData(detectMainBodyResp);

        }

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


}
