package com.heyu.api.controller.imageenhan;


import com.aliyun.imageenhan20190930.models.ChangeImageSizeResponse;
import com.aliyun.imageenhan20190930.models.ChangeImageSizeResponseBody;
import com.heyu.api.alibaba.handle.imageenhan.AChangeImageSizeHandle;
import com.heyu.api.alibaba.request.imageenhan.AChangeImageSizeRequest;
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.imageenhan.ChangeImageSizeReq;
import com.heyu.api.resp.imageenhan.ChangeImageSizeResp;
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/imageenhan/2019-09-30/ChangeImageSize?tab=DOC&lang=JAVA&RegionId=cn-shanghai
 *
 *
 *图像裁剪
 *
 * ChangelmageSize
 *
 *
 * 功能描述
 * 图像裁剪能力可以对输入的图像按照指定尺寸变换。支持自动判断主体区域位置，使用更好的裁剪方式对图像进行裁剪。
 *
 *
 */
@Slf4j
@RestController
@RequestMapping("/image")
@NotIntercept
public class ChangeImageSizeController extends BaseController {


    @Autowired
    private AChangeImageSizeHandle aChangeImageSizeHandle;


    @RequestMapping("/changeSize")
    @CacheResult
    public R changeSize(ChangeImageSizeReq req) {
        ChangeImageSizeResp resp = new ChangeImageSizeResp();

        AChangeImageSizeRequest aChangeImageSizeRequest = new AChangeImageSizeRequest();
        aChangeImageSizeRequest.setImageBase64(req.getImageBase64());
        aChangeImageSizeRequest.setWidth(req.getWidth());
        aChangeImageSizeRequest.setHeight(req.getHeight());
        aChangeImageSizeRequest.setImageUrl(req.getImageUrl());

        ApiR<ChangeImageSizeResponse> aR = aChangeImageSizeHandle.handle(aChangeImageSizeRequest);
        if (aR.isSuccess() && isSuccessStatusCode(aR.getData().getStatusCode())) {

            ChangeImageSizeResponseBody.ChangeImageSizeResponseBodyData responseBodyData = aR.getData().getBody().getData();

            resp.setImageURL(responseBodyData.getUrl());
            return R.ok().setData(resp);
        }
        return R.error();
    }
}
