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


import com.aliyun.imageenhan20190930.models.ExtendImageStyleRequest;
import com.aliyun.imageenhan20190930.models.ExtendImageStyleResponse;
import com.aliyun.teautil.models.RuntimeOptions;
import com.heyu.api.data.annotation.CustomPath;
import com.heyu.api.data.utils.StringUtils;
import com.heyu.api.alibaba.AlibabaBaseHandle;
import com.heyu.api.alibaba.request.imageenhan.AExtendImageStyleRequest;
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/ExtendImageStyle?lang=JAVA&RegionId=cn-shanghai&tab=DOC
 * <p>
 * 风格迁移
 * <p>
 * ExtendImageStyle
 */
@Component
@Slf4j
@CustomPath("extendImageStyle")
public class AExtendImageStyleHandle extends AlibabaBaseHandle<AExtendImageStyleRequest, ExtendImageStyleResponse> {


    @Autowired
    private OssFileUploadService ossFileUploadService;

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

    @Override
    public String check(AExtendImageStyleRequest aExtendImageStyleRequest) {
        if (StringUtils.isBlank(aExtendImageStyleRequest.getStyleUrl())
                && StringUtils.isBlank(aExtendImageStyleRequest.getStyleBase64())) {
            return "styleUrl和styleBase64不能同时为空";
        }

        if (StringUtils.isBlank(aExtendImageStyleRequest.getMajorUrl())
                && StringUtils.isBlank(aExtendImageStyleRequest.getMajorBase64())) {
            return "majorUrl和majorBase64不能同时为空";
        }

        if (StringUtils.isBlank(aExtendImageStyleRequest.getStyleBase64())
                && StringUtils.isNotBlank(aExtendImageStyleRequest.getStyleBase64())) {
            String url = ossFileUploadService.uploadFileContentByBase64(aExtendImageStyleRequest.getStyleBase64(), "jpg");
            if (StringUtils.isNotBlank(url)) {
                aExtendImageStyleRequest.setStyleUrl(url);
            } else {
                return "您上传的styleBase64数据格式不正确";
            }
        }
        if (StringUtils.isBlank(aExtendImageStyleRequest.getMajorUrl())
                && StringUtils.isNotBlank(aExtendImageStyleRequest.getMajorBase64())) {
            String url = ossFileUploadService.uploadFileContentByBase64(aExtendImageStyleRequest.getMajorBase64(), "jpg");
            if (StringUtils.isNotBlank(url)) {
                aExtendImageStyleRequest.setMajorUrl(url);
            } else {
                return "您上传的majorBase64数据格式不正确";
            }
        }
        return null;
    }


    @Override
    public ExtendImageStyleResponse run(AExtendImageStyleRequest aExtendImageStyleRequest, RuntimeOptions runtime) throws Exception {
        ExtendImageStyleRequest extendImageStyleRequest = new ExtendImageStyleRequest();
        extendImageStyleRequest.setStyleUrl(aExtendImageStyleRequest.getStyleUrl());
        extendImageStyleRequest.setMajorUrl(aExtendImageStyleRequest.getMajorUrl());

        // 复制代码运行请自行打印 API 的返回值
        return client.extendImageStyleWithOptions(extendImageStyleRequest, runtime);
    }
}
