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


import com.aliyun.imageenhan20190930.models.ErasePersonRequest;
import com.aliyun.imageenhan20190930.models.ErasePersonResponse;
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.AErasePersonRequest;
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/ErasePerson?tab=DOC&lang=JAVA&RegionId=cn-shanghai
 * 擦除
 * <p>
 * 图像人体擦除
 * <p>
 * ErasePerson
 */
@Component
@Slf4j
@CustomPath("erasePersonHandle")
public class AErasePersonHandle extends AlibabaBaseHandle<AErasePersonRequest, ErasePersonResponse> {

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

    @Autowired
    private OssFileUploadService ossFileUploadService;

    @Override
    public String check(AErasePersonRequest aErasePersonRequest) {
        if (StringUtils.isBlank(aErasePersonRequest.getMaskUrl()) && StringUtils.isBlank(aErasePersonRequest.getMaskBase64())) {
            return "maskUrl和maskBase64不能同时为空";
        }
        if (StringUtils.isBlank(aErasePersonRequest.getMaskUrl()) && StringUtils.isNotBlank(aErasePersonRequest.getMaskBase64())) {
            String url = ossFileUploadService.uploadFileContentByBase64(aErasePersonRequest.getMaskBase64(), ApiConstants.JPG);
            if (StringUtils.isNotBlank(url)) {
                aErasePersonRequest.setMaskUrl(url);
            } else {
                return "maskBase64编码有问题，请核对。";
            }
        }
        return checkUrlBase64(aErasePersonRequest);
    }



    @Override
    public ErasePersonResponse run(AErasePersonRequest aErasePersonRequest, RuntimeOptions runtime) throws Exception {
        ErasePersonRequest erasePersonRequest = new ErasePersonRequest();
        erasePersonRequest.setImageURL(aErasePersonRequest.getImageUrl());
        erasePersonRequest.setUserMask(aErasePersonRequest.getMaskUrl());

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