package com.heyu.api.alibaba.handle.imageenhan;


import com.aliyun.imageenhan20190930.models.ChangeImageSizeRequest;
import com.aliyun.imageenhan20190930.models.ChangeImageSizeResponse;
import com.aliyun.teautil.models.RuntimeOptions;
import com.heyu.api.data.annotation.CustomPath;
import com.heyu.api.alibaba.AlibabaBaseHandle;
import com.heyu.api.alibaba.request.imageenhan.AChangeImageSizeRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * https://next.api.aliyun.com/api/imageenhan/2019-09-30/ChangeImageSize?tab=DOC&lang=JAVA
 * <p>
 * 图像裁剪
 * <p>
 * ChangelmageSize
 */
@Component
@Slf4j
@CustomPath("changeImageSize")
public class AChangeImageSizeHandle extends AlibabaBaseHandle<AChangeImageSizeRequest, ChangeImageSizeResponse> {


    @Autowired
    private com.aliyun.imageenhan20190930.Client client;

    @Override
    public String check(AChangeImageSizeRequest aChangeImageSizeRequest) {
        if (aChangeImageSizeRequest.getWidth() == null) {
            return "请输入width";
        }
        if (aChangeImageSizeRequest.getWidth() <= 0 && aChangeImageSizeRequest.getWidth() > 2000) {
            return "width的取值范围[0,2000]";
        }

        if (aChangeImageSizeRequest.getHeight() == null) {
            return "请输入height";
        }
        if (aChangeImageSizeRequest.getHeight() <= 0 && aChangeImageSizeRequest.getHeight() > 2000) {
            return "height的取值范围[0,2000]";
        }
        return checkUrlBase64(aChangeImageSizeRequest);
    }

    @Override
    public ChangeImageSizeResponse run(AChangeImageSizeRequest aChangeImageSizeRequest, RuntimeOptions runtime) throws Exception {

        ChangeImageSizeRequest changeImageSizeRequest = new ChangeImageSizeRequest();
        changeImageSizeRequest.setWidth(aChangeImageSizeRequest.getWidth());
        changeImageSizeRequest.setHeight(aChangeImageSizeRequest.getHeight());
        changeImageSizeRequest.setUrl(aChangeImageSizeRequest.getImageUrl());
        // 复制代码运行请自行打印 API 的返回值
        return client.changeImageSizeWithOptions(changeImageSizeRequest, runtime);
    }
}
