package com.heyu.api.controller.face;


import com.aliyun.facebody20191230.models.DetectCelebrityResponse;
import com.aliyun.facebody20191230.models.DetectCelebrityResponseBody;
import com.heyu.api.alibaba.handle.facebody.ADetectCelebrityHandle;
import com.heyu.api.alibaba.request.facebody.ADetectCelebrityRequest;
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.card.CharacterRecognizeRequest;
import com.heyu.api.request.face.DetectCelebrityReq;
import com.heyu.api.resp.face.DetectCelebrityResp;
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;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/***
 * https://next.api.aliyun.com/api/facebody/2019-12-30/DetectCelebrity?tab=DEMO&lang=JAVA&RegionId=cn-shanghai
 *
 *
 *明星识别
 *
 * DetectCelebrity
 *功能描述
 * 明星识别能力可以识别图像中的明星人物。可以识别超过 2.7 万个明星人物。
 *
 *
 */
@Slf4j
@RestController
@RequestMapping("/celebrity")
@NotIntercept
public class DetectCelebrityController extends BaseController {


    @Autowired
    private ADetectCelebrityHandle aDetectCelebrityHandle;


    @RequestMapping("/recognize")
    @CacheResult
    public R recognize(DetectCelebrityReq req) {

        DetectCelebrityResp resp = new DetectCelebrityResp();

        ADetectCelebrityRequest request = new ADetectCelebrityRequest();
        request.setImageBase64(req.getImageBase64());
        request.setImageUrl(req.getImageUrl());
        ApiR<DetectCelebrityResponse> aR = aDetectCelebrityHandle.handle(request);
        if (aR.isSuccess() && isSuccessStatusCode(aR.getData().getStatusCode())) {
            DetectCelebrityResponseBody responseBody = aR.getData().getBody();
            DetectCelebrityResponseBody.DetectCelebrityResponseBodyData responseBodyData = responseBody.getData();
            List<DetectCelebrityResponseBody.DetectCelebrityResponseBodyDataFaceRecognizeResults> resultsList = responseBodyData.getFaceRecognizeResults();
            if (CollectionUtils.isNotEmpty(resultsList)) {
                List<String> names = new ArrayList<>();
                for (DetectCelebrityResponseBody.DetectCelebrityResponseBodyDataFaceRecognizeResults detectCelebrityResponseBodyDataFaceRecognizeResults : resultsList) {
                    names.add(detectCelebrityResponseBodyDataFaceRecognizeResults.getName());
                }
                resp.setNames(names);
                return R.ok().setData(resp);
            }
        }
        return R.error(aR.getErrorMsg());
    }
}
