package com.heyu.api.controller.imageenhan;


import com.aliyun.imageenhan20190930.models.ImageBlindPicWatermarkResponse;
import com.aliyun.imageenhan20190930.models.ImageBlindPicWatermarkResponseBody;
import com.heyu.api.alibaba.handle.imageenhan.AImageDecodeBlindPicWatermarkHandle;
import com.heyu.api.alibaba.handle.imageenhan.AImageEncodeBlindPicWatermarkHandle;
import com.heyu.api.alibaba.request.imageenhan.AImageDecodeBlindPicWatermarkRequest;
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.ImageBlindPicWatermarkReq;
import com.heyu.api.resp.imageenhan.ImageBlindPicWatermarkResp;
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/ImageBlindPicWatermark
 *
 * 图像隐形图片水印
 *
 * ImageBlindPicWatermark
 *
 */
@Slf4j
@RestController
@RequestMapping("/image")
@NotIntercept
public class ImageBlindPicWatermarkController extends BaseController {

    @Autowired
    private AImageEncodeBlindPicWatermarkHandle aImageEncodeBlindPicWatermarkHandle;


    @Autowired
    private AImageDecodeBlindPicWatermarkHandle aImageDecodeBlindPicWatermarkHandle;

    @RequestMapping("/picWatermark")
    @CacheResult
    public R scan(ImageBlindPicWatermarkReq req) {
        ImageBlindPicWatermarkResp resp  = new ImageBlindPicWatermarkResp();

        if ("encode".equals(req.getType())) {
            AImageEncodeBlindPicWatermarkRequest aImageEncodeBlindPicWatermarkRequest = new AImageEncodeBlindPicWatermarkRequest();
            aImageEncodeBlindPicWatermarkRequest.setImageUrl(req.getImageUrl());
            aImageEncodeBlindPicWatermarkRequest.setImageBase64(req.getImageBase64());
            aImageEncodeBlindPicWatermarkRequest.setWatermarkUrl(req.getEncodeImageUrl());
            aImageEncodeBlindPicWatermarkRequest.setWatermarkBase64(req.getEncodeImageBase64());
            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(responseBodyData);
            }
            return R.error(aR.getErrorMsg());
        } else if ("decode".equals(req.getType())) {
            AImageDecodeBlindPicWatermarkRequest aImageDecodeBlindPicWatermarkRequest = new AImageDecodeBlindPicWatermarkRequest();
            aImageDecodeBlindPicWatermarkRequest.setImageUrl(req.getImageUrl());
            aImageDecodeBlindPicWatermarkRequest.setImageBase64(req.getImageBase64());
            ApiR<ImageBlindPicWatermarkResponse> aR = aImageDecodeBlindPicWatermarkHandle.handle(aImageDecodeBlindPicWatermarkRequest);
            if(aR.isSuccess() && isSuccessStatusCode(aR.getData().getStatusCode())) {
                ImageBlindPicWatermarkResponseBody.ImageBlindPicWatermarkResponseBodyData responseBodyData = aR.getData().getBody().getData();
                resp.setImageURL(responseBodyData.getLogoURL());
                return R.ok().setData(responseBodyData);
            }
            return R.error(aR.getErrorMsg());
        }
        return R.error();
    }
}
