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


import com.aliyun.imageenhan20190930.models.ImageBlindPicWatermarkRequest;
import com.aliyun.imageenhan20190930.models.ImageBlindPicWatermarkResponse;
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.AImageEncodeBlindPicWatermarkRequest;
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/ImageBlindPicWatermark?tab=DOC&lang=JAVA
 * <p>
 * 图像隐形图片水印
 * <p>
 * ImageBlindPicWatermark
 *
 *
 *应用场景
 * 著作权保护：图片的作者依法享有著作权。在图像中增加隐形水印，可以为图片的作者或被授权人证明图片的著作权归属，避免图片在未获得作者授权的情况下被非法使用。
 * 防止信息泄露：在涉密信息的图片中，对不同的访问者打上不同的隐形水印。如果图片一旦被泄露，可以通过解析隐形水印，排查出泄密源。
 * 特色优势
 * 隐形水印与传统图章水印相比，其无法被观看者察觉，不影响图片效果。水印无法被使用者察觉，也无法通过普通的去水印方法去除，但可以通过图像隐形文字水印解析出水印，证明图片的著作权归属。
 *
 */
@Component
@Slf4j
@CustomPath("imageEncodeBlindPicWatermark")
public class AImageEncodeBlindPicWatermarkHandle extends AlibabaBaseHandle<AImageEncodeBlindPicWatermarkRequest, ImageBlindPicWatermarkResponse> {
    @Autowired
    private com.aliyun.imageenhan20190930.Client client;

    @Autowired
    private OssFileUploadService ossFileUploadService;

    @Override
    public String check(AImageEncodeBlindPicWatermarkRequest aImageEncodeBlindPicWatermarkRequest) {
        if (StringUtils.isBlank(aImageEncodeBlindPicWatermarkRequest.getWatermarkUrl())
                && StringUtils.isBlank(aImageEncodeBlindPicWatermarkRequest.getWatermarkBase64())) {
            return "watermarkUrl和watermarkBase64不能为空";
        }
        if (StringUtils.isBlank(aImageEncodeBlindPicWatermarkRequest.getWatermarkUrl())
                && StringUtils.isNotBlank(aImageEncodeBlindPicWatermarkRequest.getWatermarkBase64())) {
            String url = ossFileUploadService.uploadFileContentByBase64(aImageEncodeBlindPicWatermarkRequest.getWatermarkBase64(), "jpg");
            if (StringUtils.isNotBlank(url)) {
                aImageEncodeBlindPicWatermarkRequest.setWatermarkUrl(url);
            } else {
                return "您的watermarkBase64数据有问题";
            }
        }
        return checkUrlBase64(aImageEncodeBlindPicWatermarkRequest);
    }



    @Override
    public ImageBlindPicWatermarkResponse run(AImageEncodeBlindPicWatermarkRequest aImageEncodeBlindPicWatermarkRequest, RuntimeOptions runtime) throws Exception {
        ImageBlindPicWatermarkRequest imageBlindPicWatermarkRequest = new ImageBlindPicWatermarkRequest();
        imageBlindPicWatermarkRequest.setFunctionType(aImageEncodeBlindPicWatermarkRequest.getFunctionType());
        imageBlindPicWatermarkRequest.setOriginImageURL(aImageEncodeBlindPicWatermarkRequest.getImageUrl());
        imageBlindPicWatermarkRequest.setOutputFileType(aImageEncodeBlindPicWatermarkRequest.getOutputFileType());
        imageBlindPicWatermarkRequest.setQualityFactor(aImageEncodeBlindPicWatermarkRequest.getQualityFactor());
        imageBlindPicWatermarkRequest.setLogoURL(aImageEncodeBlindPicWatermarkRequest.getWatermarkUrl());
        // 复制代码运行请自行打印 API 的返回值
        return client.imageBlindPicWatermarkWithOptions(imageBlindPicWatermarkRequest, runtime);
    }
}
