package com.heyu.api.controller.imageseg;


import com.aliyun.imageseg20191230.models.SegmentBodyResponse;
import com.aliyun.imageseg20191230.models.SegmentBodyResponseBody;
import com.heyu.api.alibaba.handle.imageseg.ASegmentBodyHandle;
import com.heyu.api.alibaba.request.imageseg.ASegmentBodyRequest;
import com.heyu.api.controller.BaseController;
import com.heyu.api.data.annotation.CacheResult;
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.imageseg.SegmentBodyReq;
import com.heyu.api.resp.imageseg.SegmentBodyResp;
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/imageseg/2019-12-30/SegmentBody?tab=DOC&RegionId=cn-shanghai
 *
 *人体分割
 *
 * SegmentBody
 *
 *
 * 功能描述
 * 人体分割能力用于识别输入图像中的人体轮廓，与背景进行分离，返回分割后的前景人像图（4 通道）。本功能适用于真人图片，不适用于卡通图片。 关于该接口功能的示例图如下：
 *
 */
@Slf4j
@RestController
@RequestMapping("/body")
@NotIntercept
public class SegmentBodyController extends BaseController {


    @Autowired
    private ASegmentBodyHandle segmentBodyHandle;

    @RequestMapping("/segment")
    @CacheResult
    public R changeSky(SegmentBodyReq request) {
        SegmentBodyResp resp = new SegmentBodyResp();

        ASegmentBodyRequest aSegmentBodyRequest = new ASegmentBodyRequest();
        aSegmentBodyRequest.setReturnType(request.getReturnForm());
        aSegmentBodyRequest.setImageBase64(request.getImageBase64());
        aSegmentBodyRequest.setImageUrl(request.getImageUrl());


        ApiR<SegmentBodyResponse> aR = segmentBodyHandle.handle(aSegmentBodyRequest);
        if (aR.isSuccess() && isSuccessStatusCode(aR.getData().getStatusCode())) {
            SegmentBodyResponseBody.SegmentBodyResponseBodyData responseBodyData = aR.getData().getBody().getData();
            resp.setImageURL(responseBodyData.getImageURL());
            return R.ok().setData(resp);
        }


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