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


import com.aliyun.imageseg20191230.models.ChangeSkyRequest;
import com.aliyun.imageseg20191230.models.ChangeSkyResponse;
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.imageseg.AChangeSkyRequest;
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/imageseg/2019-12-30/ParseFace?tab=DEMO&lang=JAVA
 * <p>
 * <p>
 * 天空替换 ChangeSky
 */
@Component
@Slf4j
@CustomPath("changeSky")
public class AChangeSkyHandle extends AlibabaBaseHandle<AChangeSkyRequest, ChangeSkyResponse> {


    @Autowired
    private com.aliyun.imageseg20191230.Client client;
    @Autowired
    private OssFileUploadService ossFileUploadService;

    @Override
    public String check(AChangeSkyRequest aChangeSkyRequest) {
        if (StringUtils.isBlank(aChangeSkyRequest.getReplaceImageURL())
                && StringUtils.isBlank(aChangeSkyRequest.getReplaceImageBase64())) {
            return "replaceImageURL 和 replaceImageBase64 不能同时为空";
        }
        if (StringUtils.isBlank(aChangeSkyRequest.getReplaceImageURL())
                && StringUtils.isNotBlank(aChangeSkyRequest.getReplaceImageBase64())) {
            String url = ossFileUploadService.uploadFileContentByBase64(aChangeSkyRequest.getReplaceImageBase64(), ApiConstants.JPG);
            if (StringUtils.isNotBlank(url)) {
                aChangeSkyRequest.setReplaceImageURL(url);
            } else {
                return "replaceImageBase64 base64格式不正确";
            }
        }
        return null;
    }





    @Override
    public ChangeSkyResponse run(AChangeSkyRequest aChangeSkyRequest, RuntimeOptions runtime) throws Exception {
        ChangeSkyRequest changeSkyRequest = new ChangeSkyRequest();
        changeSkyRequest.setReplaceImageURL(aChangeSkyRequest.getReplaceImageURL());
        changeSkyRequest.setImageURL(aChangeSkyRequest.getImageUrl());
        // 复制代码运行请自行打印 API 的返回值
        return client.changeSkyWithOptions(changeSkyRequest, runtime);
    }
}
