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


import com.aliyun.imageenhan20190930.models.ImitatePhotoStyleRequest;
import com.aliyun.imageenhan20190930.models.ImitatePhotoStyleResponse;
import com.aliyun.teautil.models.RuntimeOptions;
import com.heyu.api.data.annotation.CustomPath;
import com.heyu.api.data.constants.ApiConstants;
import com.heyu.api.data.utils.StringUtils;
import com.heyu.api.alibaba.AlibabaBaseHandle;
import com.heyu.api.alibaba.request.imageenhan.AImitatePhotoStyleRequest;
import com.heyu.api.oss.OssFileUploadService;
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/ImitatePhotoStyle?tab=DEMO&lang=JAVA
 * <p>
 *照图修图
 *
 * ImitatePhotoStyle
 */
@Component
@Slf4j
@CustomPath("imitatePhotoStyle")
public class AImitatePhotoStyleHandle extends AlibabaBaseHandle<AImitatePhotoStyleRequest, ImitatePhotoStyleResponse> {

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


    @Autowired
    private OssFileUploadService ossFileUploadService;


    @Override
    public String check(AImitatePhotoStyleRequest aImitatePhotoStyleRequest) {
        if (StringUtils.isBlank(aImitatePhotoStyleRequest.getStyleUrl())
                && StringUtils.isBlank(aImitatePhotoStyleRequest.getStyleBase64())) {
            return "styleUrl 和 styleBase64 不能同时为空";
        }
        if (StringUtils.isBlank(aImitatePhotoStyleRequest.getStyleUrl())
                && StringUtils.isNotBlank(aImitatePhotoStyleRequest.getStyleBase64())) {
            String url = ossFileUploadService.uploadFileContentByBase64(aImitatePhotoStyleRequest.getStyleBase64(), ApiConstants.JPG);
            if (StringUtils.isNotBlank(url)) {
                aImitatePhotoStyleRequest.setStyleUrl(url);
            } else {
                return "styleBase64 base64编码格式不正确";
            }
        }
        return checkUrlBase64(aImitatePhotoStyleRequest);
    }


    @Override
    public ImitatePhotoStyleResponse run(AImitatePhotoStyleRequest aImitatePhotoStyleRequest, RuntimeOptions runtime) throws Exception {
        ImitatePhotoStyleRequest imitatePhotoStyleRequest = new ImitatePhotoStyleRequest();
        imitatePhotoStyleRequest.setImageURL(aImitatePhotoStyleRequest.getImageUrl());
        imitatePhotoStyleRequest.setStyleUrl(aImitatePhotoStyleRequest.getStyleUrl());
        // 复制代码运行请自行打印 API 的返回值
        return client.imitatePhotoStyleWithOptions(imitatePhotoStyleRequest, runtime);
    }
}
