package com.heyu.api.controller.imageseg;


import com.aliyun.imageseg20191230.models.SegmentSkinResponse;
import com.aliyun.imageseg20191230.models.SegmentSkinResponseBody;
import com.heyu.api.alibaba.handle.imageseg.ASegmentSkinHandle;
import com.heyu.api.alibaba.request.imageseg.ASegmentSkinRequest;
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.imagerecog.SegmentSkinReq;
import com.heyu.api.resp.imageseg.SegmentSkinResp;
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/SegmentSkin?tab=DEMO&lang=JAVA
 * <p>
 * 皮肤分割
 * <p>
 * SegmentSkin
 */
@Slf4j
@RestController
@RequestMapping("/skin")
@NotIntercept
public class SegmentSkinController extends BaseController {


    @Autowired
    private ASegmentSkinHandle segmentSkinHandle;


    @RequestMapping("/segment")
    @CacheResult
    public R segment(SegmentSkinReq request) {
        SegmentSkinResp resp = new SegmentSkinResp();

        ASegmentSkinRequest segmentSkinRequest = new ASegmentSkinRequest();
        segmentSkinRequest.setImageBase64(request.getImageBase64());
        segmentSkinRequest.setImageUrl(request.getImageUrl());

        ApiR<SegmentSkinResponse> aR = segmentSkinHandle.handle(segmentSkinRequest);
        if (aR.isSuccess() && isSuccessStatusCode(aR.getData().getStatusCode())) {
            SegmentSkinResponseBody.SegmentSkinResponseBodyData responseBodyData = aR.getData().getBody().getData();
            resp.setImageURL(responseBodyData.getURL());
            return R.ok().setData(resp);
        }
        return R.error(aR.getErrorMsg());
    }
}
