package com.heyu.api.controller.imagerecog;


import com.aliyun.imagerecog20190930.models.RecognizeImageStyleResponse;
import com.aliyun.imagerecog20190930.models.RecognizeImageStyleResponseBody;
import com.heyu.api.alibaba.handle.imagerecog.ARecognizeImageStyleHandle;
import com.heyu.api.alibaba.request.imagerecog.ARecognizeImageStyleRequest;
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.imagerecog.RecognizeImageStyleReq;
import com.heyu.api.resp.imagerecog.RecognizeImageStyleResp;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
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/style")
@NotIntercept
public class RecognizeImageStyleController extends BaseController {


    @Autowired
    private ARecognizeImageStyleHandle arRecognizeImageStyleHandle;


    @RequestMapping("/recognize")
    @CacheResult
    public R recognize(RecognizeImageStyleReq req) {
        RecognizeImageStyleResp resp = new RecognizeImageStyleResp();
        ARecognizeImageStyleRequest request = new ARecognizeImageStyleRequest();

        request.setImageBase64(req.getImageBase64());
        request.setImageUrl(req.getImageUrl());

        ApiR<RecognizeImageStyleResponse> aR = arRecognizeImageStyleHandle.handle(request);
        if (aR.isSuccess() && isSuccessStatusCode(aR.getData().getStatusCode())) {
            RecognizeImageStyleResponseBody.RecognizeImageStyleResponseBodyData responseBodyData = aR.getData().getBody().getData();
            if (CollectionUtils.isNotEmpty(responseBodyData.getStyles())) {
                resp.setStyleList(responseBodyData.getStyles());
                return R.ok().setData(resp);
            }

        }
        return R.error(aR.getErrorMsg());
    }
}
