package com.heyu.api.controller.imageenhan;


import com.aliyun.imageenhan20190930.models.ImageBlindCharacterWatermarkResponse;
import com.aliyun.imageenhan20190930.models.ImageBlindCharacterWatermarkResponseBody;
import com.aliyun.imageenhan20190930.models.ImageBlindPicWatermarkResponse;
import com.aliyun.imageenhan20190930.models.ImageBlindPicWatermarkResponseBody;
import com.heyu.api.alibaba.handle.imageenhan.AImageDecodeBlindCharacterWatermarkHandle;
import com.heyu.api.alibaba.handle.imageenhan.AImageEncodeBlindPicWatermarkHandle;
import com.heyu.api.alibaba.request.imageenhan.AImageDecodeBlindCharacterWatermarkRequest;
import com.heyu.api.alibaba.request.imageenhan.AImageEncodeBlindPicWatermarkRequest;
import com.heyu.api.controller.BaseController;
import com.heyu.api.data.annotation.CacheResult;
import com.heyu.api.data.annotation.NotIntercept;
import com.heyu.api.data.utils.ApiR;
import com.heyu.api.data.utils.R;
import com.heyu.api.request.imageenhan.ImageBlindCharacterWatermarkReq;
import com.heyu.api.resp.imageenhan.ImageBlindCharacterWatermarkResp;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/***
 * https://next.api.aliyun.com/api/imageenhan/2019-09-30/ImageBlindCharacterWatermark
 *
 * 图像隐形文字水印
 *
 * ImageBlindCharacterWatermark
 *
 *
 */
@Slf4j
@RestController
@RequestMapping("/image")
@NotIntercept
public class ImageBlindCharacterWatermarkController extends BaseController {

    @Autowired
    private AImageEncodeBlindPicWatermarkHandle aImageEncodeBlindPicWatermarkHandle;

    @Autowired
    private AImageDecodeBlindCharacterWatermarkHandle aImageDecodeBlindCharacterWatermarkHandle;

    @RequestMapping("/characterWatermark")
    @CacheResult
    public R characterWatermark(ImageBlindCharacterWatermarkReq req) {
        ImageBlindCharacterWatermarkResp resp = new ImageBlindCharacterWatermarkResp();
        // 图片加密
        if ("encode".equals(req.getType())) {
            AImageEncodeBlindPicWatermarkRequest aImageEncodeBlindPicWatermarkRequest = new AImageEncodeBlindPicWatermarkRequest();
            aImageEncodeBlindPicWatermarkRequest.setFunctionType("encode_pic_plus");
            aImageEncodeBlindPicWatermarkRequest.setWatermarkUrl(req.getImageUrl());
            aImageEncodeBlindPicWatermarkRequest.setWatermarkBase64(req.getImageBase64());
            aImageEncodeBlindPicWatermarkRequest.setQualityFactor(req.getQualityFactor());
            ApiR<ImageBlindPicWatermarkResponse> aR = aImageEncodeBlindPicWatermarkHandle.handle(aImageEncodeBlindPicWatermarkRequest);
            if (aR.isSuccess() && isSuccessStatusCode(aR.getData().getStatusCode())) {
                ImageBlindPicWatermarkResponseBody.ImageBlindPicWatermarkResponseBodyData responseBodyData = aR.getData().getBody().getData();
                resp.setImageURL(responseBodyData.getWatermarkImageURL());
                return R.ok().setData(resp);
            }
            return R.error(aR.getErrorMsg());

            // 图片解密
        } else if ("decode".equals(req.getType())) {
            AImageDecodeBlindCharacterWatermarkRequest aImageDecodeBlindCharacterWatermarkRequest = new AImageDecodeBlindCharacterWatermarkRequest();
            aImageDecodeBlindCharacterWatermarkRequest.setImageBase64(req.getImageBase64());
            aImageDecodeBlindCharacterWatermarkRequest.setImageUrl(req.getImageUrl());
            ApiR<ImageBlindCharacterWatermarkResponse> ar = aImageDecodeBlindCharacterWatermarkHandle.handle(aImageDecodeBlindCharacterWatermarkRequest);
            if (ar.isSuccess() && isSuccessStatusCode(ar.getData().getStatusCode())) {
                ImageBlindCharacterWatermarkResponseBody.ImageBlindCharacterWatermarkResponseBodyData responseBodyData = ar.getData().getBody().getData();
                resp.setImageURL(responseBodyData.getTextImageURL());
                return R.ok().setData(resp);
            }
        }
        return R.error();
    }
}





















