package com.heyu.api.controller.imageenhan;

import com.aliyun.imageenhan20190930.models.GenerateSuperResolutionImageResponse;
import com.aliyun.imageenhan20190930.models.GenerateSuperResolutionImageResponseBody;
import com.heyu.api.alibaba.handle.imageenhan.AGenerateSuperResolutionImageHandle;
import com.heyu.api.alibaba.request.imageenhan.AGenerateSuperResolutionImageRequest;
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.GenerateSuperResolutionImageReq;
import com.heyu.api.resp.imageenhan.GenerateSuperResolutionImageResp;
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;


@Slf4j
@RestController
@RequestMapping("/image")
@NotIntercept
public class GenerateSuperResolutionImageController extends BaseController {

    @Autowired
    private AGenerateSuperResolutionImageHandle generateSuperResolutionImageHandle;

    @RequestMapping("/superScore")
    @CacheResult
    public R superScore(GenerateSuperResolutionImageReq req) {

        GenerateSuperResolutionImageResp resp = new GenerateSuperResolutionImageResp();

        AGenerateSuperResolutionImageRequest request = new AGenerateSuperResolutionImageRequest();
        request.setImageUrl(req.getImageUrl());
        request.setImageBase64(req.getImageBase64());
        request.setScale(req.getScale());
        request.setOutputQuality(req.getOutputQuality());

        ApiR<GenerateSuperResolutionImageResponse> aR = generateSuperResolutionImageHandle.handle(request);
        if (aR.isSuccess() && isSuccessStatusCode(aR.getData().getStatusCode())) {
            GenerateSuperResolutionImageResponseBody.GenerateSuperResolutionImageResponseBodyData responseBodyData = aR.getData().getBody().getData();
            resp.setImageURL(responseBodyData.getResultUrl());
            return R.ok().setData(resp);
        }
        return R.error(aR.getErrorMsg());
    }
}
